本文整理匯總了Golang中io.RuneReader類的典型用法代碼示例。如果您正苦於以下問題:Golang RuneReader類的具體用法?Golang RuneReader怎麽用?Golang RuneReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了RuneReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Stat
// Stat calculates statistics for all runes read from r.
func (m *Main) Stat(r io.RuneReader) (Stats, error) {
var stats Stats
for {
// Read next character.
ch, sz, err := r.ReadRune()
if err == io.EOF {
break
} else if err != nil {
return stats, err
}
// Calculate stats.
stats.TotalN++
if unicode.IsControl(ch) {
stats.ControlN++
}
if unicode.IsDigit(ch) {
stats.DigitN++
}
if unicode.IsGraphic(ch) {
stats.GraphicN++
}
if unicode.IsLetter(ch) {
stats.LetterN++
}
if unicode.IsLower(ch) {
stats.LowerN++
}
if unicode.IsMark(ch) {
stats.MarkN++
}
if unicode.IsNumber(ch) {
stats.NumberN++
}
if unicode.IsPrint(ch) {
stats.PrintN++
}
if unicode.IsPunct(ch) {
stats.PunctN++
}
if unicode.IsSpace(ch) {
stats.SpaceN++
}
if unicode.IsSymbol(ch) {
stats.SymbolN++
}
if unicode.IsTitle(ch) {
stats.TitleN++
}
if unicode.IsUpper(ch) {
stats.UpperN++
}
if sz > 1 {
stats.MultiByteN++
}
}
return stats, nil
}
示例2: CharCount
// CharCount reads input and returns stats about unicode frequency
func CharCount(in io.RuneReader) UnicodeStat {
var results UnicodeStat
results.Count = make(map[rune]int) // counts of Unicode characters
results.UtfLen = make(map[int]int) // count of lengths of UTF-8 encodings
results.Category = make(map[string]int) // number of categorys like Unicode.IsNumber
results.Invalid = 0 // count of invalid UTF-8 characters
for {
r, n, err := in.ReadRune() // returns rune, nbytes, error
if err == io.EOF {
break
}
if err != nil {
fmt.Fprintf(os.Stderr, "charcount: %v\n", err)
os.Exit(1)
}
if r == unicode.ReplacementChar && n == 1 {
results.Invalid++
continue
}
results.Count[r]++
results.Category[CharType(r)]++
results.UtfLen[n]++
}
return results
}
示例3: Draw
func Draw(r io.RuneReader, f font.Face, dst draw.Image, src image.Image, bounds fixed.Rectangle26_6, dir Direction) (err error) {
var prev rune
var char rune
var origin = dir.Origin(f, bounds)
var dot = origin
var next fixed.Point26_6
for {
if char, _, err = r.ReadRune(); err != nil {
if err == io.EOF {
err = nil
}
return
}
dot, next = dir.Advance(f, origin, dot, prev, char)
if char != '\n' {
dr, mask, maskp := glyph(f, char, dot)
draw.DrawMask(dst, dr, src, dr.Min, mask, maskp, draw.Over)
}
dot, prev = next, char
}
}
示例4: Measure
func Measure(r io.RuneReader, f font.Face) (size fixed.Point26_6, err error) {
var char rune
var prev rune
w := fixed.Int26_6(0)
m := f.Metrics()
size.Y = m.Height + m.Descent
for {
if char, _, err = r.ReadRune(); err != nil {
if err == io.EOF {
err = nil
}
size.X = maxInt26_6(size.X, w)
return
}
if char == '\n' {
size.X = maxInt26_6(size.X, w)
size.Y += m.Height
w, prev = 0, 0
continue
}
if prev != 0 {
w += f.Kern(prev, char)
}
w += advance(f, char)
prev = char
}
}
示例5: Lex
func Lex(input io.RuneReader, start StateFn) *Lexer {
tokens := make(chan *Item)
go func() {
r, _, err := input.ReadRune()
buf := &RuneBuffer{
in: input,
r: r,
more: (err == nil),
}
for state := start; state != nil; {
state = state(buf, tokens)
}
close(tokens)
}()
first, ok := <-tokens
return &Lexer{
tokens: tokens,
next: first,
more: ok,
}
}
示例6: byteOffset
func byteOffset(r io.RuneReader, off int) (bo int, err error) {
for i := 0; i != off; i++ {
_, s, err := r.ReadRune()
if err != nil {
return 0, err
}
bo += s
}
return
}
示例7: readRunes
func readRunes(r io.RuneReader, buffer []rune) (int, error) {
for i, _ := range buffer {
ch, _, err := r.ReadRune()
if err != nil {
return i, err
}
buffer[i] = ch
}
return len(buffer), nil
}
示例8: byteOff
func byteOff(q int, r io.RuneReader) (off int, err error) {
for i := 0; i != q; i++ {
_, s, err := r.ReadRune()
if err != nil {
return 0, err
}
off += s
}
return
}
示例9: nextRune
func nextRune(rd io.RuneReader) (r rune, err error) {
for {
r, _, err = rd.ReadRune()
if err != nil {
return
}
if !unicode.IsSpace(r) {
return
}
}
return
}
示例10: readAllRunes
func readAllRunes(r io.RuneReader) ([]rune, error) {
p := make([]rune, 0)
for {
rn, _, err := r.ReadRune()
if err != nil {
if err == io.EOF {
return p, nil
}
return p, err
}
p = append(p, rn)
}
return p, nil
}
示例11: parseCell
func parseCell(r, c rune, sudoku Sudoku, rr io.RuneReader) (Sudoku, error) {
var x rune
var err error
for x = ' '; x != '.' && (x < '0' || x > '9'); x, _, err = rr.ReadRune() {
if err != nil {
return sudoku, err
}
}
if x >= '1' && x <= '9' {
return sudoku.withAssignment(coord(r, c), uint8(x-'0'))
}
return sudoku, nil
}
示例12: MatchReader
func (g globSetImpl) MatchReader(r io.RuneReader) bool {
// Drain the reader and convert to a byte array
b := make([]byte, 0, 10)
for {
rn, _, err := r.ReadRune()
if err == io.EOF {
break
} else if err != nil {
return false
}
b = append(b, byte(rn))
}
return g.Match(b)
}
示例13: fromReader
func fromReader(r io.RuneReader) []byte {
b := make([]byte, numReadBufferStartSize)
offset := 0
var err error = nil
for err == nil {
rune, runeWidth, err := r.ReadRune()
if err == nil {
b = grow_buffer(b, offset, runeWidth)
writeWidth := utf8.EncodeRune(b[offset:], rune)
if runeWidth != writeWidth {
panic("reading rune width not equal to the written rune width")
}
offset += writeWidth
} else {
break
}
}
return b[:offset]
}
示例14: runesUntil
func runesUntil(in io.RuneReader, stop map[rune]bool) ([]rune, rune, error) {
v := []rune{}
for {
switch r, _, e := in.ReadRune(); {
case e != nil:
return v, r, e
case inMap(r, stop):
return v, r, nil
case r == '\\':
next, _, e := in.ReadRune()
if e != nil {
return v, next, e
}
v = append(v, next)
default:
v = append(v, r)
}
}
}
示例15: BenchmarkNoopParse
// Loads same file as above benchmarks, but just counts
// newlines. This is to see how much of the time is spent
// actually parsing vs just reading runes.
func BenchmarkNoopParse(b *testing.B) {
b.StopTimer()
data, err := ioutil.ReadFile("parsebench.tcl")
if err != nil {
panic(err.String())
}
b.SetBytes(int64(len(data)))
b.StartTimer()
for i := 0; i < b.N; i++ {
nlcount := 0
var reader io.RuneReader = bufio.NewReader(bytes.NewBuffer(data))
done := false
for !done {
r, _, e := reader.ReadRune()
if e != nil {
done = true
}
if r == '\n' {
nlcount++
}
}
}
}