From fd102f39bb8c969d33015654422ff4541f211b51 Mon Sep 17 00:00:00 2001 From: skilchen Date: Tue, 5 Jun 2018 00:26:16 +0200 Subject: 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 --- lib/pure/strformat.nim | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib') 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: -- cgit 1.4.1-2-gfad0