/***************************************************/
/************** Readlines functions ****************/
/***************************************************/
196365
func min(i, k int) int {
	switch {
	case i < k:
		return i
	case k < i:
		return k
	default:
		return k
	}
}
971549
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
	f, err := os.Open(filename)
	if err != nil {
		return []string{""}, err
	}
	defer f.Close()

	var ret []string

	r := bufio.NewReader(f)
	for i := 0; i < n+int(offset) || n < 0; i++ {
		line, err := r.ReadString('\n')
		if err != nil {
			break
		}
		if i < int(offset) {
			continue
		}
		ret = append(ret, strings.Trim(line, "\n"))
	}

	return ret, nil
}
363510
func (bi *BinaryInfo) PCToLine(pc uint64) (string, int, *Function) {
	fn := bi.PCToFunc(pc)
	if fn == nil {
		return "", 0, nil
	}
	f, ln := fn.cu.lineInfo.PCToLine(fn.Entry, pc)
	return f, ln, fn
}
768362
func (pr *Prompter) getExpressionWithLiner(env *Glisp) (readin string, xs []Sexp, err error) {

	line, err := pr.Getline(nil)
	if err != nil {
		return "", nil, err
	}

	err = UnexpectedEnd
	var x []Sexp

	// parse and pause the parser if we need more input.
	env.parser.ResetAddNewInput(bytes.NewBuffer([]byte(line + "\n")))
	x, err = env.parser.ParseTokens()

	if len(x) > 0 {
		xs = append(xs, x...)
	}

	for err == ErrMoreInputNeeded || err == UnexpectedEnd || err == ResetRequested {
		nextline, err := pr.Getline(&continuationPrompt)
		if err != nil {
			return "", nil, err
		}
		// provide more input
		env.parser.NewInput(bytes.NewBuffer([]byte(nextline + "\n")))

		// get parsed expression tree(s) back in x
		x, err = env.parser.ParseTokens()
		if len(x) > 0 {
			for i := range x {
				if x[i] == SexpEnd {
					P("found an SexpEnd token, omitting it")
					continue
				}
				xs = append(xs, x[i])
			}
		}
		switch err {
		case nil:
			line += "\n" + nextline
			// no problem, parsing went fine.
			return line, xs, nil
		case ResetRequested:
			continue
		case ErrMoreInputNeeded:
			continue
		default:
			return "", nil, fmt.Errorf("Error on line %d: %v\n", env.parser.lexer.Linenum(), err)
		}
	}
	return line, xs, nil
}
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
}
270499
func (f Files) Lines(path string) []string {
	if f == nil || f[path] == nil {
		return []string{}
	}

	return strings.Split(string(f[path]), "\n")
}
861365
func NewLineScanner(r io.Reader) *LineScanner {
	br := bufio.NewReader(r)
	ls := &LineScanner{
		Reader: br,
	}
	return ls
}
70575
func (m *Matcher) Lines() []string {
	m.mut.Lock()
	defer m.mut.Unlock()
	return m.lines
}
1151582
func readLines(resp *http.Response) (lines []string, err error) {
	defer drainAndClose(resp.Body, &err)
	reader := bufio.NewReader(resp.Body)
	buffer := bytes.NewBuffer(make([]byte, 0, 128))
	var part []byte
	var prefix bool
	for {
		if part, prefix, err = reader.ReadLine(); err != nil {
			break
		}
		buffer.Write(part)
		if !prefix {
			lines = append(lines, buffer.String())
			buffer.Reset()
		}
	}
	if err == io.EOF {
		err = nil
	}
	return
}
778403
func (ctx *compctx) ErrorLine() int {
	var n int
	fmt.Sscanf(ctx.Error(), "Error > stdin:%d", &n)
	return n
}
117125
func ReadLines(file string, isGbk bool) (lines [][]string, err error) {
	//catch panic
	defer func() {
		if rerr := recover(); rerr != nil {
			err = errors.New(fmt.Sprintf("read csv file: %v, error: %v", file, rerr))
		}
	}()

	//open file
	fi, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer fi.Close()
	//get reader
	var reader *csv.Reader
	if !isGbk {
		reader = csv.NewReader(fi)
	} else {
		//transform gbk to utf8
		r := transform.NewReader(fi, simplifiedchinese.GBK.NewDecoder())
		reader = csv.NewReader(r)
	}
	lines, err = reader.ReadAll()
	return
}
951988
func lineFromConsole() (string, error) {
	bytes, _, err := bufio.NewReader(os.Stdin).ReadLine()
	return string(bytes), err
}
364721
func scanLinesFromFile(filename string) ([]string, error) {
	var targetsInput io.Reader
	if filename == "-" {
		targetsInput = os.Stdin
	} else if filename == "" {
		return nil, fmt.Errorf("you must specify an list of resources to idle")
	} else {
		inputFile, err := os.Open(filename)
		if err != nil {
			return nil, err
		}
		defer inputFile.Close()
		targetsInput = inputFile
	}

	lines := []string{}

	// grab the raw resources from the file
	lineScanner := bufio.NewScanner(targetsInput)
	for lineScanner.Scan() {
		line := lineScanner.Text()
		if line == "" {
			// skip empty lines
			continue
		}
		lines = append(lines, line)
	}
	if err := lineScanner.Err(); err != nil {
		return nil, err
	}

	return lines, nil
}
273635
func (r *ffReader) PosWithLine() (int, int) {
	currentLine := 1
	currentChar := 0

	for i := 0; i < r.i; i++ {
		c := r.s[i]
		currentChar++
		if c == '\n' {
			currentLine++
			currentChar = 0
		}
	}

	return currentLine, currentChar
}
447279
func WalkLines(inpReader io.Reader, walkFn WalkLineFn) error {
	reader := bufio.NewReader(inpReader)

	var walkErr error
	line, readErr := ReadLongLine(reader)
	for ; walkErr == nil && readErr == nil; line, readErr = ReadLongLine(reader) {
		walkErr = walkFn(line)
	}

	// if walk returned an error (other than io.EOF)
	// return that error
	if walkErr != nil && walkErr != io.EOF {
		return walkErr
	}

	// otherwise, if there was a read error (except io.EOF), return that
	if readErr != nil && readErr != io.EOF {
		return readErr
	}

	return nil
}
1286288
func (r *Reader) ReadLineString() (line string, err error) {
	b, e := r.ReadLine()
	return string(b), e
}
1293856
func MustReadLines(filename string) []string {
	r, err := ReadLines(filename)
	if err != nil {
		panic(err)
	}
	return r
}