mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-06 23:06:43 +02:00
use scad to reduce deps
This commit is contained in:
18
src/util/_impl/_parse_number_impl.scad
Normal file
18
src/util/_impl/_parse_number_impl.scad
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
use <../sub_str.scad>;
|
||||||
|
use <../split_str.scad>;
|
||||||
|
|
||||||
|
function _str_to_int(t) = ord(t) - 48;
|
||||||
|
|
||||||
|
function _parse_positive_int(t, value = 0, i = 0) =
|
||||||
|
i == len(t) ? value : _parse_positive_int(t, value * pow(10, i) + _str_to_int(t[i]), i + 1);
|
||||||
|
|
||||||
|
function _parse_positive_decimal(t, value = 0, i = 0) =
|
||||||
|
i == len(t) ? value : _parse_positive_decimal(t, value + _str_to_int(t[i]) * pow(10, -(i + 1)), i + 1);
|
||||||
|
|
||||||
|
function _parse_positive_number(t) =
|
||||||
|
len(search(".", t)) == 0 ? _parse_positive_int(t) :
|
||||||
|
_parse_positive_int(split_str(t, ".")[0]) + _parse_positive_decimal(split_str(t, ".")[1]);
|
||||||
|
|
||||||
|
function _parse_number_impl(t) =
|
||||||
|
t[0] == "-" ? -_parse_positive_number(sub_str(t, 1, len(t))) : _parse_positive_number(t);
|
||||||
|
|
@@ -7,19 +7,7 @@
|
|||||||
* @see https://openhome.cc/eGossip/OpenSCAD/lib2-parse_number.html
|
* @see https://openhome.cc/eGossip/OpenSCAD/lib2-parse_number.html
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
|
|
||||||
function _str_to_int(t) = ord(t) - 48;
|
|
||||||
|
|
||||||
function _parse_positive_int(t, value = 0, i = 0) =
|
|
||||||
i == len(t) ? value : _parse_positive_int(t, value * pow(10, i) + _str_to_int(t[i]), i + 1);
|
|
||||||
|
|
||||||
function _parse_positive_decimal(t, value = 0, i = 0) =
|
use <_impl/_parse_number_impl.scad>;
|
||||||
i == len(t) ? value : _parse_positive_decimal(t, value + _str_to_int(t[i]) * pow(10, -(i + 1)), i + 1);
|
|
||||||
|
|
||||||
function _parse_positive_number(t) =
|
|
||||||
len(search(".", t)) == 0 ? _parse_positive_int(t) :
|
|
||||||
_parse_positive_int(split_str(t, ".")[0]) + _parse_positive_decimal(split_str(t, ".")[1]);
|
|
||||||
|
|
||||||
function parse_number(t) =
|
function parse_number(t) = _parse_number_impl(t);
|
||||||
t[0] == "-" ? -_parse_positive_number(sub_str(t, 1, len(t))) : _parse_positive_number(t);
|
|
||||||
|
|
@@ -1,7 +1,5 @@
|
|||||||
include <unittest.scad>;
|
use <unittest.scad>;
|
||||||
include <util/sub_str.scad>;
|
use <util/parse_number.scad>;
|
||||||
include <util/split_str.scad>;
|
|
||||||
include <util/parse_number.scad>;
|
|
||||||
|
|
||||||
module test_parse_number() {
|
module test_parse_number() {
|
||||||
echo("==== test_parse_number ====");
|
echo("==== test_parse_number ====");
|
||||||
|
Reference in New Issue
Block a user