summary refs log tree commit diff stats
path: root/lib/pure/strutils.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <andreas@andreas-desktop>2010-03-10 16:33:55 +0100
committerAndreas Rumpf <andreas@andreas-desktop>2010-03-10 16:33:55 +0100
commit96b828a3863c28680e8a5b0165c5e0bbcca7a46c (patch)
tree6ab8e58da5bf2dde1c0c865f60ef9ecf1a55ba15 /lib/pure/strutils.nim
parent2e280eafa8b13802980cf0e2f309342742e61347 (diff)
downloadNim-96b828a3863c28680e8a5b0165c5e0bbcca7a46c.tar.gz
bugfix: len openarray
Diffstat (limited to 'lib/pure/strutils.nim')
-rwxr-xr-xlib/pure/strutils.nim18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 724d00ee9..11ba14aa1 100755
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -748,6 +748,24 @@ proc toBin*(x: BiggestInt, len: int): string =
     shift = shift + 1
     mask = mask shl 1
 
+proc insertSep*(s: string, sep = '_', digits = 3): string = 
+  ## inserts the separator `sep` after `digits` digits from right to left.
+  ## Even though the algorithm works with any string `s`, it is only useful 
+  ## if `s` contains a number.
+  ## Example: ``insertSep("1000000") == "1_000_000"`` 
+  var L = (s.len-1) div digits + s.len
+  result = newString(L)
+  var j = 0
+  dec(L)
+  for i in countdown(len(s)-1, 0): 
+    if j == digits: 
+      result[L] = sep
+      dec(L)
+      j = 0
+    result[L] = s[i]
+    inc(j)
+    dec(L)
+
 proc escape*(s: string, prefix = "\"", suffix = "\""): string =
   ## Escapes a string `s`. This does these operations (at the same time):
   ## * replaces any ``\`` by ``\\``