-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 ]
{ "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
! 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
: 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
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;
}
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));