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 }
func nextRune(r *strings.Reader) rune { ch, _, err := r.ReadRune() if err != nil { return utf8.RuneError } return unicode.ToLower(ch) }
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) } }
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++ } }
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} }
// 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) }
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) } } } }
// 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) }
// 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) }
func atoi_(reader *strings.Reader) (int, int) { n := 0 count := 0 for reader.Len() > 0 { ch, _, _ := reader.ReadRune() index := strings.IndexRune("0123456789", ch) if index >= 0 { n = n*10 + index count++ } else { reader.UnreadRune() break } } return n, count }
// 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) }
// 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 }
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) }
func (t *Textview) uiDraw() { t.ui.beginDraw() defer t.ui.endDraw() var reader *strings.Reader line := len(t.parsedLines) - 1 - t.CurrentLine if line < 0 { line = 0 } totalLines := len(t.parsedLines) if totalLines == 0 { totalLines = 1 } currentScrollLine := t.y1 - 1 - int((float32(t.CurrentLine)/float32(totalLines))*float32(t.y1-t.y0)) for y := t.y1 - 1; y >= t.y0; y-- { if t.parsedLines != nil && line >= 0 { reader = strings.NewReader(t.parsedLines[line]) } else { reader = nil } for x := t.x0; x < t.x1; x++ { var chr rune = ' ' if x == t.x1-1 { // scrollbar if y == currentScrollLine { chr = '█' } else { chr = '░' } } else if x < t.x1-3 { if reader != nil { if ch, _, err := reader.ReadRune(); err == nil { chr = ch } } } termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg)) } line-- } }
func scanToken(rd *strings.Reader) (token string, err error) { r, _, err := rd.ReadRune() if err != nil { return "", err } if isSplitRune(r) { return string(r), nil } var tok []rune for { tok = append(tok, r) r, _, err = rd.ReadRune() if err != nil { return "", err } if isSplitRune(r) { rd.UnreadRune() return string(tok), nil } } }
func collectStringLiteral(r *strings.Reader) StringLiteral { // pick up the opening quotation mark for ch, _, err := r.ReadRune(); err == nil && ch != '"'; ch, _, err = r.ReadRune() { } var buffer bytes.Buffer // collect till we get the closing quotation mark for ch, _, err := r.ReadRune(); err == nil && ch != '"'; ch, _, err = r.ReadRune() { buffer.WriteRune(ch) } return StringLiteral{buffer.String()} }