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 | |
| 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')
| -rw-r--r-- | src/bindings/libregexp.nim | 8 | ||||
| -rw-r--r-- | src/buffer/buffer.nim | 54 | ||||
| -rw-r--r-- | src/buffer/container.nim | 40 | ||||
| -rw-r--r-- | src/display/pager.nim | 2 | ||||
| -rw-r--r-- | src/js/regex.nim | 54 | ||||
| -rw-r--r-- | src/utils/twtstr.nim | 42 |
6 files changed, 100 insertions, 100 deletions
diff --git a/src/bindings/libregexp.nim b/src/bindings/libregexp.nim index 957730ad..ecc0813a 100644 --- a/src/bindings/libregexp.nim +++ b/src/bindings/libregexp.nim @@ -8,12 +8,12 @@ const proc lre_compile*(plen: ptr cint, error_msg: cstring, error_msg_size: cint, buf: cstring, buf_len: csize_t, re_flags: cint, - opaque: pointer): ptr uint8 {.importc: "lre_compile".} + opaque: pointer): ptr uint8 {.importc.} proc lre_exec*(capture: ptr ptr uint8, bc_buf: ptr uint8, cbuf: ptr uint8, cindex: cint, clen: cint, cbuf_type: cint, - opaque: pointer): cint {.importc: "lre_exec".} + opaque: pointer): cint {.importc.} -proc lre_get_capture_count*(bc_buf: ptr uint8): cint {.importc: "lre_get_capture_count".} +proc lre_get_capture_count*(bc_buf: ptr uint8): cint {.importc.} -proc lre_get_flags*(bc_buf: ptr uint8): cint {.importc: "lre_get_flags".} +proc lre_get_flags*(bc_buf: ptr uint8): cint {.importc.} diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim index b4a1c202..7b38b255 100644 --- a/src/buffer/buffer.nim +++ b/src/buffer/buffer.nim @@ -288,7 +288,7 @@ func cursorBytes(buffer: Buffer, y: int, cc: int): int = while i < line.len and w < cc: var r: Rune fastRuneAt(line, i, r) - w += r.width() + w += r.twidth(w) return i proc findPrevLink*(buffer: Buffer, cursorx, cursory: int): tuple[x, y: int] {.proxy.} = @@ -378,17 +378,14 @@ proc findNextLink*(buffer: Buffer, cursorx, cursory: int): tuple[x, y: int] {.pr proc findPrevMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: bool): BufferMatch {.proxy.} = if cursory >= buffer.lines.len: return - template return_if_match = - if res.success and res.captures.len > 0: - let cap = res.captures[^1] - let x = buffer.lines[y].str.width(cap.s) - let str = buffer.lines[y].str.substr(cap.s, cap.e - 1) - return BufferMatch(success: true, x: x, y: y, str: str) var y = cursory let b = buffer.cursorBytes(y, cursorx) - let b2 = if b > 0: b - buffer.lines[y].str.lastRune(b)[1] else: 0 - let res = regex.exec(buffer.lines[y].str, 0, b2) - return_if_match + let res = regex.exec(buffer.lines[y].str, 0, b) + if res.success and res.captures.len > 0: + let cap = res.captures[^1] + let x = buffer.lines[y].str.width(0, cap.s) + let str = buffer.lines[y].str.substr(cap.s, cap.e - 1) + return BufferMatch(success: true, x: x, y: y, str: str) dec y while true: if y < 0: @@ -396,27 +393,26 @@ proc findPrevMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: b y = buffer.lines.high else: break + let res = regex.exec(buffer.lines[y].str) + if res.success and res.captures.len > 0: + let cap = res.captures[^1] + let x = buffer.lines[y].str.width(0, cap.s) + let str = buffer.lines[y].str.substr(cap.s, cap.e - 1) + return BufferMatch(success: true, x: x, y: y, str: str) if y == cursory: - let res = regex.exec(buffer.lines[y].str, b, buffer.lines[y].str.len) - return_if_match break - let res = regex.exec(buffer.lines[y].str) - return_if_match dec y proc findNextMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: bool): BufferMatch {.proxy.} = if cursory >= buffer.lines.len: return - template return_if_match = - if res.success and res.captures.len > 0: - let cap = res.captures[0] - let x = buffer.lines[y].str.width(cap.s) - let str = buffer.lines[y].str.substr(cap.s, cap.e - 1) - return BufferMatch(success: true, x: x, y: y, str: str) var y = cursory - let b = buffer.cursorBytes(y, cursorx) - let b2 = if buffer.lines[y].str.len > b: b + buffer.lines[y].str.runeLenAt(b) else: b - let res = regex.exec(buffer.lines[y].str, b2, buffer.lines[y].str.len) - return_if_match + let b = buffer.cursorBytes(y, cursorx + 1) + let res = regex.exec(buffer.lines[y].str, b, buffer.lines[y].str.len) + if res.success and res.captures.len > 0: + let cap = res.captures[0] + let x = buffer.lines[y].str.width(0, cap.s) + let str = buffer.lines[y].str.substr(cap.s, cap.e - 1) + return BufferMatch(success: true, x: x, y: y, str: str) inc y while true: if y > buffer.lines.high: @@ -424,12 +420,14 @@ proc findNextMatch*(buffer: Buffer, regex: Regex, cursorx, cursory: int, wrap: b y = 0 else: break + let res = regex.exec(buffer.lines[y].str) + if res.success and res.captures.len > 0: + let cap = res.captures[0] + let x = buffer.lines[y].str.width(0, cap.s) + let str = buffer.lines[y].str.substr(cap.s, cap.e - 1) + return BufferMatch(success: true, x: x, y: y, str: str) if y == cursory: - let res = regex.exec(buffer.lines[y].str, 0, b) - return_if_match break - let res = regex.exec(buffer.lines[y].str) - return_if_match inc y proc gotoAnchor*(buffer: Buffer): tuple[x, y: int] {.proxy.} = diff --git a/src/buffer/container.nim b/src/buffer/container.nim index a3406463..2d2ff73e 100644 --- a/src/buffer/container.nim +++ b/src/buffer/container.nim @@ -601,37 +601,33 @@ proc clearSearchHighlights*(container: Container) = if container.highlights[i].clear: container.highlights.del(i) +proc onMatch(container: Container, res: BufferMatch) = + if res.success: + container.setCursorXY(res.x, res.y) + if container.hlon: + container.clearSearchHighlights() + let ex = res.str.twidth(res.x) - 1 + let hl = Highlight(x: res.x, y: res.y, endx: ex, endy: res.y, clear: true) + container.highlights.add(hl) + container.triggerEvent(UPDATE) + container.hlon = false + elif container.hlon: + container.clearSearchHighlights() + container.triggerEvent(UPDATE) + container.needslines = true + container.hlon = false + proc cursorNextMatch*(container: Container, regex: Regex, wrap: bool) {.jsfunc.} = container.iface .findNextMatch(regex, container.cursorx, container.cursory, wrap) .then(proc(res: BufferMatch) = - if res.success: - container.setCursorXY(res.x, res.y) - if container.hlon: - container.clearSearchHighlights() - let ex = res.str.twidth(res.x) - 1 - let hl = Highlight(x: res.x, y: res.y, endx: ex, endy: res.y, clear: true) - container.highlights.add(hl) - container.triggerEvent(UPDATE) - container.hlon = false - elif container.hlon: - container.clearSearchHighlights() - container.triggerEvent(UPDATE) - container.needslines = true - container.hlon = false) + container.onMatch(res)) proc cursorPrevMatch*(container: Container, regex: Regex, wrap: bool) {.jsfunc.} = container.iface .findPrevMatch(regex, container.cursorx, container.cursory, wrap) .then(proc(res: BufferMatch) = - if res.success: - container.setCursorXY(res.x, res.y) - if container.hlon: - container.clearSearchHighlights() - let ex = res.str.twidth(res.x) - 1 - let hl = Highlight(x: res.x, y: res.y, endx: ex, endy: res.y, clear: true) - container.highlights.add(hl) - container.hlon = false) + container.onMatch(res)) proc setLoadInfo(container: Container, msg: string) = container.loadinfo = msg diff --git a/src/display/pager.nim b/src/display/pager.nim index 7e4be928..bc9a33c1 100644 --- a/src/display/pager.nim +++ b/src/display/pager.nim @@ -649,11 +649,11 @@ proc updateReadLineISearch(pager: Pager, linemode: LineMode) = if x != "": pager.iregex = compileSearchRegex(x) pager.container.popCursorPos(true) if pager.iregex.isSome: + pager.container.hlon = true if linemode == ISEARCH_F: pager.container.cursorNextMatch(pager.iregex.get, pager.config.searchwrap) else: pager.container.cursorPrevMatch(pager.iregex.get, pager.config.searchwrap) - pager.container.hlon = true pager.container.pushCursorPos() of FINISH: if pager.iregex.isSome: diff --git a/src/js/regex.nim b/src/js/regex.nim index 2a205130..eb4fe365 100644 --- a/src/js/regex.nim +++ b/src/js/regex.nim @@ -6,6 +6,7 @@ import unicode import bindings/libregexp import bindings/quickjs import js/javascript +import utils/twtstr export LRE_FLAG_GLOBAL, @@ -158,21 +159,19 @@ proc compileSearchRegex*(str: string): Option[Regex] = else: assert false return compileRegex(str.substr(0, flagsi - 1), flags) -proc exec*(regex: Regex, str: string, start = 0, length = str.len): RegexResult = +proc exec*(regex: Regex, str: string, start = 0, length = -1, nocaps = false): RegexResult = + let length = if length == -1: + str.len + else: + length assert 0 <= start and start <= length, "Start: " & $start & ", length: " & $length & " str: " & $str let captureCount = lre_get_capture_count(regex.bytecode) - var capture: ptr ptr uint8 = nil if captureCount > 0: capture = cast[ptr ptr uint8](alloc0(sizeof(ptr uint8) * captureCount * 2)) - var cstr = cstring(str) - var ascii = true - for c in str: - if c > char(0x80): - ascii = false - break + let ascii = str.isAscii() var ustr: string16 if not ascii: if start != 0 or length != str.len: @@ -180,27 +179,25 @@ proc exec*(regex: Regex, str: string, start = 0, length = str.len): RegexResult else: ustr = toUTF16(str) cstr = cstring(ustr) - - let ret = lre_exec(capture, regex.bytecode, - cast[ptr uint8](cstr), cint(start), - cint(length), cint(not ascii), dummyContext) - - result.success = ret == 1 #TODO error handling? (-1) - - if result.success: - var i = 0 + let flags = lre_get_flags(regex.bytecode) + var start = start + while true: + let ret = lre_exec(capture, regex.bytecode, + cast[ptr uint8](cstr), cint(start), + cint(length), cint(not ascii), dummyContext) + if ret != 1: #TODO error handling? (-1) + break + result.success = true + if captureCount == 0 or nocaps: + break let cstrAddress = cast[int](cstr) + start = cast[ptr int](cast[int](capture) + sizeof(ptr uint8))[] - cstrAddress + var i = 0 while i < captureCount * sizeof(ptr uint8): - let startPointerAddress = cast[int](capture) + i + let s = cast[ptr int](cast[int](capture) + i)[] - cstrAddress i += sizeof(ptr uint8) - let endPointerAddress = cast[int](capture) + i + let e = cast[ptr int](cast[int](capture) + i)[] - cstrAddress i += sizeof(ptr uint8) - let startPointer = cast[ptr ptr uint8](startPointerAddress) - let endPointer = cast[ptr ptr uint8](endPointerAddress) - let startAddress = cast[int](startPointer[]) - let endAddress = cast[int](endPointer[]) - var s = startAddress - cstrAddress - var e = endAddress - cstrAddress if ascii: result.captures.add((s, e)) else: @@ -217,7 +214,10 @@ proc exec*(regex: Regex, str: string, start = 0, length = str.len): RegexResult fastRuneAt(ustr, i, r) e8 += r.size() result.captures.add((s8, e8)) - dealloc(capture) + if (flags and LRE_FLAG_GLOBAL) != 1: + break + if captureCount > 0: + dealloc(capture) proc match*(regex: Regex, str: string, start = 0, length = str.len): bool = - return regex.exec(str, start, length).success + return regex.exec(str, start, length, nocaps = true).success 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()) |