mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-03-14 11:10:01 +01:00
use scad to reduce deps
This commit is contained in:
parent
a5772f7ee6
commit
67a5465e09
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
|
||||
*
|
||||
**/
|
||||
|
||||
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]);
|
||||
use <_impl/_parse_number_impl.scad>;
|
||||
|
||||
function parse_number(t) =
|
||||
t[0] == "-" ? -_parse_positive_number(sub_str(t, 1, len(t))) : _parse_positive_number(t);
|
||||
|
||||
function parse_number(t) = _parse_number_impl(t);
|
@ -1,7 +1,5 @@
|
||||
include <unittest.scad>;
|
||||
include <util/sub_str.scad>;
|
||||
include <util/split_str.scad>;
|
||||
include <util/parse_number.scad>;
|
||||
use <unittest.scad>;
|
||||
use <util/parse_number.scad>;
|
||||
|
||||
module test_parse_number() {
|
||||
echo("==== test_parse_number ====");
|
||||
|
Loading…
x
Reference in New Issue
Block a user