diff options
author | bptato <nincsnevem662@gmail.com> | 2024-07-24 22:41:07 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-07-24 22:41:28 +0200 |
commit | 5a16c29c8d68e8907a1ecc93ba1cf810efaefe1e (patch) | |
tree | f729bc809f3b20e38af794260959f77ffaf178f4 /src/utils/twtstr.nim | |
parent | de2a70dc814658a8c72e7da6180ea5e16a8b985b (diff) | |
download | chawan-5a16c29c8d68e8907a1ecc93ba1cf810efaefe1e.tar.gz |
url: misc fixes & improvements
* fix various parsing bugs * rewrite state machine * other small optimizations
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r-- | src/utils/twtstr.nim | 38 |
1 files changed, 13 insertions, 25 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index a4345251..14e93bda 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -147,7 +147,7 @@ func stripAndCollapse*(s: string): string = else: result &= ' ' -func until*(s: string; c: set[char]; starti = 0): string = +func until*(s: openArray[char]; c: set[char]; starti = 0): string = result = "" for i in starti ..< s.len: if s[i] in c: @@ -364,14 +364,10 @@ proc percentEncode*(append: var string; c: char; set: set[char]; append.pushHex(c) proc percentEncode*(append: var string; s: string; set: set[char]; - spaceAsPlus = false) {.inline.} = + spaceAsPlus = false) = for c in s: append.percentEncode(c, set, spaceAsPlus) -func percentEncode*(c: char; set: set[char]; spaceAsPlus = false): string - {.inline.} = - result.percentEncode(c, set, spaceAsPlus) - func percentEncode*(s: string; set: set[char]; spaceAsPlus = false): string = result.percentEncode(s, set, spaceAsPlus) @@ -413,14 +409,11 @@ func dqEscape*(s: string): string = func join*(ss: openArray[string]; sep: char): string = if ss.len == 0: return "" - var n = ss.high - 1 - for i in 0..high(ss): - n += ss[i].len - result = newStringOfCap(n) - result &= ss[0] - for i in 1..high(ss): - result &= sep - result &= ss[i] + var s = ss[0] + for i in 1 ..< ss.len: + s &= sep + s &= ss[i] + return s proc passRealloc*(opaque, p: pointer; size: csize_t): pointer {.cdecl.} = return realloc(p, size) @@ -512,21 +505,16 @@ proc expandPath*(path: string): string = return $p.pw_dir / path.substr(usr.len) return path -func deleteChars*(s: string; todel: set[char]): string = - let i = s.find(todel) - if i == -1: - return s - var rs = s.substr(0, i - 1) - for j in i + 1 ..< s.len: - if s[j] in todel: - continue - rs &= s[j] - return rs +func deleteChars*(s: openArray[char]; todel: set[char]): string = + result = newStringOfCap(s.len) + for c in s: + if c notin todel: + result &= c func replaceControls*(s: string): string = result = newStringOfCap(s.len) for c in s: - if c in Controls - {' '}: + if c in Controls: result &= '^' result &= c.getControlLetter() else: |