summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2015-09-11 14:35:25 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2015-09-11 14:35:25 +0100
commit808253370bac20c96f5ea9fab9bfc33352011562 (patch)
treee7aa6313ce493bd9185af5c1467d144f11bd278a
parent28e3ad945a100fd0e669bcedb7d28ddd540b6978 (diff)
downloadNim-808253370bac20c96f5ea9fab9bfc33352011562.tar.gz
Implement strutils.indent
-rw-r--r--lib/pure/strutils.nim18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index ae3bd7f63..d1c09f43d 100644
--- a/lib/pure/strutils.nim
+++ b/lib/pure/strutils.nim
@@ -630,6 +630,22 @@ proc wordWrap*(s: string, maxLineWidth = 80,
       result.add(lastSep & word)
       lastSep.setLen(0)
 
+proc indent*(s: string, count: Natural, padding: string = " "): string
+    {.noSideEffect, rtl, extern: "nsuIndent".} =
+  ## Indents each line in ``s`` by ``count`` amount of ``padding``.
+  ##
+  ## **Note:** This currently does not preserve the specific new line characters
+  ## used.
+  result = ""
+  var i = 0
+  for line in s.splitLines():
+    if i != 0:
+      result.add("\n")
+    for j in 1..count:
+      result.add(padding)
+    result.add(line)
+    i.inc
+
 proc unindent*(s: string, eatAllIndent = false): string {.
                noSideEffect, rtl, extern: "nsuUnindent".} =
   ## Unindents `s`.
@@ -1502,3 +1518,5 @@ when isMainModule:
                  chars = {'s', 't', 'r', 'i', 'p', 'm', 'e'}) == " but don't strip this "
   doAssert strip("sfoofoofoos", leading = false, chars = {'s'}) == "sfoofoofoo"
   doAssert strip("sfoofoofoos", trailing = false, chars = {'s'}) == "foofoofoos"
+
+  doAssert "  foo\n  bar".indent(4, "Q") == "QQQQ  foo\nQQQQ  bar"