本文整理汇总了Golang中scanner.Scanner.Mode方法的典型用法代码示例。如果您正苦于以下问题:Golang Scanner.Mode方法的具体用法?Golang Scanner.Mode怎么用?Golang Scanner.Mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scanner.Scanner
的用法示例。
在下文中一共展示了Scanner.Mode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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
}