Skip to content

Instantly share code, notes, and snippets.

@nitely
Created August 2, 2020 06:08
Show Gist options
  • Save nitely/3063868bede57b0c9fb5305242a6a6e7 to your computer and use it in GitHub Desktop.
Save nitely/3063868bede57b0c9fb5305242a6a6e7 to your computer and use it in GitHub Desktop.
Fast find for Rune / Unicode / utf-8 in Nim
from strutils import find
from unicode import Rune
# XXX do a simpler find when vm (or no memchr available),
# since this is only fast due to memchr
# untested
func find*(s: string, r: Rune, start: Natural = 0): int =
if r.ord < 0xff:
return find(s, r.char, start)
let c = (r.ord and 0xff).char
let rsize = r.size()
var i = start+rsize-1
var r2 = 0'u32
doAssert rsize >= 1 and rsize <= 4
while i < len(s):
i = find(s, c, i)
if i == -1:
return -1
for j in i-rsize-1 .. i:
r2 = (r2 shl 8) or s[j].uint32
if r.uint32 == r2:
return i-rsize-1
r2 = 0
inc i
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment