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
This commit is contained in:
skilchen 2018-06-05 00:26:16 +02:00 • committed by Varriount
commit fd102f39bb
2 changed files with 30 additions and 0 deletions

View file

@ -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: