summary refs log tree commit diff stats
path: root/lib/pure/strmisc.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2018-12-12 17:51:33 +0100
committerAraq <rumpf_a@web.de>2018-12-12 17:51:33 +0100
commitac8274c60f860b334a08199c4c0a65620962ef2a (patch)
tree6afc4046622ff10ff383a1e2a412bef325555158 /lib/pure/strmisc.nim
parenteb8383cb28e52bb9a790a743de442e4b71687001 (diff)
parent9f453592a40b6db4ab6715fe2f29ca573bb89148 (diff)
downloadNim-ac8274c60f860b334a08199c4c0a65620962ef2a.tar.gz
fix merge conflict
Diffstat (limited to 'lib/pure/strmisc.nim')
-rw-r--r--lib/pure/strmisc.nim29
1 files changed, 28 insertions, 1 deletions
diff --git a/lib/pure/strmisc.nim b/lib/pure/strmisc.nim
index d1ff920c9..31f1471ff 100644
--- a/lib/pure/strmisc.nim
+++ b/lib/pure/strmisc.nim
@@ -16,7 +16,20 @@ import strutils
 
 proc expandTabs*(s: string, tabSize: int = 8): string {.noSideEffect,
   procvar.} =
-  ## Expand tab characters in `s` by `tabSize` spaces
+  ## Expand tab characters in `s` replacing them by spaces.
+  ##
+  ## The amount of inserted spaces for each tab character is the difference
+  ## between the current column number and the next tab position. Tab positions
+  ## occur every `tabSize` characters.
+  ## The column number starts at 0 and is increased with every single character
+  ## and inserted space, except for newline, which resets the column number
+  ## back to 0.
+  runnableExamples:
+    doAssert expandTabs("\t", 4) == "    "
+    doAssert expandTabs("\tfoo\t", 4) == "    foo "
+    doAssert expandTabs("\tfoo\tbar", 4) == "    foo bar"
+    doAssert expandTabs("\tfoo\tbar\t", 4) == "    foo bar "
+    doAssert expandTabs("ab\tcd\n\txy\t", 3) == "ab cd\n   xy "
 
   result = newStringOfCap(s.len + s.len shr 2)
   var pos = 0
@@ -48,6 +61,13 @@ proc partition*(s: string, sep: string,
   ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
   ## (`s`, "", "") if `sep` is not found and `right` is false or
   ## ("", "", `s`) if `sep` is not found and `right` is true
+  runnableExamples:
+    doAssert partition("foo:bar", ":") == ("foo", ":", "bar")
+    doAssert partition("foobarbar", "bar") == ("foo", "bar", "bar")
+    doAssert partition("foobarbar", "bank") == ("foobarbar", "", "")
+    doAssert partition("foobarbar", "foo") == ("", "foo", "barbar")
+    doAssert partition("foofoobar", "bar") == ("foofoo", "bar", "")
+
   let position = if right: s.rfind(sep) else: s.find(sep)
   if position != -1:
     return (s[0 ..< position], sep, s[position + sep.len ..< s.len])
@@ -59,6 +79,13 @@ proc rpartition*(s: string, sep: string): (string, string, string)
   ##
   ## Returns a 3 string tuple of (beforeSep, `sep`, afterSep) or
   ## ("", "", `s`) if `sep` is not found
+  runnableExamples:
+    doAssert rpartition("foo:bar", ":") == ("foo", ":", "bar")
+    doAssert rpartition("foobarbar", "bar") == ("foobar", "bar", "")
+    doAssert rpartition("foobarbar", "bank") == ("", "", "foobarbar")
+    doAssert rpartition("foobarbar", "foo") == ("", "foo", "barbar")
+    doAssert rpartition("foofoobar", "bar") == ("foofoo", "bar", "")
+
   return partition(s, sep, right = true)
 
 when isMainModule: