当前位置: 首页>>代码示例>>Golang>>正文


Golang scanner.Scanner类代码示例

本文整理汇总了Golang中scanner.Scanner的典型用法代码示例。如果您正苦于以下问题:Golang Scanner类的具体用法?Golang Scanner怎么用?Golang Scanner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Scanner类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: readGridFromFile

func readGridFromFile(path string) (*ttypes.Grid, []ttypes.BotConf) {
	gridFile, err := os.Open(path, os.O_RDONLY, 0)
	if err != nil {
		log.Panic(err)
	}
	defer gridFile.Close()

	var s scanner.Scanner
	s.Init(gridFile)
	w := scanAndReadUint(&s)
	h := scanAndReadUint(&s)
	grid := simpleGrid(w, h)

	botConfs := make([]ttypes.BotConf, 0)
	for j := uint(0); j < h; j++ {
		for i := uint(0); i < w; i++ {
			tok := uint8(scanAndReadUint(&s))
			if tok > 0 {
				botConfs = append(botConfs, ttypes.BotConf{"build/test", i, j})
			}
		}
	}

	return grid, botConfs
}
开发者ID:jmptrader,项目名称:Tecellate,代码行数:25,代码来源:grid.go

示例2: expectToken

func expectToken(found token, expected string, s *scanner.Scanner) {
	if found.tokentype != expected {
		fmt.Println("On line ", s.LineNumber(), "; Expected ", expected, " found ", found.tokentype, " with value '", found.value, "'")
		os.Exit(1)
	}

}
开发者ID:bossen,项目名称:bdsolver,代码行数:7,代码来源:compiler.go

示例3: scanAndReadUint

func scanAndReadUint(s *scanner.Scanner) uint {
	s.Scan()
	tok, err := strconv.Atoui(s.TokenText())
	if err != nil {
		log.Panic(err)
	}
	return tok
}
开发者ID:jmptrader,项目名称:Tecellate,代码行数:8,代码来源:grid.go

示例4: main

/*func (d *Decoder) decode(src io.Reader) {

}
*/
func main() {
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Println("wrong number of arguments")
		os.Exit(1)
	}

	file, err := os.Open(flag.Arg(0), os.O_RDONLY, 0)
	if file == nil {
		fmt.Printf("can't open file; err=%s\n", err.String())
		os.Exit(1)
	}

	var s scanner.Scanner
	s.Init(file)
	tok := s.Scan()
	for tok != scanner.EOF {
		fmt.Println(scanner.TokenString(tok))
		tok = s.Scan()
	}
	fmt.Println()

	file.Close()
}
开发者ID:saschpe,项目名称:go-json,代码行数:28,代码来源:json.go

示例5: main

func main() {
	var s scanner.Scanner
	s.Init(os.Stdin)
	s.Scan()
	total, _ := strconv.Atoi(s.TokenText())
	for i := 1; i <= total; i++ {
		s.Scan()
		_ = s.TokenText()
		s.Scan()
		b, _ := strconv.Atoi(s.TokenText())
		var out string
		if b%2 == 0 {
			out = "WHITE"
		} else {
			out = "BLACK"
		}
		fmt.Printf("Case #%d: %s\n", i, out)
	}
}
开发者ID:jefferyyuan,项目名称:Google-Code-Jam,代码行数:19,代码来源:A.go

示例6: loadPatterns

func (h *Hyphenator) loadPatterns(reader io.Reader) os.Error {
	var s scanner.Scanner
	s.Init(reader)
	s.Mode = scanner.ScanIdents | scanner.ScanRawStrings | scanner.SkipComments

	var which string

	tok := s.Scan()
	for tok != scanner.EOF {
		switch tok {
		case scanner.Ident:
			// we handle two identifiers: 'patterns' and 'exceptions'
			switch ident := s.TokenText(); ident {
			case `patterns`, `exceptions`:
				which = ident
			default:
				return os.ErrorString(fmt.Sprintf("Unrecognized identifier '%s' at position %v",
					ident, s.Pos()))
			}
		case scanner.String, scanner.RawString:
			// trim the quotes from around the string
			tokstr := s.TokenText()
			str := tokstr[1 : len(tokstr)-1]

			switch which {
			case `patterns`:
				h.patterns.AddPatternString(str)
			case `exceptions`:
				key := strings.Replace(str, `-`, ``, -1)
				h.exceptions[key] = str
			}
		}
		tok = s.Scan()
	}
	return nil
}
开发者ID:AlanQuatermain,项目名称:go-hyphenator,代码行数:36,代码来源:hyphenator.go

示例7: Hyphenate

func (h *Hyphenator) Hyphenate(s, hyphen string) (string, bool) {
	var sc scanner.Scanner
	sc.Init(strings.NewReader(s))
	sc.Mode = scanner.ScanIdents
	sc.Whitespace = 0

	var outstr string

	tok := sc.Scan()
	for tok != scanner.EOF {
		switch tok {
		case scanner.Ident:
			// a word (or part thereof) to hyphenate
			t := sc.TokenText()

			// try the exceptions first
			exc := h.exceptions[t]
			if len(exc) != 0 {
				if hyphen != `-` {
					strings.Replace(exc, `-`, hyphen, -1)
				}
				return exc, true
			}

			// not an exception, hyphenate normally
			outstr += h.hyphenateWord(sc.TokenText(), hyphen)
		default:
			// A Unicode rune to append to the output
			p := make([]byte, utf8.UTFMax)
			l := utf8.EncodeRune(tok, p)
			outstr += string(p[0:l])
		}

		tok = sc.Scan()
	}

	return outstr, true
}
开发者ID:AlanQuatermain,项目名称:go-hyphenator,代码行数:38,代码来源:hyphenator.go

示例8: main

func main() {

	var scanInput scanner.Scanner
	scanInput.Init(os.Stdin)

	// main loop, reads scanner input and parses, EOF is strictly not used at this time.
	for scanInput.Scan() != scanner.EOF {
		switch scanInput.TokenText() {
		case "load", "l":
			scanInput.Scan()
			fmt.Printf("Loading file \"%s\" from database\n", scanInput.TokenText())
			go readHandler.LoadFile(scanInput.TokenText())
		case "save", "store", "s":
			scanInput.Scan()
			fmt.Printf("Storing file \"%s\" to database\n", scanInput.TokenText())
			go storeFile(scanInput.TokenText())
		case "hora":
			fmt.Printf("kingkong IS ONE!")
			os.Exit(0)
		}
	}
}
开发者ID:gynther,项目名称:ilait-exjobb,代码行数:22,代码来源:main.go

示例9: loadPatterns

func loadPatterns(reader io.Reader) (*Trie, os.Error) {
	trie := NewTrie()
	var s scanner.Scanner
	s.Init(reader)
	s.Mode = scanner.ScanIdents | scanner.ScanRawStrings | scanner.SkipComments

	var which string

	tok := s.Scan()
	for tok != scanner.EOF {
		switch tok {
		case scanner.Ident:
			// we handle two identifiers: 'patterns' and 'exceptions'
			switch ident := s.TokenText(); ident {
			case `patterns`, `exceptions`:
				which = ident
			default:
				return nil, os.ErrorString(fmt.Sprintf("Unrecognized identifier '%s' at position %v",
					ident, s.Pos()))
			}
		case scanner.String, scanner.RawString:
			// trim the quotes from around the string
			tokstr := s.TokenText()
			str := tokstr[1 : len(tokstr)-1]

			switch which {
			case `patterns`:
				trie.AddPatternString(str)
			}
		}
		tok = s.Scan()
	}

	return trie, nil
}
开发者ID:AlanQuatermain,项目名称:go-trie,代码行数:35,代码来源:trie_test.go

示例10: scan

func scan(c *scanner.Scanner) token {
	c.EatWhitespaceAndComments()
	if c.Peek() == '/' {
		c.ReadChar()
		return token{"Divide", ""}
	} else if c.EndOfFile() {
		log.Println("Found EOF")
		return token{"EOF", ""}
	} else if c.Peek() == '-' {
		c.ReadChar()
		chr := c.ReadChar()
		if chr != '>' {
			c.Fail("Found -, and expected a \"to\" statement(->) but found ", chr)
		}
		return token{"To", ""}
	} else if utils.IsAlphabetic(c.Peek()) {
		word := c.ReadWord()
		if word == "States" {
			return token{"States", ""}
		} else if word == "Edges" {
			return token{"Edges", ""}
		}
		return token{"Word", word}

	} else if utils.IsNumeric(c.Peek()) {
		integer := strconv.Itoa(c.ReadNumber())
		return token{"Integer", integer}
	} else {
		c.Fail("Unexpected keyword ", c.Peek())
	}
	return token{}
}
开发者ID:bossen,项目名称:bdsolver,代码行数:32,代码来源:compiler.go


注:本文中的scanner.Scanner类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。