aboutsummaryrefslogtreecommitdiffstats
path: root/src/strings
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-07-23 16:54:02 +0200
committerbptato <nincsnevem662@gmail.com>2022-07-23 17:39:06 +0200
commite7b53775a3f52cb5d8da865213c5dc38b954e33c (patch)
tree200741f8ed965f407c1a6af8823705db133bf3d4 /src/strings
parentc7f25b2fe470849e028f9502d3da0851f149f065 (diff)
downloadchawan-e7b53775a3f52cb5d8da865213c5dc38b954e33c.tar.gz
Improved incremental search: support unicode
Diffstat (limited to 'src/strings')
-rw-r--r--src/strings/charset.nim69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/strings/charset.nim b/src/strings/charset.nim
new file mode 100644
index 00000000..8726268a
--- /dev/null
+++ b/src/strings/charset.nim
@@ -0,0 +1,69 @@
+import unicode
+
+type string16* = distinct string
+
+# Convert a UTF-8 string to UTF-16.
+# Note: this doesn't check for (invalid) UTF-8 containing surrogates.
+proc toUTF16*(s: string): string16 =
+ var res = ""
+ var i = 0
+ template put16(c: uint16) =
+ res.setLen(res.len + 2)
+ res[i] = cast[char](c)
+ inc i
+ res[i] = cast[char](c shr 8)
+ inc i
+ for r in s.runes:
+ var c = uint32(r)
+ if c < 0x10000: # ucs-2
+ put16 uint16(c)
+ elif c <= 0x10FFFF: # surrogate
+ c -= 0x10000
+ put16 uint16((c shr 10) + 0xD800)
+ put16 uint16((c and 0x3FF) + 0xDC00)
+ else: # invalid
+ put16 uint16(0xFFFD)
+ result = string16(res)
+
+proc len*(s: string16): int {.borrow.}
+proc `[]`*(s: string16, i: int): char = string(s)[i]
+proc `[]`*(s: string16, i: BackwardsIndex): char = string(s)[i]
+
+template fastRuneAt*(s: string16, i: int, r: untyped, doInc = true, be = false) =
+ if i + 1 == s.len: # unmatched byte
+ when doInc: inc i
+ r = Rune(0xFFFD)
+ else:
+ when be:
+ var c1: uint32 = (uint32(s[i]) shl 8) + uint32(s[i + 1])
+ else:
+ var c1: uint32 = uint32(s[i]) + (uint32(s[i + 1]) shl 8)
+ if c1 >= 0xD800 or c1 < 0xDC00:
+ if i + 3 == s.len:
+ when doInc: i += 2
+ r = Rune(c1) # unmatched surrogate
+ else:
+ when be:
+ var c2: uint32 = (uint32(s[i + 2]) shl 8) + uint32(s[i + 3])
+ else:
+ var c2: uint32 = uint32(s[i + 2]) + (uint32(s[i + 3]) shl 8)
+ if c2 >= 0xDC00 and c2 < 0xE000:
+ r = Rune((((c1 and 0x3FF) shl 10) or (c2 and 0x3FF)) + 0x10000)
+ when doInc: i += 4
+ else:
+ r = Rune(c1) # unmatched surrogate
+ when doInc: i += 2
+ else:
+ r = Rune(c1) # ucs-2
+ when doInc: i += 2
+
+iterator runes*(s: string16): Rune =
+ var i = 0
+ var r: Rune
+ while i < s.len:
+ fastRuneAt(s, i, r)
+ yield r
+
+proc fromUTF16*(s: string16): string =
+ for r in s.runes:
+ result &= r