mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-21 23:05:27 +00:00
Version of trimZeros without temp strings (#12633)
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user