mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 22:28:24 +02:00
More efficient multi line scanning
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
@@ -18,3 +19,46 @@ func Capitalize(x string) string {
|
||||
cr := strings.ToUpper(string(s))
|
||||
return cr + x[sz:]
|
||||
}
|
||||
|
||||
type ScanLines struct {
|
||||
entries []string
|
||||
|
||||
scanner *bufio.Scanner
|
||||
}
|
||||
|
||||
func NewScanLines(entries ...string) *ScanLines {
|
||||
return &ScanLines{entries: entries}
|
||||
}
|
||||
|
||||
func (self *ScanLines) Scan() bool {
|
||||
if self.scanner == nil {
|
||||
if len(self.entries) == 0 {
|
||||
return false
|
||||
}
|
||||
self.scanner = bufio.NewScanner(strings.NewReader(self.entries[0]))
|
||||
self.entries = self.entries[1:]
|
||||
return self.Scan()
|
||||
} else {
|
||||
if self.scanner.Scan() {
|
||||
return true
|
||||
}
|
||||
self.scanner = nil
|
||||
return self.Scan()
|
||||
}
|
||||
}
|
||||
|
||||
func (self *ScanLines) Text() string {
|
||||
if self.scanner == nil {
|
||||
return ""
|
||||
}
|
||||
return self.scanner.Text()
|
||||
}
|
||||
|
||||
func Splitlines(x string) []string {
|
||||
ans := make([]string, 0, 8)
|
||||
scanner := bufio.NewScanner(strings.NewReader(x))
|
||||
for scanner.Scan() {
|
||||
ans = append(ans, scanner.Text())
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user