Skip to content

Commit

Permalink
Version of trimZeros without temp strings (#12633)
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m authored and narimiran committed Nov 11, 2019
1 parent a4d43d7 commit 0c4d812
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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``).
##
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
36 changes: 36 additions & 0 deletions tests/stdlib/tstrutil.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -246,6 +281,7 @@ proc testParseInts =
testDelete()
testFind()
testRFind()
testTrimZeros()
testSplitLines()
testCountLines()
testParseInts()
Expand Down

0 comments on commit 0c4d812

Please sign in to comment.