minor breacking change: in string formats '' the '1' is now interpreted as a number, not as an identifier. This is more consistent with the rest of the mini language and allows '12'.
This commit is contained in:
parent
125ccd303e
commit
9b1a23c554
1 changed files with 21 additions and 5 deletions
|
|
@ -2169,11 +2169,26 @@ proc addf*(s: var string, formatstr: string, a: varargs[string, `$`]) {.
|
||||||
if idx >% a.high: invalidFormatString()
|
if idx >% a.high: invalidFormatString()
|
||||||
add s, a[idx]
|
add s, a[idx]
|
||||||
of '{':
|
of '{':
|
||||||
var j = i+1
|
var j = i+2
|
||||||
while formatstr[j] notin {'\0', '}'}: inc(j)
|
var k = 0
|
||||||
var x = findNormalized(substr(formatstr, i+2, j-1), a)
|
var negative = formatstr[j] == '-'
|
||||||
if x >= 0 and x < high(a): add s, a[x+1]
|
if negative: inc j
|
||||||
else: invalidFormatString()
|
var isNumber = 0
|
||||||
|
while formatstr[j] notin {'\0', '}'}:
|
||||||
|
if formatstr[j] in Digits:
|
||||||
|
k = k * 10 + ord(formatstr[j]) - ord('0')
|
||||||
|
if isNumber == 0: isNumber = 1
|
||||||
|
else:
|
||||||
|
isNumber = -1
|
||||||
|
inc(j)
|
||||||
|
if isNumber == 1:
|
||||||
|
let idx = if not negative: k-1 else: a.len-k
|
||||||
|
if idx >% a.high: invalidFormatString()
|
||||||
|
add s, a[idx]
|
||||||
|
else:
|
||||||
|
var x = findNormalized(substr(formatstr, i+2, j-1), a)
|
||||||
|
if x >= 0 and x < high(a): add s, a[x+1]
|
||||||
|
else: invalidFormatString()
|
||||||
i = j+1
|
i = j+1
|
||||||
of 'a'..'z', 'A'..'Z', '\128'..'\255', '_':
|
of 'a'..'z', 'A'..'Z', '\128'..'\255', '_':
|
||||||
var j = i+1
|
var j = i+1
|
||||||
|
|
@ -2315,6 +2330,7 @@ when isMainModule:
|
||||||
["1,0e-11", "1,0e-011"]
|
["1,0e-11", "1,0e-011"]
|
||||||
|
|
||||||
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
||||||
|
doAssert "${1}12 ${-1}$2" % ["a", "b"] == "a12 bb"
|
||||||
|
|
||||||
block: # formatSize tests
|
block: # formatSize tests
|
||||||
doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB"
|
doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue