summary refs log tree commit diff stats
path: root/lib/pure/strformat.nim
diff options
context:
space:
mode:
authorskilchen <skilchen@users.noreply.github.com>2018-06-05 00:26:16 +0200
committerVarriount <Varriount@users.noreply.github.com>2018-06-04 18:26:16 -0400
commitfd102f39bb8c969d33015654422ff4541f211b51 (patch)
treed9993c8e87e943e50193d07f1340eb239249f4c0 /lib/pure/strformat.nim
parenta3e5242d31fa2ac86072534a7528468c7a6f257d (diff)
downloadNim-fd102f39bb8c969d33015654422ff4541f211b51.tar.gz
Fix strformat precision handling for strings (#7941)
* fix strformat precision handling for strings

* add some limited unicode awareness to the precision handling for strings

* improvement suggested by Varriount: use setLen and runeOffset instead of runeSubstr
Diffstat (limited to 'lib/pure/strformat.nim')
-rw-r--r--lib/pure/strformat.nim4
1 files changed, 4 insertions, 0 deletions
diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim
index 12a102c9f..3e7b043ce 100644
--- a/lib/pure/strformat.nim
+++ b/lib/pure/strformat.nim
@@ -558,12 +558,16 @@ proc format*(value: string; specifier: string; res: var string) =
   ## sense to call this directly, but it is required to exist
   ## by the ``&`` macro.
   let spec = parseStandardFormatSpecifier(specifier)
+  var value = value
   case spec.typ
   of 's', '\0': discard
   else:
     raise newException(ValueError,
       "invalid type in format string for string, expected 's', but got " &
       spec.typ)
+  if spec.precision != -1:
+    if spec.precision < runelen(value):
+      setLen(value, runeOffset(value, spec.precision))
   res.add alignString(value, spec.minimumWidth, spec.align, spec.fill)
 
 when isMainModule: