// EmbeddedLines returns a sorted slice of lines that
// are common in every input string when the string is split
// by \n. That is, the input string has embedded newlines.
961043
func EmbeddedLines(inputs []string) []string {
	commonalities := make(map[string]int)
	for _, input := range inputs {
		split := strings.Split(input, "\n")
		for _, line := range split {
			commonalities[line] += 1
		}
	}

	max := 0
	for _, count := range commonalities {
		if count > max {
			max = count
		}
	}

	commonLines := []string{} // != nil

	for line, count := range commonalities {
		if count == max {
			commonLines = append(commonLines, line)
		}
	}

	sort.Strings(commonLines)

	return commonLines
}
1181400
func manualStrip(file []byte) []byte {
	stripped := []byte{}
	for _, line := range bytes.Split(file, []byte("\n")) {
		if bytes.HasPrefix(bytes.TrimSpace(line), []byte("#")) {
			continue
		}
		stripped = append(stripped, line...)
		stripped = append(stripped, '\n')
	}
	return stripped
}
241404
func (r *RefErrInvalidDetail) Lines() []string {
	lines := []string{r.Ref.String()}
	offset := len(r.Ref[:r.Pos].String()) + 1
	pad := strings.Repeat(" ", offset)
	lines = append(lines, fmt.Sprintf("%s^", pad))
	if r.Have != nil {
		lines = append(lines, fmt.Sprintf("%shave (type): %v", pad, r.Have))
	} else {
		lines = append(lines, fmt.Sprintf("%shave: %v", pad, r.Ref[r.Pos]))
	}
	if len(r.OneOf) > 0 {
		lines = append(lines, fmt.Sprintf("%swant (one of): %v", pad, r.OneOf))
	} else {
		lines = append(lines, fmt.Sprintf("%swant (type): %v", pad, r.Want))
	}
	return lines
}
977095
func splitLine(line string) []string {
	var s []string
	var start, end int
	for ; start < len(line); start++ {
		c := line[start]
		if !isWhitespace(c) {
			break
		}
	}
	var ws bool
	for end = start; end < len(line); end++ {
		c := line[end]
		if !isWhitespace(c) {
			ws = false
			continue
		}
		if ws == true {
			start++
			continue
		}
		ws = true
		s = append(s, line[start:end])
		start = end + 1
	}
	if start != end {
		s = append(s, line[start:end])
	}
	return s
}
470357
func (loc *Location) unmatchedLines(re *regexp.Regexp) []Line {
	var lines []Line
	for _, ln := range loc.Line {
		if fn := ln.Function; fn != nil {
			if re.MatchString(fn.Name) {
				continue
			}
			if re.MatchString(fn.Filename) {
				continue
			}
		}
		lines = append(lines, ln)
	}
	return lines
}
118083
func (t *Tree) elideNewline() Node {
	token := t.peek()
	if token.typ != itemText {
		t.unexpected(token, "input")
		return nil
	}

	t.next()
	stripped := strings.TrimLeft(token.val, "\n\r")
	diff := len(token.val) - len(stripped)
	if diff > 0 {
		// This is a bit nasty. We mutate the token in-place to remove
		// preceding newlines.
		token.pos += Pos(diff)
		token.val = stripped
	}
	return t.newText(token.pos, token.val)
}
622628
func ForEachLine(json string, iterator func(line Result) bool) {
	var res Result
	var i int
	for {
		i, res, _ = parseAny(json, i, true)
		if !res.Exists() {
			break
		}
		if !iterator(res) {
			return
		}
	}
}
671536
func (f *File) Lines() ([]string, error) {
	content, err := f.Contents()
	if err != nil {
		return nil, err
	}

	splits := strings.Split(content, "\n")
	// remove the last line if it is empty
	if splits[len(splits)-1] == "" {
		return splits[:len(splits)-1], nil
	}

	return splits, nil
}
610571
func fileToLines(filename string) []string {
	contents, _ := ioutil.ReadFile(filename)
	return strings.Split(html.EscapeString(string(contents)), "\n")
}
922122
func (a *UnificationErrDetail) Lines() []string {
	lines := make([]string, 2)
	lines[0] = fmt.Sprint("left  : ", types.Sprint(a.Left))
	lines[1] = fmt.Sprint("right : ", types.Sprint(a.Right))
	return lines
}
335418
func getPatternFromLine(line string) (*regexp.Regexp, bool) {
	// Trim OS-specific carriage returns.
	line = strings.TrimRight(line, "\r")

	// Strip comments [Rule 2]
	if strings.HasPrefix(line, `#`) {
		return nil, false
	}

	// Trim string [Rule 3]
	// TODO: Handle [Rule 3], when the " " is escaped with a \
	line = strings.Trim(line, " ")

	// Exit for no-ops and return nil which will prevent us from
	// appending a pattern against this line
	if line == "" {
		return nil, false
	}

	// TODO: Handle [Rule 4] which negates the match for patterns leading with "!"
	negatePattern := false
	if line[0] == '!' {
		negatePattern = true
		line = line[1:]
	}

	// Handle [Rule 2, 4], when # or ! is escaped with a \
	// Handle [Rule 4] once we tag negatePattern, strip the leading ! char
	if regexp.MustCompile(`^(\#|\!)`).MatchString(line) {
		line = line[1:]
	}

	// If we encounter a foo/*.blah in a folder, prepend the / char
	if regexp.MustCompile(`([^\/+])/.*\*\.`).MatchString(line) && line[0] != '/' {
		line = "/" + line
	}

	// Handle escaping the "." char
	line = regexp.MustCompile(`\.`).ReplaceAllString(line, `\.`)

	magicStar := "#$~"

	// Handle "/**/" usage
	if strings.HasPrefix(line, "/**/") {
		line = line[1:]
	}
	line = regexp.MustCompile(`/\*\*/`).ReplaceAllString(line, `(/|/.+/)`)
	line = regexp.MustCompile(`\*\*/`).ReplaceAllString(line, `(|.`+magicStar+`/)`)
	line = regexp.MustCompile(`/\*\*`).ReplaceAllString(line, `(|/.`+magicStar+`)`)

	// Handle escaping the "*" char
	line = regexp.MustCompile(`\\\*`).ReplaceAllString(line, `\`+magicStar)
	line = regexp.MustCompile(`\*`).ReplaceAllString(line, `([^/]*)`)

	// Handle escaping the "?" char
	line = strings.Replace(line, "?", `\?`, -1)

	line = strings.Replace(line, magicStar, "*", -1)

	// Temporary regex
	var expr = ""
	if strings.HasSuffix(line, "/") {
		expr = line + "(|.*)$"
	} else {
		expr = line + "(|/.*)$"
	}
	if strings.HasPrefix(expr, "/") {
		expr = "^(|/)" + expr[1:]
	} else {
		expr = "^(|.*/)" + expr
	}
	pattern, _ := regexp.Compile(expr)

	return pattern, negatePattern
}
1044720
func TrimLinesInString(input string) string {
	return strings.Join(TrimLines(strings.Split(strings.TrimSpace(input), "\n")), "\n")
}
442733
func (enc *Encoder) AddEmbeddedJSONKey(key string, v *EmbeddedJSON) {
	if enc.hasKeys {
		if !enc.keyExists(key) {
			return
		}
	}
	enc.grow(len(key) + len(*v) + 5)
	r := enc.getPreviousRune()
	if r != '{' {
		enc.writeByte(',')
	}
	enc.writeByte('"')
	enc.writeStringEscape(key)
	enc.writeBytes(objKey)
	enc.writeBytes(*v)
}
173600
func getLines(input []byte, commentMarker []byte) [][]byte {
	lines := bytes.Split(input, []byte("\n"))
	var output [][]byte
	for _, currentLine := range lines {
		var commentIndex = bytes.Index(currentLine, commentMarker)
		if commentIndex == -1 {
			output = append(output, currentLine)
		} else {
			output = append(output, currentLine[:commentIndex])
		}
	}
	return output
}
559200
func (mp *MacroProcessor) ProcessLines(lines []*Line, readMacros bool) []*Line {
	result := make([]*Line, 0, len(lines))
	for _, line := range lines {
		if line.Pattern != nil {
			resultLine := Line{
				Pattern: line.Pattern,
				Attrs:   make([]*Attr, 0, len(line.Attrs)),
			}
			for _, attr := range line.Attrs {
				macros := mp.macros[attr.K]
				if attr.V == "true" && macros != nil {
					resultLine.Attrs = append(
						resultLine.Attrs,
						macros...,
					)
				}

				// Git copies through aliases as well as
				// expanding them.
				resultLine.Attrs = append(
					resultLine.Attrs,
					attr,
				)
			}
			result = append(result, &resultLine)
		} else if readMacros {
			mp.macros[line.Macro] = line.Attrs
		}
	}
	return result
}
1262675
func (diffSection *DiffSection) Line(lineType DiffLineType, idx int) *DiffLine {
	var (
		difference    = 0
		addCount      = 0
		delCount      = 0
		matchDiffLine *DiffLine
	)

LOOP:
	for _, diffLine := range diffSection.Lines {
		switch diffLine.Type {
		case DIFF_LINE_ADD:
			addCount++
		case DIFF_LINE_DEL:
			delCount++
		default:
			if matchDiffLine != nil {
				break LOOP
			}
			difference = diffLine.RightIdx - diffLine.LeftIdx
			addCount = 0
			delCount = 0
		}

		switch lineType {
		case DIFF_LINE_DEL:
			if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
				matchDiffLine = diffLine
			}
		case DIFF_LINE_ADD:
			if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
				matchDiffLine = diffLine
			}
		}
	}

	if addCount == delCount {
		return matchDiffLine
	}
	return nil
}
808117
func string_splitlines(fnname string, recv Value, args Tuple, kwargs []Tuple) (Value, error) {
	var keepends bool
	if err := UnpackPositionalArgs(fnname, args, kwargs, 0, &keepends); err != nil {
		return nil, err
	}
	s := string(recv.(String))
	var lines []string
	// TODO(adonovan): handle CRLF correctly.
	if keepends {
		lines = strings.SplitAfter(s, "\n")
	} else {
		lines = strings.Split(s, "\n")
	}
	if strings.HasSuffix(s, "\n") {
		lines = lines[:len(lines)-1]
	}
	list := make([]Value, len(lines))
	for i, x := range lines {
		list[i] = String(x)
	}
	return NewList(list), nil
}