summary refs log tree commit diff stats
path: root/lib/pure/strutils.nim
diff options
context:
space:
mode:
authorAndrey Sobolev <andrey.sobolev@xored.com>2015-09-14 11:50:39 +0600
committerAndrey Sobolev <andrey.sobolev@xored.com>2015-09-14 11:50:39 +0600
commit2f6dc8c47f836b66c1a28fd0b90a9e591b6c9be5 (patch)
tree0676259b78fba9788d6dffd441de1d7f5b0df140 /lib/pure/strutils.nim
parenta1aa7da37612eac6f961f0daaf5454f936b4e295 (diff)
parent148bbee05f31802666dbf1730e0df328ff57a384 (diff)
downloadNim-2f6dc8c47f836b66c1a28fd0b90a9e591b6c9be5.tar.gz
Merge remote-tracking branch 'nim-lang/devel' into emscripten-support
Diffstat (limited to 'lib/pure/strutils.nim')
-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"