fixed float formatting for js

This commit is contained in:
Yuriy Glukhov 2015-11-26 01:10:52 +02:00
commit 2fcc37fd2b
2 changed files with 42 additions and 24 deletions

View file

@ -1344,8 +1344,8 @@ proc editDistance*(a, b: string): int {.noSideEffect,
# floating point formating: # floating point formating:
when not defined(js):
proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>", proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
importc: "sprintf", varargs, noSideEffect.} importc: "sprintf", varargs, noSideEffect.}
type type
@ -1370,29 +1370,44 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
## after the decimal point for Nim's ``biggestFloat`` type. ## after the decimal point for Nim's ``biggestFloat`` type.
## ##
## If ``precision == 0``, it tries to format it nicely. ## If ``precision == 0``, it tries to format it nicely.
const floatFormatToChar: array[FloatFormatMode, char] = ['g', 'f', 'e'] when defined(js):
var var res: cstring
frmtstr {.noinit.}: array[0..5, char] case format
buf {.noinit.}: array[0..2500, char] of ffDefault:
L: cint {.emit: "`res` = `f`.toString();".}
frmtstr[0] = '%' of ffDecimal:
if precision > 0: {.emit: "`res` = `f`.toFixed(`precision`);".}
frmtstr[1] = '#' of ffScientific:
frmtstr[2] = '.' {.emit: "`res` = `f`.toExponential(`precision`);".}
frmtstr[3] = '*' result = $res
frmtstr[4] = floatFormatToChar[format] for i in 0 ..< result.len:
frmtstr[5] = '\0' # Depending on the locale either dot or comma is produced,
L = c_sprintf(buf, frmtstr, precision, f) # but nothing else is possible:
if result[i] in {'.', ','}: result[i] = decimalsep
else: else:
frmtstr[1] = floatFormatToChar[format] const floatFormatToChar: array[FloatFormatMode, char] = ['g', 'f', 'e']
frmtstr[2] = '\0' var
L = c_sprintf(buf, frmtstr, f) frmtstr {.noinit.}: array[0..5, char]
result = newString(L) buf {.noinit.}: array[0..2500, char]
for i in 0 ..< L: L: cint
# Depending on the locale either dot or comma is produced, frmtstr[0] = '%'
# but nothing else is possible: if precision > 0:
if buf[i] in {'.', ','}: result[i] = decimalsep frmtstr[1] = '#'
else: result[i] = buf[i] frmtstr[2] = '.'
frmtstr[3] = '*'
frmtstr[4] = floatFormatToChar[format]
frmtstr[5] = '\0'
L = c_sprintf(buf, frmtstr, precision, f)
else:
frmtstr[1] = floatFormatToChar[format]
frmtstr[2] = '\0'
L = c_sprintf(buf, frmtstr, f)
result = newString(L)
for i in 0 ..< L:
# Depending on the locale either dot or comma is produced,
# but nothing else is possible:
if buf[i] in {'.', ','}: result[i] = decimalsep
else: result[i] = buf[i]
proc formatFloat*(f: float, format: FloatFormatMode = ffDefault, proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
precision: range[0..32] = 16; decimalSep = '.'): string {. precision: range[0..32] = 16; decimalSep = '.'): string {.

View file

@ -223,6 +223,9 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
"varres/tvartup"]: "varres/tvartup"]:
test "tests/" & testfile & ".nim" test "tests/" & testfile & ".nim"
for testfile in ["pure/strutils"]:
test "lib/" & testfile & ".nim"
# ------------------------- manyloc ------------------------------------------- # ------------------------- manyloc -------------------------------------------
#proc runSpecialTests(r: var TResults, options: string) = #proc runSpecialTests(r: var TResults, options: string) =
# for t in ["lib/packages/docutils/highlite"]: # for t in ["lib/packages/docutils/highlite"]: