當前位置: 首頁>>代碼示例>>Golang>>正文


Golang io.RuneReader類代碼示例

本文整理匯總了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
}
開發者ID:boutros,項目名稱:unistat,代碼行數:61,代碼來源:main.go

示例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
}
開發者ID:aliwatters,項目名稱:go-course-02,代碼行數:28,代碼來源:ex4.8.go

示例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
	}
}
開發者ID:achille-roussel,項目名稱:go-vu,代碼行數:26,代碼來源:draw.go

示例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
	}
}
開發者ID:achille-roussel,項目名稱:go-vu,代碼行數:32,代碼來源:measure.go

示例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,
	}
}
開發者ID:conlang-software-dev,項目名稱:Logopoeist,代碼行數:25,代碼來源:lexer.go

示例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
}
開發者ID:aoeu,項目名稱:acme,代碼行數:10,代碼來源:main.go

示例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
}
開發者ID:kiskovacs,項目名稱:golucene,代碼行數:10,代碼來源:tokenizerImpl.go

示例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
}
開發者ID:davidrjenni,項目名稱:Guru,代碼行數:10,代碼來源:guru.go

示例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
}
開發者ID:zxpbenson,項目名稱:rog-go,代碼行數:12,代碼來源:parse.go

示例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
}
開發者ID:joshlf,項目名稱:rot13,代碼行數:15,代碼來源:rot13_test.go

示例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
}
開發者ID:thriqon,項目名稱:sudoku,代碼行數:15,代碼來源:sudoku.go

示例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)
}
開發者ID:hailocab,項目名稱:ohmyglob,代碼行數:15,代碼來源:globset.go

示例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]
}
開發者ID:charlieegan3,項目名稱:sirjest,代碼行數:19,代碼來源:regex.go

示例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)
		}
	}
}
開發者ID:kubernetes,項目名稱:helm,代碼行數:19,代碼來源:parser.go

示例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++
			}
		}
	}
}
開發者ID:ptrr,項目名稱:gobot,代碼行數:26,代碼來源:parse_test.go


注:本文中的io.RuneReader類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。