]> gitweb.factorcode.org Git - factor.git/commitdiff
math.parser: (format-float) doesn't need to zero-terminate returned byte-array
authorBjörn Lindqvist <bjourne@gmail.com>
Sat, 8 Oct 2016 09:31:08 +0000 (11:31 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Sat, 8 Oct 2016 22:47:37 +0000 (00:47 +0200)
core/math/parser/parser-tests.factor
core/math/parser/parser.factor
vm/math.cpp

index 5dca74660cb78c9a10422b1a9da4fd4381012e4e..4b8bbd1e358f0e23227769f00347a9ff6d31629b 100644 (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
index 63c09ba8d39cb3d56cc39001278da50b2d9db3a6..5169100680f1085b8da54383f4b29a5a92f1f418 100644 (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
index 29daa91e1b555cea212784532ebeea23ff89af0e..98be48275bfcc32fb609c89ea98aa16b9da6a7c8 100644 (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));