diff options
author | bptato <nincsnevem662@gmail.com> | 2023-01-01 16:14:01 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-01-01 18:31:17 +0100 |
commit | 1a24350ee44859f6b3370ac15e8875a79fa7ae9b (patch) | |
tree | 0c168d725baa44bf1813c1456c9c835c4e1bf776 /src/utils | |
parent | 9a22a844c956d80f0d5c80330e898d52acadfd2f (diff) | |
download | chawan-1a24350ee44859f6b3370ac15e8875a79fa7ae9b.tar.gz |
buffer: fix some search bugs & refactor regex stuff
cursorBytes uses twidth now. cursorNextMatch matches the byte *after* the cursor (somewhat more consistently than before). match() no longer counts capture groups. LRE_FLAG_GLOBAL now goes through the entire string.
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/twtstr.nim | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 96d4bf2f..d89a70ac 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -162,6 +162,12 @@ func decValue*(c: char): int = func isAscii*(r: Rune): bool = return int32(r) < 128 +func isAscii*(s: string): bool = + for c in s: + if c > char(0x80): + return false + return true + const HexChars = "0123456789ABCDEF" func toHex*(c: char): string = result = newString(2) @@ -993,47 +999,47 @@ func width*(r: Rune): int = return int(width_table[int(r)]) {.pop.} -func width*(s: string, len: int): int = - var i = 0 +# Width, but also works with tabs. +# Needs the column width of the text so far. +func twidth*(r: Rune, w: int): int = + if r != Rune('\t'): + return r.width() + return ((w div 8) + 1) * 8 - w + +func width*(s: string): int = + for r in s.runes(): + result += r.twidth(result) + +func width*(s: string, start, len: int): int = + var i = start var m = len if m > s.len: m = s.len while i < m: var r: Rune fastRuneAt(s, i, r) - result += width(r) + result += r.twidth(result) func width*(s: seq[Rune]): int = for r in s: - result += width(r) + result += r.twidth(result) func width*(s: seq[Rune], min, max: int): int = var i = min var mi = min(max, s.len) while i < mi: - result += width(s[i]) + result += s[i].twidth(result) inc i func width*(s: seq[Rune], min: int): int = var i = min while i < s.len: - result += width(s[i]) + result += s[i].twidth(result) inc i -# Width, but also works with tabs. -# Needs the column width of the text so far. -func twidth*(r: Rune, w: int): int = - if r != Rune('\t'): - return r.width() - return ((w div 8) + 1) * 8 - w - -func width*(s: string): int = - for r in s.runes(): - result += twidth(r, result) - func twidth*(s: string, w: int): int = result = w for r in s.runes(): - result += twidth(r, result) + result += r.twidth(result) func breaksWord*(r: Rune): bool = return not (r.isDigitAscii() or r.width() == 0 or r.isAlpha()) |