本文整理汇总了Golang中bufio.Scanner类的典型用法代码示例。如果您正苦于以下问题:Golang Scanner类的具体用法?Golang Scanner怎么用?Golang Scanner使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Scanner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getContinueInput
func getContinueInput(scanner *bufio.Scanner) bool {
ok := scanner.Scan()
if !ok {
return false
}
return scanner.Text() == "y"
}
示例2: PushFile
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())
}
}
示例3: GetNextLine
func GetNextLine(i int, s *bufio.Scanner) string {
ok := s.Scan()
if !ok {
panic(fmt.Sprintf("EOF. expected more lines during problem %v", i))
}
return s.Text()
}
示例4: media
func (this *M3U) media(scanner *bufio.Scanner) {
line := scanner.Text()
buf := make(map[string]string)
scanner.Scan()
l2 := scanner.Text()
scanner.Scan()
toMap(line[strings.Index(line, ":")+1:]+","+l2[strings.Index(l2, ":")+1:], buf)
rank := 0
switch buf["VIDEO"] {
case "chunked":
rank = 1
case "high":
rank = 2
case "medium":
rank = 3
case "low":
rank = 4
case "mobile":
rank = 5
}
this.PlayLists = append(this.PlayLists, M3UStream{
Video: buf["VIDEO"],
Name: buf["NAME"],
Resolution: buf["RESOLUTION"],
Location: scanner.Text(),
Rank: rank,
})
}
示例5: phasesFromLog14
// phasesFromLog14 parses the phases for a single Go 1.4 GC cycle.
func phasesFromLog14(scanner *bufio.Scanner) (phases []Phase, haveBegin bool) {
sub := gc14Log.FindStringSubmatch(scanner.Text())
n := atoi(sub[1])
stop, sweepTerm, markTerm, shrink := atoi(sub[2]), atoi(sub[3]), atoi(sub[4]), atoi(sub[5])
var begin int64
if sub[6] != "" {
begin = atoi64(sub[6][1:])
haveBegin = true
}
phases = []Phase{
// Go 1.5 includes stoptheworld() in sweep termination.
{0, int64(stop+sweepTerm) * 1000, PhaseSweepTerm, n, 1, 1, true},
// Go 1.5 includes stack shrink in mark termination.
{0, int64(markTerm+shrink) * 1000, PhaseMarkTerm, n, 1, 1, true},
{0, -1, PhaseSweep, n, 1, 0, false},
}
if haveBegin {
for i := range phases {
phases[i].Begin += begin
begin += phases[i].Duration
}
}
return
}
示例6: buildBoard
// 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++
}
}
示例7: next
func next(in *bufio.Scanner, shouldScan bool) int {
if shouldScan {
in.Scan()
}
n, _ := strconv.Atoi(in.Text())
return n
}
示例8: run
func run(ruleset *internal.RuleSet, scanner *bufio.Scanner, apiInstance api.API, unmatched *os.File) Statistics {
stat := Statistics{
perMetric: make(map[api.MetricKey]PerMetricStatistics),
}
for scanner.Scan() {
input := scanner.Text()
converted, matched := ruleset.MatchRule(input)
if matched {
stat.matched++
perMetric := stat.perMetric[converted.MetricKey]
perMetric.matched++
reversed, err := ruleset.ToGraphiteName(converted)
if *insertToDatabase {
apiInstance.AddMetric(converted)
}
if err != nil {
perMetric.reverseError++
} else if string(reversed) != input {
perMetric.reverseIncorrect++
} else {
perMetric.reverseSuccess++
}
stat.perMetric[converted.MetricKey] = perMetric
} else {
stat.unmatched++
if unmatched != nil {
unmatched.WriteString(input)
unmatched.WriteString("\n")
}
}
}
return stat
}
示例9: importReader
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")
}
示例10: getNextLine
func getNextLine(s *bufio.Scanner) string {
more := s.Scan()
if !more {
checkScanError(s)
}
return s.Text()
}
示例11: scanLoop
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
}
示例12: orderedRemoval
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
}
}
}
示例13: parseFile
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
}
示例14: readBoard
func readBoard(scanner *bufio.Scanner) (out board) {
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ",")
out = append(out, parts)
}
return out
}
示例15: adhocTextFix
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
}