// readRune consumes and returns the next rune in the input.
// Newlines in Unix, DOS, or Mac format are treated as one rune, '\n'.
510803
func (sc *scanner) readRune() rune {
	if len(sc.rest) == 0 {
		sc.error(sc.pos, "internal scanner error: readRune at EOF")
		return 0 // unreachable but eliminates bounds-check below
	}

	// fast path: ASCII
	if b := sc.rest[0]; b < utf8.RuneSelf {
		r := rune(b)
		sc.rest = sc.rest[1:]
		if r == '\r' {
			if len(sc.rest) > 0 && sc.rest[0] == '\n' {
				sc.rest = sc.rest[1:]
			}
			r = '\n'
		}
		if r == '\n' {
			sc.pos.Line++
			sc.pos.Col = 1
		} else {
			sc.pos.Col++
		}
		return r
	}

	r, size := utf8.DecodeRune(sc.rest)
	sc.rest = sc.rest[size:]
	sc.pos.Col++
	return r
}
303223
func Rune() gopter.Gen {
	return genRune(Frequency(map[int]gopter.Gen{
		0xD800:                Int64Range(0, 0xD800),
		utf8.MaxRune - 0xDFFF: Int64Range(0xDFFF, int64(utf8.MaxRune)),
	}))
}
811650
func (l *StringLexer) Peek() (r rune) {
	r = l.Next()
	l.Backup()
	return r
}
635043
func (r *Reader) ReadLine() (line []byte, err error) {

	// Sets r.idx to first \n or \r, or -1 if not found.
	scan := func() {
		end := r.n
		i1 := bytes.IndexByte(r.buf[r.p:end], '\n')
		if i1 >= 0 {
			end = r.p + i1
		}
		i2 := bytes.IndexByte(r.buf[r.p:end], '\r')
		if i2 >= 0 {
			r.idx = r.p + i2
		} else if i1 >= 0 {
			r.idx = r.p + i1
		} else {
			r.idx = -1
		}
	}

	// Fills buffer if no \n or \r found and no error yet,
	// and then rescans for first \n or \r.
	fill := func() {
		if (r.idx < 0 || r.idx == r.n - 1) && r.err == nil {
			if r.p > 0 {
				// make room
				if r.p < r.n {
					copy(r.buf, r.buf[r.p:r.n])
				}
				r.n -= r.p
				r.p = 0
			}
			if r.n < len(r.buf) {
				var i int
				i, r.err = r.rd.Read(r.buf[r.n:])
				r.n += i
			}
			scan()
		}
	}

	lines := make([][]byte, 0)
	beginning := true
	for {

		fill()

		// if at beginning of line, skip EOL of previous line
		if beginning {
			var c byte
			for i := 0; i < 2; i++ {
				// 1 or 2 times \r or \n, but not \r\r or \n\n
				if r.idx == r.p && r.buf[r.p] != c {
					c = r.buf[r.p]
					r.p += 1
					scan() // reset r.idx
				} else {
					break
				}
			}
			fill()
			beginning = false
		}

		// if error and no more unscanned data, return saved parts if they exist, else return error
		if r.p == r.n && r.err != nil {
			if len(lines) > 0 {
				return buildline(lines, r.buf[0:0]), nil
			} else {
				return r.buf[0:0], r.err
			}
		}

		if r.p < r.n {
			if r.idx < 0 { // no EOL found
				buf := make([]byte, r.n-r.p)
				copy(buf, r.buf[r.p:r.n])
				lines = append(lines, buf)
				r.p = r.n
			} else { // EOL found
				p := r.p
				r.p = r.idx
				return buildline(lines, r.buf[p:r.p]), nil
			}
		}

	}
	panic("not reached")
}
1327931
func (s *scanner) consume(runes int) {
	if len(s.buffer) < runes {
		panic(fmt.Sprintf("Unable to consume %d runes from buffer.", runes))
	}

	s.lastTokenLine = s.line
	s.lastTokenCol = s.col
	s.lastTokenSize = runes

	s.buffer = s.buffer[runes:]
	s.col += runes
}
1307989
func reverseIndexRune(s string, r rune) int {
	if s == "" {
		return -1
	}

	rs := []rune(s)
	for i := len(rs) - 1; i >= 0; i-- {
		if rs[i] == r {
			return i
		}
	}

	return -1
}
971
func (c *Conn) ReadLine() (text string, ok bool) {
	ok = c.RwcScanner.Scan()
	return c.RwcScanner.Text(), ok
}
995854
func (b *buffer) insertRunes(runes []rune) error {
	for _, r := range runes {
		if err := b.insertRune(r); err != nil {
			return err
		}
	}
	return nil
}
324368
func NewConstRune(n rune) *ConstNumber {
	z := new(ConstNumber)
	z.Type = ConstRune
	z.Value.Re.Denom().SetInt64(1)
	z.Value.Re.Num().SetInt64(int64(n))
	return z
}
857525
func ReadLines(fileName string) ([]string, error) {
	file, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	var lines []string
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}
	return lines, scanner.Err()
}
141366
func ReadRune(input string) (theRune string, rest string, runeWidth int) {
	prop, size := width.LookupString(input)
	rest = input[size:]
	theRune = input[:size]
	switch prop.Kind() {
	case width.EastAsianFullwidth, width.EastAsianHalfwidth, width.EastAsianWide:
		runeWidth = 2
	default:
		runeWidth = 1
	}
	return
}
1059258
func ReadlinesUTF16(reader io.Reader) (generator chan []byte) {
	generator = make(chan []byte)
	go func() {
		var utf16Rune [2]byte
		var r rune
		var u [1]uint16

		// closing generator
		defer close(generator)

		line := make([]byte, 0, 4096)
		i := 0
		for read, err := reader.Read(utf16Rune[:]); read == 2 && err == nil; {
			u[0] = *(*uint16)(unsafe.Pointer(&utf16Rune[0]))
			// Filter out UTF16 BOM
			if u[0] == 0xfeff && i == 0 {
				goto tail
			}

			r = utf16.Decode(u[:])[0]
			if r == '\n' {
				fullLine := make([]byte, len(line))
				copy(fullLine, line)
				fullLine = bytes.TrimRight(fullLine, "\r\n")
				generator <- fullLine
				line = make([]byte, 0, 4096)
			} else {
				line = append(line, byte(r))
			}

		tail:
			read, err = reader.Read(utf16Rune[:])
			i++

		}
	}()
	return generator
}
141213
func (p *parser) read() {
	p.pt.offset += p.pt.w
	rn, n := utf8.DecodeRune(p.data[p.pt.offset:])
	p.pt.rn = rn
	p.pt.w = n
	p.pt.col++
	if rn == '\n' {
		p.pt.line++
		p.pt.col = 0
	}

	if rn == utf8.RuneError && n == 1 { // see utf8.DecodeRune
		if !p.allowInvalidUTF8 {
			p.addErr(errInvalidEncoding)
		}
	}
}
242343
func (c *Conn) ReadRune() (r rune, size int, err error) {
loop:
	r, size, err = c.r.ReadRune()
	if err != nil {
		return
	}
	if r != unicode.ReplacementChar || size != 1 {
		// Properly readed rune
		return
	}
	// Bad rune
	err = c.r.UnreadRune()
	if err != nil {
		return
	}
	// Read telnet command or escaped IAC
	_, retry, err := c.tryReadByte()
	if err != nil {
		return
	}
	if retry {
		// This bad rune was a beginning of telnet command. Try read next rune.
		goto loop
	}
	// Return escaped IAC as unicode.ReplacementChar
	return
}
1312978
func (p *responseParser) readLine() ([]byte, error) {
	line, err := p.rdr.ReadBytes(byte('\n'))
	if err != nil {
		return nil, NewNetError(err.Error())
	}

	if len(line) < termLen {
		return nil, ErrMalformed
	}

	if len(line) >= termLen {
		if line[len(line)-termLen] != '\r' {
			return nil, ErrMalformed
		}

		line = line[:len(line)-termLen]
	}

	return line, nil
}
91076
func Parse(r io.Reader) (Value, error) {
	rr := bufio.NewReader(r)
	data := []rune{}
	for {
		c, _, err := rr.ReadRune()
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, err
		}
		data = append(data, c)
	}
	return parse(data)
}
1353200
func (l *Lexer) peekRunePast(skip int) rune {
	if l.pos+skip+1 > len(l.input) {
		return rune(0)
	}

	for ; skip < len(l.input)-l.pos; skip++ {
		r, ri := utf8.DecodeRuneInString(l.input[l.pos+skip:])
		if ri != 1 {
			skip += (ri - 1)
		}
		if !unicode.IsSpace(r) {
			return r
		}
	}
	return rune(0)
}