From 0c4d81241a168010ddaf53f5d651b649f7218240 Mon Sep 17 00:00:00 2001 From: b3liever <43617260+b3liever@users.noreply.github.com> Date: Mon, 11 Nov 2019 13:54:17 +0200 Subject: [PATCH] Version of trimZeros without temp strings (#12633) --- lib/pure/strutils.nim | 24 +++++++++++------------- tests/stdlib/tstrutil.nim | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 3d455fd30..72d9471d1 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -2391,7 +2391,7 @@ proc formatFloat*(f: float, format: FloatFormatMode = ffDefault, result = formatBiggestFloat(f, format, precision, decimalSep) -proc trimZeros*(x: var string) {.noSideEffect.} = +proc trimZeros*(x: var string; decimalSep = '.') {.noSideEffect.} = ## Trim trailing zeros from a formatted floating point ## value `x` (must be declared as ``var``). ## @@ -2400,17 +2400,15 @@ proc trimZeros*(x: var string) {.noSideEffect.} = var x = "123.456000000" x.trimZeros() doAssert x == "123.456" - var spl: seq[string] - if x.contains('.') or x.contains(','): - if x.contains('e'): - spl = x.split('e') - x = spl[0] - while x[x.high] == '0': - x.setLen(x.len-1) - if x[x.high] in [',', '.']: - x.setLen(x.len-1) - if spl.len > 0: - x &= "e" & spl[1] + + let sPos = find(x, decimalSep) + if sPos >= 0: + var last = find(x, 'e', start = sPos) + last = if last >= 0: last - 1 else: high(x) + var pos = last + while pos >= 0 and x[pos] == '0': dec(pos) + if pos > sPos: inc(pos) + x.delete(pos, last) type BinaryPrefixMode* = enum ## the different names for binary prefixes @@ -2467,7 +2465,7 @@ proc formatSize*(bytes: int64, fbytes = bytes.float / (1'i64 shl (matchedIndex*10)).float result = formatFloat(fbytes, format = ffDecimal, precision = 3, decimalSep = decimalSep) - result.trimZeros() + result.trimZeros(decimalSep) if includeSpace: result &= " " result &= prefixes[matchedIndex] diff --git a/tests/stdlib/tstrutil.nim b/tests/stdlib/tstrutil.nim index 0ad0cbd46..9dbc98912 100644 --- a/tests/stdlib/tstrutil.nim +++ b/tests/stdlib/tstrutil.nim @@ -187,6 +187,41 @@ proc testRFind = assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9 assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=10) == -1 +proc testTrimZeros() = + var x = "1200" + x.trimZeros() + assert x == "1200" + x = "120.0" + x.trimZeros() + assert x == "120" + x = "0." + x.trimZeros() + assert x == "0" + x = "1.0e2" + x.trimZeros() + assert x == "1e2" + x = "78.90" + x.trimZeros() + assert x == "78.9" + x = "1.23e4" + x.trimZeros() + assert x == "1.23e4" + x = "1.01" + x.trimZeros() + assert x == "1.01" + x = "1.1001" + x.trimZeros() + assert x == "1.1001" + x = "0.0" + x.trimZeros() + assert x == "0" + x = "0.01" + x.trimZeros() + assert x == "0.01" + x = "1e0" + x.trimZeros() + assert x == "1e0" + proc testSplitLines() = let fixture = "a\nb\rc\r\nd" assert len(fixture.splitLines) == 4 @@ -246,6 +281,7 @@ proc testParseInts = testDelete() testFind() testRFind() +testTrimZeros() testSplitLines() testCountLines() testParseInts()