// add a rune to the buffer; will grow until maxcap, then overwrite circularly
552871
func (b *circularRuneBuffer) put(r rune) bool {
	// the slice to grow into, if necessary
	var grow []rune

	switch {
	// offset too big, stop
	case b.length() == math.MaxInt32:
		return false

	// acting like a normal slice
	case len(b.runes) < cap(b.runes):
		b.runes = b.runes[:len(b.runes)+1]
		b.runes[len(b.runes)-1] = r
		return true

	// at max capacity, overwrite circularly
	case cap(b.runes) == b.maxcap:
		idx := int(b.offset) % len(b.runes)
		b.runes[idx] = r
		b.offset++
		return true

	// buffer is still small, grow normally
	case cap(b.runes) < b.maxcap/2:
		grow = make([]rune, len(b.runes), 2*cap(b.runes)+1)

	// buffer can be grown, but normal growth would put it past maxcap
	default:
		grow = make([]rune, len(b.runes), b.maxcap)
	}

	// copy to the new buffer, reasign the field
	copy(grow, b.runes)
	b.runes = grow

	// buffer has been grown, store normally
	return b.put(r)
}
185652
func (b *Builder) ReadRune() (r rune, size int, err error) {
	b.lastRead = opInvalid
	if b.off >= len(b.buf) {
		// Buffer is empty, reset to recover space.
		b.Truncate(0)
		return 0, 0, io.EOF
	}
	b.lastRead = opReadRune
	c := b.buf[b.off]
	if c < utf8.RuneSelf {
		b.off++
		return rune(c), 1, nil
	}
	r, n := utf8.DecodeRune(b.buf[b.off:])
	b.off += n
	return r, n, nil
}
1016675
func (b *builderBuffer) Grow(n uint) []byte {
	l := uint(len(*b))
	if n > 0 {
		b.growCapacity(n)
		*b = (*b)[:l+n]
	}
	return (*b)[l:]
}
374953
func runes(s string) (r []rune) {
	for _, ch := range s {
		r = append(r, ch)
	}
	return
}
837763
func runeAt(body []byte, position int) (code rune, charWidth int) {
	if len(body) <= position {
		// <EOF>
		return -1, utf8.RuneError
	}

	c := body[position]
	if c < utf8.RuneSelf {
		return rune(c), 1
	}

	r, n := utf8.DecodeRune(body[position:])
	return r, n
}
438074
func (z *StreamLexer) PeekRune(pos int) (rune, int) {
	// from unicode/utf8
	c := z.Peek(pos)
	if c < 0xC0 {
		return rune(c), 1
	} else if c < 0xE0 {
		return rune(c&0x1F)<<6 | rune(z.Peek(pos+1)&0x3F), 2
	} else if c < 0xF0 {
		return rune(c&0x0F)<<12 | rune(z.Peek(pos+1)&0x3F)<<6 | rune(z.Peek(pos+2)&0x3F), 3
	}
	return rune(c&0x07)<<18 | rune(z.Peek(pos+1)&0x3F)<<12 | rune(z.Peek(pos+2)&0x3F)<<6 | rune(z.Peek(pos+3)&0x3F), 4
}
1141465
func (s *cacheShard) add(index uint64, obj interface{}) bool {
	s.Lock()
	defer s.Unlock()
	_, isOverwrite := s.items[index]
	if !isOverwrite && len(s.items) >= s.maxSize {
		var randomKey uint64
		for randomKey = range s.items {
			break
		}
		delete(s.items, randomKey)
	}
	s.items[index] = obj
	return isOverwrite
}
685231
func (p *parser) addUnitOne(ch rune) {
	if p.useOptionI() {
		ch = unicode.ToLower(ch)
	}

	p.unit = newRegexNodeCh(ntOne, p.options, ch)
}
966082
func (m *mruNonceMap) Add(nonce uint64) {
	m.mtx.Lock()
	defer m.mtx.Unlock()

	// When the limit is zero, nothing can be added to the map, so just
	// return.
	if m.limit == 0 {
		return
	}

	// When the entry already exists move it to the front of the list
	// thereby marking it most recently used.
	if node, exists := m.nonceMap[nonce]; exists {
		m.nonceList.MoveToFront(node)
		return
	}

	// Evict the least recently used entry (back of the list) if the the new
	// entry would exceed the size limit for the map.  Also reuse the list
	// node so a new one doesn't have to be allocated.
	if uint(len(m.nonceMap))+1 > m.limit {
		node := m.nonceList.Back()
		lru := node.Value.(uint64)

		// Evict least recently used item.
		delete(m.nonceMap, lru)

		// Reuse the list node of the item that was just evicted for the
		// new item.
		node.Value = nonce
		m.nonceList.MoveToFront(node)
		m.nonceMap[nonce] = node
		return
	}

	// The limit hasn't been reached yet, so just add the new item.
	node := m.nonceList.PushFront(nonce)
	m.nonceMap[nonce] = node
}
553220
func toBytes(runes []rune) []byte {
	var bytes []byte
	char := make([]byte, utf8.UTFMax)

	for _, r := range runes {
		n := utf8.EncodeRune(char, r)
		bytes = append(bytes, char[:n]...)
	}

	return bytes
}
1280987
func (b *Buffer) Add(m *rfc5424.Message) error {
	b.mu.Lock()
	defer b.mu.Unlock()

	if b.length == -1 {
		return errors.New("buffer closed")
	}
	if b.head == nil {
		b.head = &message{Message: *m}
		b.tail = b.head
	} else {
		// iterate from newest to oldest through messages to find position
		// to insert new message
		for other := b.tail; other != nil; other = other.prev {
			if m.Timestamp.Equal(other.Timestamp) && bytes.Equal(m.StructuredData, other.StructuredData) {
				// duplicate log line
				return nil
			}
			if m.Timestamp.Before(other.Timestamp) {
				if other.prev == nil {
					// insert before other at head
					other.prev = &message{Message: *m, next: other}
					b.head = other.prev
					break
				} else {
					continue
				}
			}
			msg := &message{Message: *m, prev: other}
			if other.next != nil {
				// insert between other and other.next
				other.next.prev = msg
				msg.next = other.next
			} else {
				// insert at tail
				b.tail = msg
			}
			other.next = msg
			break
		}
	}
	if b.length < b.capacity {
		// buffer not yet full
		b.length++
	} else {
		// at capacity, remove head
		b.head = b.head.next
		b.head.prev = nil
	}

	for msgc := range b.subs {
		select {
		case msgc <- m:
		default: // chan is full, drop this message to it
		}
	}

	return nil
}
768549
func RuneIn(ru rune, rs ...rune) int {
	for index, r := range rs {
		if r == ru {
			return index
		}
	}

	return -1
}
655984
func (opts *Options) SetMaxWriteBufferNumber(value int) {
	C.rocksdb_options_set_max_write_buffer_number(opts.c, C.int(value))
}
1279740
func (b *builderBuffer) growCapacity(n uint) {
	_b := *b
	curLen := uint(len(_b))
	curCap := uint(cap(_b))
	newCap := curLen + n
	if newCap <= curCap {
		// No need to do anything
		return
	}
	// Increase the capacity
	extra := newCap // Grow a bit more to avoid copying all the time
	if extra < minGrowDelta {
		extra = minGrowDelta
	} else if extra > maxGrowDelta {
		extra = maxGrowDelta
	}
	newBuffer := make(builderBuffer, curLen, newCap+extra)
	copy(newBuffer, _b)
	*b = newBuffer
}
1082253
func (r *RuneBuffer) MoveToLineStart() {
	for i := r.idx; i > 0; i-- {
		if r.buf[i-1] == '\n' {
			r.idx = i
			return
		}
	}
	r.idx = 0
}
1045765
func (t *Tombstoner) Add(keys [][]byte) error {
	return t.AddRange(keys, math.MinInt64, math.MaxInt64)
}
123365
func (i *Input) Add(src io.Reader) error {
	i.Mutex.Lock()
	defer i.Mutex.Unlock()
	if i.src != nil {
		return fmt.Errorf("Maximum number of sources reached: 1")
	}
	i.src = src
	return nil
}