summary refs log tree commit diff stats
path: root/lib/std/private
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2021-01-05 10:52:26 -0600
committerGitHub <noreply@github.com>2021-01-05 17:52:26 +0100
commitc04f305bf791ee5ecfd17f5a40d009d9c6b6f07a (patch)
tree5c0a24f50e8fd073448ff98634d7d4c01e01f1da /lib/std/private
parent0c4bd65e8d2d81a5e52624215e864f7846eb320b (diff)
downloadNim-c04f305bf791ee5ecfd17f5a40d009d9c6b6f07a.tar.gz
make cstrutils work in VM (#16590)
* make cstrutils work in VM

* more
Diffstat (limited to 'lib/std/private')
-rw-r--r--lib/std/private/strimpl.nim29
1 files changed, 24 insertions, 5 deletions
diff --git a/lib/std/private/strimpl.nim b/lib/std/private/strimpl.nim
index ae752165a..3fa0dc1d3 100644
--- a/lib/std/private/strimpl.nim
+++ b/lib/std/private/strimpl.nim
@@ -4,13 +4,13 @@ func toLowerAscii*(c: char): char {.inline.} =
   else:
     result = c
 
-template firstCharCaseSensitiveImpl(a, b: typed, aLen, bLen: int) =
+template firstCharCaseSensitiveImpl[T: string | cstring](a, b: T, aLen, bLen: int) =
   if aLen == 0 or bLen == 0:
     return aLen - bLen
   if a[0] != b[0]: return ord(a[0]) - ord(b[0])
 
-template cmpIgnoreStyleImpl*(a, b: typed, firstCharCaseSensitive: static bool = false) =
-  # a, b are string or cstring
+template cmpIgnoreStyleImpl*[T: string | cstring](a, b: T,
+            firstCharCaseSensitive: static bool = false) =
   let aLen = a.len
   let bLen = b.len
   var i = 0
@@ -37,8 +37,8 @@ template cmpIgnoreStyleImpl*(a, b: typed, firstCharCaseSensitive: static bool =
     inc i
     inc j
 
-template cmpIgnoreCaseImpl*(a, b: typed, firstCharCaseSensitive: static bool = false) =
-  # a, b are string or cstring
+template cmpIgnoreCaseImpl*[T: string | cstring](a, b: T,
+        firstCharCaseSensitive: static bool = false) =
   let aLen = a.len
   let bLen = b.len
   var i = 0
@@ -51,3 +51,22 @@ template cmpIgnoreCaseImpl*(a, b: typed, firstCharCaseSensitive: static bool = f
     if result != 0: return
     inc i
   result = aLen - bLen
+
+template startsWithImpl*[T: string | cstring](s, prefix: T) =
+  let prefixLen = prefix.len
+  let sLen = s.len
+  var i = 0
+  while true:
+    if i >= prefixLen: return true
+    if i >= sLen or s[i] != prefix[i]: return false
+    inc(i)
+
+template endsWithImpl*[T: string | cstring](s, suffix: T) =
+  let suffixLen = suffix.len
+  let sLen = s.len
+  var i = 0
+  var j = sLen - suffixLen
+  while i+j >= 0 and i+j < sLen:
+    if s[i+j] != suffix[i]: return false
+    inc(i)
+  if i >= suffixLen: return true