コード例 #1
0
ファイル: i18n.go プロジェクト: N1xx1/golang-i18n
func parseFile(scanner *bufio.Scanner) (map[string]string, error) {
	translations := make(map[string]string)
	definitionRegexp := regexp.MustCompile(`^([\d\w\-_]+)\s*=\s*(".*")\s*(?:\#.*)?$`)
	emptyLineRegexp := regexp.MustCompile(`^(|\s*(\#.*)?)$`)

	for lineNumber := 1; scanner.Scan(); lineNumber++ {
		line := scanner.Text()

		if emptyLineRegexp.MatchString(line) {
			continue
		}

		matches := definitionRegexp.FindStringSubmatch(line)
		if len(matches) != 3 {
			return nil, errors.New(fmt.Sprintf("Malformed line :%d (%q)", lineNumber, line))
		}

		unquoted, err := strconv.Unquote(matches[2])
		if err != nil {
			return nil, errors.New(fmt.Sprintf("Malformed string :%d (%q)", lineNumber, line))
		}
		if _, ok := translations[matches[1]]; ok {
			return nil, errors.New(fmt.Sprintf("Multiple definitions of key %q", matches[1]))
		}

		translations[matches[1]] = unquoted
	}
	return translations, nil
}
コード例 #2
0
ファイル: 1880.go プロジェクト: vishwesh5/algo
func next(in *bufio.Scanner, shouldScan bool) int {
	if shouldScan {
		in.Scan()
	}
	n, _ := strconv.Atoi(in.Text())
	return n
}
コード例 #3
0
ファイル: main.go プロジェクト: ikravets/ev
func parseEventCodes(scanner *bufio.Scanner, row *TableRow) {
	spacesRegexp := regexp.MustCompile("\\s+")
	tableRowRegexp := regexp.MustCompile("^ “(\\w)” {15}(\\w.*) {7}")
	tableContRegexp := regexp.MustCompile("^ {19}(\\w.*)")

	if len(row.codes) != 0 {
		log.Fatal("row.codes is non-empty:", row.codes)
	}

	row.codes = make(map[byte]string)
	scanner.Scan()
	for {
		match := tableRowRegexp.FindStringSubmatch(scanner.Text())
		if match == nil {
			break
		}
		key := match[1]
		descr := match[2]
		for scanner.Scan() {
			match := tableContRegexp.FindStringSubmatch(scanner.Text())
			if match == nil {
				break
			}
			descr += " " + match[1]
		}
		descr = strings.TrimSpace(spacesRegexp.ReplaceAllLiteralString(descr, " "))
		row.codes[key[0]] = descr
	}
}
コード例 #4
0
ファイル: m3u.go プロジェクト: rychipman/m3u
func getNextLine(s *bufio.Scanner) string {
	more := s.Scan()
	if !more {
		checkScanError(s)
	}
	return s.Text()
}
コード例 #5
0
ファイル: main.go プロジェクト: ikravets/ev
func adhocTextFix(scanner *bufio.Scanner) string {
	text := scanner.Text()

	r := regexp.MustCompile("(^\\s*Open State.*Alphabetic    ) (The allowable values are:)")
	text = r.ReplaceAllString(text, "$1$2")

	r2 := regexp.MustCompile("^(Number Delta {26})(associated with the new quote)$")
	text = r2.ReplaceAllString(text, "$1                   $2")

	r3 := regexp.MustCompile("^ (Ask|Bid) Reference {41}The (ask|bid) reference number delta$")
	if r3.MatchString(text) {
		scanner.Scan()
		text = superimposeStrings(text, scanner.Text())
	}

	r4 := regexp.MustCompile("^Total Number of")
	if r4.MatchString(text) {
		text = "Total Number of Reference 5 2 Integer                 The number of single side deletes in this"
		scanner.Scan()
		scanner.Scan()
	}

	r5 := regexp.MustCompile("^Reference {45}The order/quote side reference number")
	if r5.MatchString(text) {
		scanner.Scan()
		scanner.Scan()
		text = ""
	}

	return text
}
コード例 #6
0
ファイル: cache.go プロジェクト: warmfusion/cache
// BuildRetentions build cache storage retention matchers
func (cs *CacheStorage) BuildRetentions(retentionScanner *bufio.Scanner) error {
	cs.retentions = make([]retentionMatcher, 0, 100)

	for retentionScanner.Scan() {
		line := retentionScanner.Text()
		if strings.HasPrefix(line, "#") || strings.Count(line, "=") != 1 {
			continue
		}

		pattern, err := regexp.Compile(strings.TrimSpace(strings.Split(line, "=")[1]))
		if err != nil {
			return err
		}

		retentionScanner.Scan()
		line = retentionScanner.Text()
		retentions := strings.TrimSpace(strings.Split(line, "=")[1])
		retention, err := rawRetentionToSeconds(retentions[0:strings.Index(retentions, ":")])
		if err != nil {
			return err
		}

		cs.retentions = append(cs.retentions, retentionMatcher{
			pattern:   pattern,
			retention: retention,
		})
	}
	return retentionScanner.Err()
}
コード例 #7
0
ファイル: import.go プロジェクト: unrealord/bitcannon
func importReader(reader io.Reader, gzipped bool) {
	var scanner *bufio.Scanner
	if gzipped {
		gReader, err := gzip.NewReader(reader)
		if err != nil {
			log.Println("[ERR] My bad! I tried to start uncompressing your archive but failed.")
			log.Println("      Try checking the file, or send me the file so I can check it out.")
			return
		}
		defer gReader.Close()
		log.Println("[OK!] GZip detected, unzipping enabled")
		scanner = bufio.NewScanner(gReader)
	} else {
		scanner = bufio.NewScanner(reader)
	}
	log.Println("[OK!] Reading initialized")
	imported := 0
	skipped := 0
	// Now we scan ୧༼ಠ益ಠ༽୨
	for scanner.Scan() {
		status, _ := importLine(scanner.Text())
		if status {
			imported++
		} else {
			skipped++
		}
	}
	log.Println("[OK!] Reading completed")
	log.Println("      " + strconv.Itoa(imported) + " torrents imported")
	log.Println("      " + strconv.Itoa(skipped) + " torrents skipped")
}
コード例 #8
0
ファイル: haircut.go プロジェクト: topher200/code-jam-haircut
// Returns the relevant information for the next problem
//
// Each problem is described on two lines:
//  1. First line includes the number of barbers and the customer position
//  2. Second line has the cutting times for each barber
//
// Returns a tuple of (<all the barbers in a []Barber list, the customerNumber).
// These have all be converted into ints (instead of strings).
//
// Returns error when EOF is reached (ie: no more problems).
func readNextProblem(scanner *bufio.Scanner) ([]Barber, int, error) {
	if !scanner.Scan() {
		return nil, 0, errors.New("All done!")
	}

	firstLine := splitString(scanner.Text())
	numBarbers := firstLine[0]
	customerNumber := firstLine[1]

	if !scanner.Scan() {
		panic("EOF at unexpected time")
	}
	barberTimes := splitString(scanner.Text())

	if len(barberTimes) != numBarbers {
		log.Panicf("Found %d barbers, expected to find %d\n",
			len(barberTimes), numBarbers)
	}

	barbers := make([]Barber, numBarbers)
	for b, barberTime := range barberTimes {
		// barberNumbers start at 1
		barbers[b] = Barber{barberTime, b + 1}
	}

	return barbers, customerNumber, nil
}
コード例 #9
0
ファイル: tpldata.go プロジェクト: lynndotconfig/trofaf
// Read the front matter from the post. If there is no front matter, this is
// not a valid post.
func readFrontMatter(s *bufio.Scanner) (map[string]string, error) {
	m := make(map[string]string)
	infm := false
	for s.Scan() {
		l := strings.Trim(s.Text(), " ")
		if l == "---" { // The front matter is delimited by 3 dashes
			if infm {
				// This signals the end of the front matter
				return m, nil
			} else {
				// This is the start of the front matter
				infm = true
			}
		} else if infm {
			sections := strings.SplitN(l, ":", 2)
			if len(sections) != 2 {
				// Invalid front matter line
				return nil, ErrInvalidFrontMatter
			}
			m[sections[0]] = strings.Trim(sections[1], " ")
		} else if l != "" {
			// No front matter, quit
			return nil, ErrMissingFrontMatter
		}
	}
	if err := s.Err(); err != nil {
		return nil, err
	}
	return nil, ErrEmptyPost
}
コード例 #10
0
ファイル: main.go プロジェクト: jeidsath/marginalia
func toString(scanner *bufio.Scanner) (string, error) {
	output := ""
	for scanner.Scan() {
		output += scanner.Text() + "\n"
	}
	return output, scanner.Err()
}
コード例 #11
0
ファイル: docker-fw.go プロジェクト: josegonzalez/docker-fw
func runCommandsFromScanner(scanner *bufio.Scanner, action string) error {
	lineNo := 0
	for scanner.Scan() {
		lineNo++

		// create a new 'commandLine' for each input line,
		// but always use same action for all lines
		commandLine := NewAction(action, false)
		// set executable name
		newArgs := []string{os.Args[0]}
		newArgs = append(newArgs, strings.Split(scanner.Text(), " ")...)
		if err := commandLine.Parse(newArgs); err != nil {
			return errors.New(fmt.Sprintf("%s: error at line %d: %s", commandLine.Action, lineNo, err))
		}

		err := commandLine.ExecuteAddAction()
		if err != nil {
			return errors.New(fmt.Sprintf("[file] %s: %s", commandLine.Action, err))
		}
	}

	if err := scanner.Err(); err != nil {
		return err
	}

	return nil
}
コード例 #12
0
ファイル: ptracer.go プロジェクト: dilgerma/scope
func pidForTid(tid int) (pid int, err error) {
	var (
		status  *os.File
		scanner *bufio.Scanner
		splits  []string
	)

	status, err = os.Open(fmt.Sprintf("/proc/%d/status", tid))
	if err != nil {
		return
	}
	defer status.Close()

	scanner = bufio.NewScanner(status)
	for scanner.Scan() {
		splits = strings.Split(scanner.Text(), ":")
		if splits[0] != "Tgid" {
			continue
		}

		pid, err = strconv.Atoi(strings.TrimSpace(splits[1]))
		return
	}

	if err = scanner.Err(); err != nil {
		return
	}

	err = fmt.Errorf("Pid not found for proc %d", tid)
	return
}
コード例 #13
0
ファイル: extract.go プロジェクト: noscripter/rexdep
func extractCore(module string, scanner *bufio.Scanner, config *Config) *Dependency {
	dependency := newDependency()
	enable := config.Start == nil
	for scanner.Scan() {
		line := scanner.Text()
		if config.Start != nil && config.Start.MatchString(line) {
			enable = true
		}
		if config.Module != nil {
			if matches := config.Module.FindStringSubmatch(line); matches != nil {
				module = matches[len(matches)-1]
			}
		}
		if enable {
			if matches := config.Pattern.FindStringSubmatch(line); len(matches) >= 1 {
				for _, name := range matches[1:] {
					if name != "" {
						dependency.add(module, name)
					}
				}
			}
		}
		if enable && config.End != nil && config.End.MatchString(line) {
			enable = false
		}
	}
	return dependency
}
コード例 #14
0
ファイル: filelist.go プロジェクト: sinfomicien/rkt
// parseHeader parses the first two lines of a block described in
// ParseFilelist docs. So it returns a data kind (files, symlinks or
// dirs) and a count of elements in the following list. If the
// returned kind is empty then it means that there is no more entries
// (provided that there is no error either).
func parseHeader(scanner *bufio.Scanner) (string, int, error) {
	if !scanner.Scan() {
		if err := scanner.Err(); err != nil {
			return "", 0, err
		}
		// no more entries in the file, just return empty kind
		return "", 0, nil
	}
	kind := scanner.Text()
	if kind == "" {
		return "", 0, fmt.Errorf("got an empty kind, expected 'files', 'symlinks' or 'dirs'")
	}
	if !scanner.Scan() {
		if err := scanner.Err(); err != nil {
			return "", 0, err
		} else {
			return "", 0, fmt.Errorf("expected a line with a count, unexpected EOF?")
		}
	}
	countReader := strings.NewReader(scanner.Text())
	count := 0
	n, err := fmt.Fscanf(countReader, "(%d)", &count)
	if err != nil {
		return "", 0, err
	}
	if n != 1 {
		return "", 0, fmt.Errorf("incorrectly formatted line with number of %s", kind)
	}
	return kind, count, nil
}
コード例 #15
0
ファイル: main.go プロジェクト: zeebo/mebipenny2015
func readStaff(scanner *bufio.Scanner) (out staff) {
	for scanner.Scan() {
		if len(scanner.Text()) == 0 {
			return out
		}

		text := strings.TrimSpace(replacer.Replace(scanner.Text()))
		line := make([]bool, 0, len(text))
		for i := 0; i < len(text); i++ {
			ch := text[i]
			if '0' <= ch && ch <= '9' {
				amount := int(ch - '0')
				i += amount - 1
				for j := 0; j < amount; j++ {
					line = append(line, true)
				}
			} else {
				line = append(line, false)
			}
		}

		out = append(out, line)
	}

	return out
}
コード例 #16
0
ファイル: rxq.go プロジェクト: proebsting/re
func main() {
	var ifile *bufio.Scanner
	if len(os.Args) == 2 {
		ifile = rx.MkScanner("-")
	} else if len(os.Args) == 3 {
		ifile = rx.MkScanner(os.Args[2])
	} else {
		log.Fatal("usage: rxq \"rexpr\" [file]")
	}
	spec := os.Args[1]
	fmt.Printf("regexp: %s\n", spec)
	dfa, err := rx.Compile(spec)
	if err != nil {
		log.Fatal(err)
	}

	// load and process candidate strings
	for i := 0; ifile.Scan(); i++ {
		s := ifile.Text()
		if dfa.Accepts(s) != nil {
			fmt.Println("accept:", s)
		} else {
			fmt.Println("REJECT:", s)
		}
	}
	rx.CkErr(ifile.Err())
}
コード例 #17
0
func main() {
	flag.Parse()
	flags := flag.Args()

	var text string
	var scanner *bufio.Scanner
	var err error

	if len(flags) > 0 {

		file, err := os.Open(flags[0])

		if err != nil {
			log.Fatal(err)
		}

		scanner = bufio.NewScanner(file)

	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	for scanner.Scan() {
		text += scanner.Text()
	}

	err = scanner.Err()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(text)
}
コード例 #18
0
ファイル: legacy_profile.go プロジェクト: google/pprof
// parseThreadSample parses a symbolized or unsymbolized stack trace.
// Returns the first line after the traceback, the sample (or nil if
// it hits a 'same-as-previous' marker) and an error.
func parseThreadSample(s *bufio.Scanner) (nextl string, addrs []uint64, err error) {
	var line string
	sameAsPrevious := false
	for s.Scan() {
		line = strings.TrimSpace(s.Text())
		if line == "" {
			continue
		}

		if strings.HasPrefix(line, "---") {
			break
		}
		if strings.Contains(line, "same as previous thread") {
			sameAsPrevious = true
			continue
		}

		addrs = append(addrs, parseHexAddresses(line)...)
	}
	if err := s.Err(); err != nil {
		return "", nil, err
	}
	if sameAsPrevious {
		return line, nil, nil
	}
	return line, addrs, nil
}
コード例 #19
0
ファイル: gdb.go プロジェクト: zuBux/crashwalk
func mustAddExtra(prefix string, scanner *bufio.Scanner, ci *crash.Info, die func()) {
	scanner.Scan()
	if !strings.HasPrefix(scanner.Text(), prefix) {
		die()
	}
	ci.Extra = append(ci.Extra, scanner.Text())
}
コード例 #20
0
ファイル: query.go プロジェクト: bac/juju
// QueryVerify writes a question to w and waits for an answer to be read from
// scanner.  It will pass the answer into the verify function.  Verify, if
// non-nil, should check the answer for validity, returning an error that will
// be written out to the user, or nil if answer is valid.
//
// This function takes a scanner rather than an io.Reader to avoid the case
// where the scanner reads past the delimiter and thus might lose data.  It is
// expected that this method will be used repeatedly with the same scanner if
// multiple queries are required.
func QueryVerify(question []byte, scanner *bufio.Scanner, w io.Writer, verify func(string) error) (answer string, err error) {
	defer fmt.Fprint(w, "\n")
	for {
		if _, err = w.Write(question); err != nil {
			return "", err
		}

		if !scanner.Scan() {
			if err := scanner.Err(); err != nil {
				return "", err
			}
			return "", io.EOF
		}
		answer = scanner.Text()
		if verify == nil {
			return answer, nil
		}
		err := verify(answer)
		// valid answer, return it!
		if err == nil {
			return answer, nil
		}
		// invalid answer, inform user of problem and retry.
		_, err = fmt.Fprint(w, err, "\n\n")
		if err != nil {
			return "", err
		}
	}
}
コード例 #21
0
ファイル: agora_test.go プロジェクト: jmptrader/agora
func readFrontMatter(s *bufio.Scanner) map[string]string {
	m := make(map[string]string)
	infm := false
	for s.Scan() {
		l := strings.Trim(s.Text(), " ")
		if l == "/*---" || l == "---*/" { // The front matter is delimited by 3 dashes and in a block comment
			if infm {
				// This signals the end of the front matter
				return m
			} else {
				// This is the start of the front matter
				infm = true
			}
		} else if infm {
			sections := strings.SplitN(l, ":", 2)
			if len(sections) != 2 {
				// Invalid front matter line
				return nil
			}
			m[sections[0]] = strings.Trim(sections[1], " ")
		} else if l != "" {
			// No front matter, quit
			return nil
		}
	}
	if err := s.Err(); err != nil {
		// The scanner stopped because of an error
		return nil
	}
	return nil
}
コード例 #22
0
ファイル: main.go プロジェクト: ericcapricorn/gotags
func readNames(names []string) ([]string, error) {
	if len(inputFile) == 0 {
		return names, nil
	}

	var scanner *bufio.Scanner
	if inputFile != "-" {
		in, err := os.Open(inputFile)
		if err != nil {
			return nil, err
		}

		defer in.Close()
		scanner = bufio.NewScanner(in)
	} else {
		scanner = bufio.NewScanner(os.Stdin)
	}

	for scanner.Scan() {
		names = append(names, scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return names, nil
}
コード例 #23
0
ファイル: shell.go プロジェクト: liquidz/shelltest
func scanLoop(scanner *bufio.Scanner, outCh chan string, outTermCh chan bool) {
	first := true
	buf := new(bytes.Buffer)

	for scanner.Scan() {
		text := scanner.Text()
		if DeleteColor(text) == Delimiter {
			if first {
				first = false
			} else {
				text := strings.TrimSpace(buf.String())
				DebugPrint("shell", "out <- [%v]", text)
				outCh <- text
				buf = new(bytes.Buffer)
			}
		} else {
			DebugPrint("shell", "read [%v]", text)
			buf.WriteString(text + "\n")
		}
	}
	text := strings.TrimSpace(buf.String())
	DebugPrint("shell", "out <- [%v]", text)
	if text != "" {
		outCh <- text
	}
	outTermCh <- true
}
コード例 #24
0
ファイル: spell-checker.go プロジェクト: XOP/de_faq
func parseHunspellOutput(scanner *bufio.Scanner) map[string]*TypeResult {
	line := 1
	types := make(map[string]*TypeResult)

	scanner.Scan()

	for scanner.Scan() {
		text := scanner.Text()

		if text == "" {
			line++
		} else {
			resultType := text[0:1]

			typeResult, ok := types[resultType]

			if !ok {
				typeResult = &TypeResult{}
				types[resultType] = typeResult
			}

			typeResult.results = append(typeResult.results, Result{line: line, word: strings.Trim(text[1:], " ")})
		}
	}

	return types
}
コード例 #25
0
ファイル: main.go プロジェクト: zeebo/mebipenny2015
func readBoard(scanner *bufio.Scanner) (out board) {
	for scanner.Scan() {
		parts := strings.Split(scanner.Text(), ",")
		out = append(out, parts)
	}
	return out
}
コード例 #26
0
ファイル: filelist.go プロジェクト: sinfomicien/rkt
// parseList parses the list part of a block. It makes sure that there
// is an exactly expected count of items.
func parseList(scanner *bufio.Scanner, count int) ([]string, error) {
	got := 0
	items := make([]string, 0, count)
	for {
		if !scanner.Scan() {
			if err := scanner.Err(); err != nil {
				return nil, err
			}
			return nil, fmt.Errorf("expected either an empty line or a line with an item, unexpected EOF?")
		}
		line := scanner.Text()
		if line == "" {
			if got < count {
				return nil, fmt.Errorf("too few items (declared %d, got %d)", count, got)
			}
			break
		}
		got++
		if got > count {
			return nil, fmt.Errorf("too many items (declared %d)", count)
		}
		items = append(items, line)
	}
	return items, nil
}
コード例 #27
0
ファイル: sudoku.go プロジェクト: brownjohnf/sudoku
// buildBoard builds a "board" (an array) by scanning input, splitting comma-
// separated integers and inserting them into an array.
func buildBoard(input *bufio.Scanner, board *[81]int) {
	l := 0

	for input.Scan() {
		for i, n := range strings.Split(input.Text(), ",") {
			var val int

			// If i is a dash, val is 0
			if n == "-" {
				val = 0
			} else {
				// Convert i to an int
				val2, err := strconv.Atoi(n)
				if err != nil {
					fmt.Println(os.Stderr, err)
					os.Exit(2)
				}
				val = val2
			}

			board[i+9*l] = val
		}

		l++
	}
}
コード例 #28
-1
ファイル: push.go プロジェクト: gophergala2016/fireup
func PushFile(host string, port int, fname string) {
	var scanner *bufio.Scanner

	if len(fname) == 0 {
		scanner = bufio.NewScanner(os.Stdin)
	} else {
		file, err := os.Open(fname)
		defer file.Close()

		if err != nil {
			fmt.Fprintln(os.Stderr, "ERROR", err)
			return
		}

		scanner = bufio.NewScanner(file)
	}

	addr := fmt.Sprintf("%s:%d", host, port)
	conn, err := net.Dial("tcp", addr)

	if err != nil {
		fmt.Fprintln(os.Stderr, "ERROR:", err)
		return
	}

	for scanner.Scan() {
		fmt.Fprintln(conn, scanner.Text())
	}
}
コード例 #29
-1
ファイル: readData.go プロジェクト: caneroj1/cardsData
func getContinueInput(scanner *bufio.Scanner) bool {
	ok := scanner.Scan()
	if !ok {
		return false
	}
	return scanner.Text() == "y"
}
コード例 #30
-25
ファイル: p345.go プロジェクト: benjgibbs/Go
func orderedRemoval(scanner *bufio.Scanner) {
	var entries Entries
	rows := 0
	count := 0
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Split(line, " ")
		parts = removeSpaces(parts)
		for col, str := range parts {
			entry := Entry{atoi(str), rows, col}
			entries = append(entries, entry)
			count++
		}
		rows++
	}
	fmt.Printf("Matrix dimensions: rows=%d, cols=%d\n", rows, count/rows)
	sort.Sort(entries)
	best := 0
	for i := 0; i < 1000; i++ {
		sum := run(entries, rows, count/rows)
		if sum > best {
			fmt.Printf("sum=%d, i=%d\n", sum, i)
			best = sum
		}
	}

}