math.parser: (format-float) doesn't need to zero-terminate returned byte-array

char-rename
Björn Lindqvist 2016-10-08 11:31:08 +02:00
parent 560900275a
commit bbd71ae975
3 changed files with 18 additions and 16 deletions

View File

@ -1,4 +1,5 @@
USING: layouts literals math math.parser sequences tools.test ;
USING: layouts literals math math.parser.private sequences tools.test ;
IN: math.parser
{ f }
[ f string>number ]
@ -461,3 +462,14 @@ unit-test
{ "deadbeef" } [ B{ 222 173 190 239 } bytes>hex-string ] unit-test
{ B{ 222 173 190 239 } } [ "deADbeEF" hex-string>bytes ] unit-test
{
B{ 49 46 53 53 69 43 48 53 }
} [
155000.0 B{ } -1 3 B{ 69 0 } B{ 67 0 } (format-float)
] unit-test
! Missing locale
{ "" } [
33.4 "" 4 4 "f" "missing" format-float
] unit-test

View File

@ -1,8 +1,7 @@
! Copyright (C) 2009 Joe Groff, 2013 John Benediktsson
! See http://factorcode.org/license.txt for BSD license.
USING: accessors byte-arrays combinators kernel kernel.private
layouts make math math.private sbufs sequences sequences.private
strings strings.private ;
USING: accessors byte-arrays combinators kernel kernel.private layouts
make math math.private sbufs sequences sequences.private strings ;
IN: math.parser
<PRIVATE
@ -542,18 +541,10 @@ M: ratio >base
: format-string ( format -- format )
0 suffix >byte-array ; foldable
: format-head ( byte-array n -- string )
swap over 0 <string> [
[
[ [ nth-unsafe ] 2keep drop ]
[ set-string-nth-fast ] bi*
] 2curry each-integer
] keep ; inline
: format-float ( n fill width precision format locale -- string )
[
[ format-string ] 4dip [ format-string ] bi@ (format-float)
dup [ 0 = ] find drop format-head
>string
] [
"C" = [ [ "G" = ] [ "E" = ] bi or CHAR: E CHAR: e ? fix-float ]
[ drop ] if

View File

@ -223,8 +223,7 @@ void factor_vm::primitive_format_float() {
try {
localized_stream.imbue(std::locale(locale));
} catch (const runtime_error&) {
byte_array* array = allot_byte_array(1);
array->data<char>()[0] = '\0';
byte_array* array = allot_byte_array(0);
ctx->replace(tag<byte_array>(array));
return;
}
@ -247,7 +246,7 @@ void factor_vm::primitive_format_float() {
localized_stream << value;
const std::string& tmp = localized_stream.str();
const char* cstr = tmp.c_str();
size_t size = tmp.length()+1;
size_t size = tmp.length();
byte_array* array = allot_byte_array(size);
memcpy(array->data<char>(), cstr, size);
ctx->replace(tag<byte_array>(array));