本文整理汇总了Golang中strings.Reader类的典型用法代码示例。如果您正苦于以下问题:Golang Reader类的具体用法?Golang Reader怎么用?Golang Reader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Reader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: quotedWordCutter
func quotedWordCutter(reader *strings.Reader) (string, bool) {
var buffer bytes.Buffer
for {
if reader.Len() <= 0 {
return "", false
}
ch, _, _ := reader.ReadRune()
if ch != ' ' {
reader.UnreadRune()
break
}
}
quote := NULQUOTE
yenCount := 0
for reader.Len() > 0 {
ch, _, _ := reader.ReadRune()
if yenCount%2 == 0 {
if quote == NULQUOTE && (ch == '"' || ch == '\'') {
quote = ch
} else if quote != NULQUOTE && ch == quote {
quote = NULQUOTE
}
}
if ch == ' ' && quote == NULQUOTE {
break
}
if ch == '\\' {
yenCount++
} else {
yenCount = 0
}
buffer.WriteRune(ch)
}
return buffer.String(), true
}
示例2: insertHistory
func insertHistory(buffer *bytes.Buffer, reader *strings.Reader, historyNo int) {
history1 := conio.DefaultEditor.Histories[historyNo]
ch, siz, _ := reader.ReadRune()
if siz > 0 && ch == '^' {
if len(history1.Word) >= 2 {
buffer.WriteString(history1.At(1))
}
} else if siz > 0 && ch == '$' {
if len(history1.Word) >= 2 {
buffer.WriteString(history1.At(-1))
}
} else if siz > 0 && ch == '*' {
if len(history1.Word) >= 2 {
buffer.WriteString(strings.Join(history1.Word[1:], " "))
}
} else if siz > 0 && ch == ':' {
n, count := atoi_(reader)
if count <= 0 {
buffer.WriteRune(':')
} else if n < len(history1.Word) {
buffer.WriteString(history1.Word[n])
}
} else {
if siz > 0 {
reader.UnreadRune()
}
buffer.WriteString(history1.Line)
}
}
示例3: nextRune
func nextRune(r *strings.Reader) rune {
ch, _, err := r.ReadRune()
if err != nil {
return utf8.RuneError
}
return unicode.ToLower(ch)
}
示例4: uiDraw
func (t *Tree) uiDraw() {
t.ui.beginDraw()
defer t.ui.endDraw()
if t.lines == nil {
t.Rebuild()
}
line := 0
for y := t.y0; y < t.y1; y++ {
var reader *strings.Reader
var item TreeItem
level := 0
if line < len(t.lines) {
item = t.lines[line].Item
level = t.lines[line].Level
reader = strings.NewReader(item.String())
}
for x := t.x0; x < t.x1; x++ {
var chr rune = ' '
fg := t.Fg
bg := t.Bg
dx := x - t.x0
dy := y - t.y0
if reader != nil && level*2 <= dx {
if ch, _, err := reader.ReadRune(); err == nil {
chr = ch
fg, bg = item.TreeItemStyle(fg, bg, t.active && t.activeLine == dy)
}
}
termbox.SetCell(x, y, chr, termbox.Attribute(fg), termbox.Attribute(bg))
}
line++
}
}
示例5: sum
func sum(stream *strings.Reader) (s int) {
v, err := stream.ReadByte()
for ; v != 0 && err != os.EOF; v, err = stream.ReadByte() {
s += int(v)
}
return
}
示例6: collectQuotation
func collectQuotation(r *strings.Reader) *Quotation {
var buffer bytes.Buffer
for ch, _, err := r.ReadRune(); err == nil && ch != ']'; ch, _, err = r.ReadRune() {
buffer.WriteRune(ch)
}
parsedTokens := tokenize(buffer.String())
return &Quotation{*parsedTokens}
}
示例7: Get
// Get the Trie at the end of a strings.Reader
func (t *Trie) Get(s *strings.Reader) *Trie {
rune, _, err := s.ReadRune()
if err != nil {
// We have reached EOF
return t
}
t = t.children[rune]
if t == nil {
return nil
}
return t.Get(s)
}
示例8: applyHeadersToRequest
func (s *GitUploadPackService) applyHeadersToRequest(req *http.Request, content *strings.Reader) {
req.Header.Add("User-Agent", "git/1.0")
req.Header.Add("Host", "github.com")
if content == nil {
req.Header.Add("Accept", "*/*")
} else {
req.Header.Add("Accept", "application/x-git-upload-pack-result")
req.Header.Add("Content-Type", "application/x-git-upload-pack-request")
req.Header.Add("Content-Length", string(content.Len()))
}
}
示例9: Example_reset
// In performance critical applications, Reset can be used to discard the
// current compressor or decompressor state and reinitialize them quickly
// by taking advantage of previously allocated memory.
func Example_reset() {
proverbs := []string{
"Don't communicate by sharing memory, share memory by communicating.\n",
"Concurrency is not parallelism.\n",
"The bigger the interface, the weaker the abstraction.\n",
"Documentation is for users.\n",
}
var r strings.Reader
var b bytes.Buffer
buf := make([]byte, 32<<10)
zw, err := flate.NewWriter(nil, flate.DefaultCompression)
if err != nil {
log.Fatal(err)
}
zr := flate.NewReader(nil)
for _, s := range proverbs {
r.Reset(s)
b.Reset()
// Reset the compressor and encode from some input stream.
zw.Reset(&b)
if _, err := io.CopyBuffer(zw, &r, buf); err != nil {
log.Fatal(err)
}
if err := zw.Close(); err != nil {
log.Fatal(err)
}
// Reset the decompressor and decode to some output stream.
if err := zr.(flate.Resetter).Reset(&b, nil); err != nil {
log.Fatal(err)
}
if _, err := io.CopyBuffer(os.Stdout, zr, buf); err != nil {
log.Fatal(err)
}
if err := zr.Close(); err != nil {
log.Fatal(err)
}
}
// Output:
// Don't communicate by sharing memory, share memory by communicating.
// Concurrency is not parallelism.
// The bigger the interface, the weaker the abstraction.
// Documentation is for users.
}
示例10: tokenize
func tokenize(mode int, maxTokenSize int, str *strings.Reader, l int, token string, buffer []string) []string {
ch, _, err := str.ReadRune()
if err == io.EOF {
if l > 0 {
buffer = append(buffer, token)
}
return buffer
} else {
switch mode {
case apostrophe:
if isLetter(ch) {
return tokenize(normal, maxTokenSize, str, l+2, token+"'"+string(ch), buffer)
} else {
buffer = append(buffer, token)
return tokenize(start, maxTokenSize, str, 0, "", buffer)
}
case start:
if isLetter(ch) {
return tokenize(normal, maxTokenSize, str, l+1, token+string(ch), buffer)
} else if isCJK(ch) {
buffer = append(buffer, string(ch))
return tokenize(start, maxTokenSize, str, 0, "", buffer)
} else {
return tokenize(start, maxTokenSize, str, 0, "", buffer)
}
case skip:
if isLetter(ch) || ch == '\'' {
return tokenize(skip, maxTokenSize, str, l+1, token, buffer)
} else {
return tokenize(start, maxTokenSize, str, 0, "", buffer)
}
default:
if isLetter(ch) {
if l+1 > maxTokenSize {
log.Printf("Token is too long: “%v”, skipping it", token)
return tokenize(skip, maxTokenSize, str, l+1, token, buffer)
}
return tokenize(normal, maxTokenSize, str, l+1, token+string(ch), buffer)
} else if ch == '\'' && l > 0 {
return tokenize(apostrophe, maxTokenSize, str, l, token, buffer)
} else {
if l > 0 {
buffer = append(buffer, token)
}
return tokenize(start, maxTokenSize, str, 0, "", buffer)
}
}
}
}
示例11: Insert
// Insert a strings.Reader into the Trie
func (t *Trie) Insert(s *strings.Reader) {
rune, _, err := s.ReadRune()
if err != nil {
// We have reached EOF
t.leaf = true
return
}
child := t.children[rune]
if child == nil {
child = NewTrie()
t.children[rune] = child
}
child.Insert(s)
}
示例12: addRunes
// Internal function: adds items to the trie, reading runes from a strings.Reader. It returns
// the leaf node at which the addition ends.
func (p *Trie) addRunes(r *strings.Reader) *Trie {
rune, _, err := r.ReadRune()
if err != nil {
p.leaf = true
return p
}
n := p.children[rune]
if n == nil {
n = NewTrie()
p.children[rune] = n
}
// recurse to store sub-runes below the new node
return n.addRunes(r)
}
示例13: removeRunes
// Internal string removal function. Returns true if this node is empty following the removal.
func (p *Trie) removeRunes(r *strings.Reader) bool {
rune, _, err := r.ReadRune()
if err != nil {
// remove value, remove leaf flag
p.value = nil
p.leaf = false
return len(p.children) == 0
}
child, ok := p.children[rune]
if ok && child.removeRunes(r) {
// the child is now empty following the removal, so prune it
p.children[rune] = nil, false
}
return len(p.children) == 0
}
示例14: includes
// Internal string inclusion function.
func (p *Trie) includes(r *strings.Reader) *Trie {
rune, _, err := r.ReadRune()
if err != nil {
if p.leaf {
return p
}
return nil
}
child, ok := p.children[rune]
if !ok {
return nil // no node for this rune was in the trie
}
// recurse down to the next node with the remainder of the string
return child.includes(r)
}
示例15: MatchReader
func (n *Node) MatchReader(reader *strings.Reader) bool {
if reader.Len() == 0 && n.Terminal {
return true
}
r, _, err := reader.ReadRune()
if err != nil {
fmt.Println("Bad rune")
}
next, ok := n.Next(r)
if !ok {
return false
}
return next.MatchReader(reader)
}