Version of trimZeros without temp strings (#12633)

This commit is contained in:
b3liever 2019-11-11 13:54:17 +02:00 • committed by Miran
commit 0c4d81241a
2 changed files with 47 additions and 13 deletions

View file

@ -2391,7 +2391,7 @@ proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
result = formatBiggestFloat(f, format, precision, decimalSep) 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 ## Trim trailing zeros from a formatted floating point
## value `x` (must be declared as ``var``). ## value `x` (must be declared as ``var``).
## ##
@ -2400,17 +2400,15 @@ proc trimZeros*(x: var string) {.noSideEffect.} =
var x = "123.456000000" var x = "123.456000000"
x.trimZeros() x.trimZeros()
doAssert x == "123.456" doAssert x == "123.456"
var spl: seq[string]
if x.contains('.') or x.contains(','): let sPos = find(x, decimalSep)
if x.contains('e'): if sPos >= 0:
spl = x.split('e') var last = find(x, 'e', start = sPos)
x = spl[0] last = if last >= 0: last - 1 else: high(x)
while x[x.high] == '0': var pos = last
x.setLen(x.len-1) while pos >= 0 and x[pos] == '0': dec(pos)
if x[x.high] in [',', '.']: if pos > sPos: inc(pos)
x.setLen(x.len-1) x.delete(pos, last)
if spl.len > 0:
x &= "e" & spl[1]
type type
BinaryPrefixMode* = enum ## the different names for binary prefixes 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 fbytes = bytes.float / (1'i64 shl (matchedIndex*10)).float
result = formatFloat(fbytes, format = ffDecimal, precision = 3, result = formatFloat(fbytes, format = ffDecimal, precision = 3,
decimalSep = decimalSep) decimalSep = decimalSep)
result.trimZeros() result.trimZeros(decimalSep)
if includeSpace: if includeSpace:
result &= " " result &= " "
result &= prefixes[matchedIndex] result &= prefixes[matchedIndex]

View file

@ -187,6 +187,41 @@ proc testRFind =
assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9 assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=5) == 9
assert "0123456789ABCDEFGAH".rfind({'0'..'9'}, start=10) == -1 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() = proc testSplitLines() =
let fixture = "a\nb\rc\r\nd" let fixture = "a\nb\rc\r\nd"
assert len(fixture.splitLines) == 4 assert len(fixture.splitLines) == 4
@ -246,6 +281,7 @@ proc testParseInts =
testDelete() testDelete()
testFind() testFind()
testRFind() testRFind()
testTrimZeros()
testSplitLines() testSplitLines()
testCountLines() testCountLines()
testParseInts() testParseInts()