Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
6e448b6f1This file is pinned to the selected release. Download the original to open it in your design tool.
// String utility functions below each have a small comment describing their behavior and parameters.
// Join function: Joins elements of a list into a single string with an optional delimiter.
join = function (l,delimiter="")
let(s = len(l), d = delimiter,
jb = function (b,e) let(s = e-b, m = floor(b+s/2)) // join binary
s > 2 ? str(jb(b,m), jb(m,e)) : s == 2 ? str(l[b],l[b+1]) : l[b],
jd = function (b,e) let(s = e-b, m = floor(b+s/2)) // join delimiter
s > 2 ? str(jd(b,m), d, jd(m,e)) : s == 2 ? str(l[b],d,l[b+1]) : l[b])
s > 0 ? (d=="" ? jb(0,s) : jd(0,s)) : "";
// Substring function: Extracts a substring from a string `s` starting at index `b` and ending at index `e`.
substr = function(s,b,e) let(e=is_undef(e) || e > len(s) ? len(s) : e) (b==e) ? "" : join([for(i=[b:1:e-1]) s[i] ]);
// Split function: Splits a string `s` by a given separator into a list of substrings.
split = function(s,separator=" ") separator=="" ? [for(i=[0:1:len(s)-1]) s[i]] :
let(t=separator, e=len(s), f=len(t),
_s=function(b,c,d,r) b<e ?
(s[b]==t[c] ?
(c+1 == f ?
_s(b+1,0,b+1,concat(r,substr(s,d,b-c))) : // full separator match, concat substr to result
_s(b+1,c+1,d,r) ) : // separator match char, more to test
_s(b-c+1,0,d,r) ) : // separator mismatch
concat(r,substr(s,d,e))) // end of input string, return result
_s(0,0,0,[]);
// Fixed-point formatting function: Formats a number `x` to a fixed width `w` with `p` decimal places, padding with `sp`.
fixed = function(x,w,p,sp="0")
assert(len(sp)==1)
let(mult = pow(10,p), x2 = round(x*mult)/mult,
lz = function (x, l) l>1 && abs(x) < pow(10,l-1) ? str(sp, lz(x,l-1)) : "",
tz = function (x, t) let(mult=pow(10,t-1)) t>0 && abs(floor(x*mult)-x*mult) < 1e-9 ? str(sp, tz(x,t-1)) : ""
)
str(x2<0?"-":" ", lz(x2,w-p-2), abs(x2), abs(floor(x)-x2)<1e-9 ? "." : "" ,tz(x2,p));
// Floating-point string conversion function: Converts a string `s` to a floating-point number, handling various formats.
float = function(s) let(
_f = function(s, i, x, vM, dM, ddM, m)
i >= len(s) ? round(x*dM)/dM :
let(
d = ord(s[i])
)
(d == 32 && m == 0) || (d == 43 && (m == 0 || m == 2)) ?
_f(s, i+1, x, vM, dM, ddM, m) :
d == 45 && (m == 0 || m == 2) ?
_f(s, i+1, x, vM, -dM, ddM, floor(m/2)+1) :
d >= 48 && d <= 57 ?
_f(s, i+1, x*vM + (d-48)/dM, vM, dM*ddM, ddM, floor(m/2)+1) :
d == 46 && m<=1 ? _f(s, i+1, x, 1, 10*dM, 10, max(m, 1)) :
(d == 69 || d == 101) && m==1 ? let(
expon = _f(s, i+1, 0, 10, 1, 1, 2)
)
(is_undef(expon) ? undef : expon >= 0 ?
(round(x*dM)*(10^expon/dM)) :
(round(x*dM)/dM)/10^(-expon)) :
undef
)
_f(s, 0, 0, 10, 1, 1, 0);
// CSV parsing function: Parses a CSV-formatted string `s` and converts each value to a floating-point number.
csv_parse = function(s) [for (e=split(s, ",")) float(e)];