summaryrefslogtreecommitdiffstats
path: root/src/util/chars.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/chars.go')
-rw-r--r--src/util/chars.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/util/chars.go b/src/util/chars.go
index f84d365f..7c706ae8 100644
--- a/src/util/chars.go
+++ b/src/util/chars.go
@@ -1,6 +1,7 @@
package util
import (
+ "bytes"
"fmt"
"unicode"
"unicode/utf8"
@@ -74,6 +75,35 @@ func (chars *Chars) Bytes() []byte {
return chars.slice
}
+func (chars *Chars) NumLines(atMost int) int {
+ lines := 1
+ if runes := chars.optionalRunes(); runes != nil {
+ for _, r := range runes {
+ if r == '\n' {
+ lines++
+ }
+ if lines >= atMost {
+ return atMost
+ }
+ }
+ return lines
+ }
+
+ for idx := 0; idx < len(chars.slice); idx++ {
+ found := bytes.IndexByte(chars.slice[idx:], '\n')
+ if found < 0 {
+ break
+ }
+
+ idx += found
+ lines++
+ if lines >= atMost {
+ return atMost
+ }
+ }
+ return lines
+}
+
func (chars *Chars) optionalRunes() []rune {
if chars.inBytes {
return nil