mirror of
https://github.com/revarbat/BOSL2.git
synced 2025-08-30 08:30:51 +02:00
Standardize indention on spaces, not tabs.
This commit is contained in:
430
strings.scad
430
strings.scad
@@ -27,13 +27,13 @@
|
||||
// substr("abcdefg",[2,4]); // Returns "cde"
|
||||
// substr("abcdefg",len=-2); // Returns ""
|
||||
function substr(str, pos=0, len=undef) =
|
||||
is_list(pos) ? _substr(str, pos[0], pos[1]-pos[0]+1) :
|
||||
len == undef ? _substr(str, pos, len(str)-pos) :
|
||||
_substr(str,pos,len);
|
||||
is_list(pos) ? _substr(str, pos[0], pos[1]-pos[0]+1) :
|
||||
len == undef ? _substr(str, pos, len(str)-pos) :
|
||||
_substr(str,pos,len);
|
||||
|
||||
function _substr(str,pos,len,substr="") =
|
||||
len <= 0 || pos>=len(str) ? substr :
|
||||
_substr(str, pos+1, len-1, str(substr, str[pos]));
|
||||
len <= 0 || pos>=len(str) ? substr :
|
||||
_substr(str, pos+1, len-1, str(substr, str[pos]));
|
||||
|
||||
|
||||
// Function suffix()
|
||||
@@ -57,8 +57,8 @@ function suffix(str,len) = substr(str, len(str)-len,len);
|
||||
// str_join(["abc","def","ghi"]); // Returns "abcdefghi"
|
||||
// str_join(["abc","def","ghi"], " + "); // Returns "abc + def + ghi"
|
||||
function str_join(list,sep="",_i=0, _result="") =
|
||||
_i >= len(list)-1 ? (_i==len(list) ? _result : str(_result,list[_i])) :
|
||||
str_join(list,sep,_i+1,str(_result,list[_i],sep));
|
||||
_i >= len(list)-1 ? (_i==len(list) ? _result : str(_result,list[_i])) :
|
||||
str_join(list,sep,_i+1,str(_result,list[_i],sep));
|
||||
|
||||
|
||||
// Function: downcase()
|
||||
@@ -72,7 +72,7 @@ function str_join(list,sep="",_i=0, _result="") =
|
||||
// Example:
|
||||
// downcase("ABCdef"); // Returns "abcdef"
|
||||
function downcase(str) =
|
||||
str_join([for(char=str) let(code=ord(char)) code>=65 && code<=90 ? chr(code+32) : char]);
|
||||
str_join([for(char=str) let(code=ord(char)) code>=65 && code<=90 ? chr(code+32) : char]);
|
||||
|
||||
|
||||
// Function: upcase()
|
||||
@@ -86,7 +86,7 @@ function downcase(str) =
|
||||
// Example:
|
||||
// upcase("ABCdef"); // Returns "ABCDEF"
|
||||
function upcase(str) =
|
||||
str_join([for(char=str) let(code=ord(char)) code>=97 && code<=122 ? chr(code-32) : char]);
|
||||
str_join([for(char=str) let(code=ord(char)) code>=97 && code<=122 ? chr(code-32) : char]);
|
||||
|
||||
|
||||
// Function: str_int()
|
||||
@@ -110,19 +110,19 @@ function upcase(str) =
|
||||
// str_int("CEDE", 16); // Returns 52958
|
||||
// str_int(""); // Returns 0
|
||||
function str_int(str,base=10) =
|
||||
str==undef ? undef :
|
||||
len(str)==0 ? 0 :
|
||||
let(str=downcase(str))
|
||||
str[0] == "-" ? -_str_int_recurse(substr(str,1),base,len(str)-2) :
|
||||
str[0] == "+" ? _str_int_recurse(substr(str,1),base,len(str)-2) :
|
||||
_str_int_recurse(str,base,len(str)-1);
|
||||
str==undef ? undef :
|
||||
len(str)==0 ? 0 :
|
||||
let(str=downcase(str))
|
||||
str[0] == "-" ? -_str_int_recurse(substr(str,1),base,len(str)-2) :
|
||||
str[0] == "+" ? _str_int_recurse(substr(str,1),base,len(str)-2) :
|
||||
_str_int_recurse(str,base,len(str)-1);
|
||||
|
||||
function _str_int_recurse(str,base,i) =
|
||||
let(
|
||||
digit = search(str[i],"0123456789abcdef"),
|
||||
last_digit = digit == [] || digit[0] >= base ? (0/0) : digit[0]
|
||||
) i==0 ? last_digit :
|
||||
_str_int_recurse(str,base,i-1)*base + last_digit;
|
||||
let(
|
||||
digit = search(str[i],"0123456789abcdef"),
|
||||
last_digit = digit == [] || digit[0] >= base ? (0/0) : digit[0]
|
||||
) i==0 ? last_digit :
|
||||
_str_int_recurse(str,base,i-1)*base + last_digit;
|
||||
|
||||
|
||||
// Function: str_float()
|
||||
@@ -142,15 +142,15 @@ function _str_int_recurse(str,base,i) =
|
||||
// str_float("7.342e-4"); // Returns 0.0007342
|
||||
// str_float(""); // Returns 0
|
||||
function str_float(str) =
|
||||
str==undef ? undef :
|
||||
len(str) == 0 ? 0 :
|
||||
in_list(str[1], ["+","-"]) ? (0/0) : // Don't allow --3, or +-3
|
||||
str[0]=="-" ? -str_float(substr(str,1)) :
|
||||
str[0]=="+" ? str_float(substr(str,1)) :
|
||||
let(esplit = str_split(str,"eE") )
|
||||
len(esplit)==2 ? str_float(esplit[0]) * pow(10,str_int(esplit[1])) :
|
||||
let( dsplit = str_split(str,["."]))
|
||||
str_int(dsplit[0])+str_int(dsplit[1])/pow(10,len(dsplit[1]));
|
||||
str==undef ? undef :
|
||||
len(str) == 0 ? 0 :
|
||||
in_list(str[1], ["+","-"]) ? (0/0) : // Don't allow --3, or +-3
|
||||
str[0]=="-" ? -str_float(substr(str,1)) :
|
||||
str[0]=="+" ? str_float(substr(str,1)) :
|
||||
let(esplit = str_split(str,"eE") )
|
||||
len(esplit)==2 ? str_float(esplit[0]) * pow(10,str_int(esplit[1])) :
|
||||
let( dsplit = str_split(str,["."]))
|
||||
str_int(dsplit[0])+str_int(dsplit[1])/pow(10,len(dsplit[1]));
|
||||
|
||||
|
||||
// Function: str_frac()
|
||||
@@ -186,24 +186,24 @@ function str_float(str) =
|
||||
// str_frac("-2 12/4",mixed=false); // Returns nan
|
||||
// str_frac("2 1/4",mixed=false); // Returns nan
|
||||
function str_frac(str,mixed=true,improper=true,signed=true) =
|
||||
str == undef ? undef :
|
||||
len(str)==0 ? 0 :
|
||||
signed && str[0]=="-" ? -str_frac(substr(str,1),mixed=mixed,improper=improper,signed=false) :
|
||||
signed && str[0]=="+" ? str_frac(substr(str,1),mixed=mixed,improper=improper,signed=false) :
|
||||
mixed ? (
|
||||
str_find(str," ")>0 || is_undef(str_find(str,"/"))? (
|
||||
let(whole = str_split(str,[" "]))
|
||||
_str_int_recurse(whole[0],10,len(whole[0])-1) + str_frac(whole[1], mixed=false, improper=improper, signed=false)
|
||||
) : str_frac(str,mixed=false, improper=improper)
|
||||
) : (
|
||||
let(split = str_split(str,"/"))
|
||||
len(split)!=2 ? (0/0) :
|
||||
let(
|
||||
numerator = _str_int_recurse(split[0],10,len(split[0])-1),
|
||||
denominator = _str_int_recurse(split[1],10,len(split[1])-1)
|
||||
) !improper && numerator>=denominator? (0/0) :
|
||||
denominator<0 ? (0/0) : numerator/denominator
|
||||
);
|
||||
str == undef ? undef :
|
||||
len(str)==0 ? 0 :
|
||||
signed && str[0]=="-" ? -str_frac(substr(str,1),mixed=mixed,improper=improper,signed=false) :
|
||||
signed && str[0]=="+" ? str_frac(substr(str,1),mixed=mixed,improper=improper,signed=false) :
|
||||
mixed ? (
|
||||
str_find(str," ")>0 || is_undef(str_find(str,"/"))? (
|
||||
let(whole = str_split(str,[" "]))
|
||||
_str_int_recurse(whole[0],10,len(whole[0])-1) + str_frac(whole[1], mixed=false, improper=improper, signed=false)
|
||||
) : str_frac(str,mixed=false, improper=improper)
|
||||
) : (
|
||||
let(split = str_split(str,"/"))
|
||||
len(split)!=2 ? (0/0) :
|
||||
let(
|
||||
numerator = _str_int_recurse(split[0],10,len(split[0])-1),
|
||||
denominator = _str_int_recurse(split[1],10,len(split[1])-1)
|
||||
) !improper && numerator>=denominator? (0/0) :
|
||||
denominator<0 ? (0/0) : numerator/denominator
|
||||
);
|
||||
|
||||
|
||||
// Function: str_num()
|
||||
@@ -216,10 +216,10 @@ function str_frac(str,mixed=true,improper=true,signed=true) =
|
||||
// str_num("3/4"); // Returns 0.75
|
||||
// str_num("3.4e-2"); // Returns 0.034
|
||||
function str_num(str) =
|
||||
str == undef ? undef :
|
||||
let( val = str_frac(str) )
|
||||
val == val ? val :
|
||||
str_float(str);
|
||||
str == undef ? undef :
|
||||
let( val = str_frac(str) )
|
||||
val == val ? val :
|
||||
str_float(str);
|
||||
|
||||
|
||||
// Function: str_split()
|
||||
@@ -247,25 +247,25 @@ function str_num(str) =
|
||||
// str_split("abc+def-qrs*iop",["+","-","*"]); // Returns ["abc", "def", "qrs", "iop"]
|
||||
// str_split("abc+def-qrs*iop",["-","+","*"]); // Returns ["abc+def", "qrs*iop", "", ""]
|
||||
function str_split(str,sep,keep_nulls=true) =
|
||||
!keep_nulls ? _remove_empty_strs(str_split(str,sep,keep_nulls=true)) :
|
||||
is_list(sep) ? _str_split_recurse(str,sep,i=0,result=[]) :
|
||||
let( cutpts = concat([-1],sort(flatten(search(sep, str,0))),[len(str)]))
|
||||
[for(i=[0:len(cutpts)-2]) substr(str,cutpts[i]+1,cutpts[i+1]-cutpts[i]-1)];
|
||||
!keep_nulls ? _remove_empty_strs(str_split(str,sep,keep_nulls=true)) :
|
||||
is_list(sep) ? _str_split_recurse(str,sep,i=0,result=[]) :
|
||||
let( cutpts = concat([-1],sort(flatten(search(sep, str,0))),[len(str)]))
|
||||
[for(i=[0:len(cutpts)-2]) substr(str,cutpts[i]+1,cutpts[i+1]-cutpts[i]-1)];
|
||||
|
||||
function _str_split_recurse(str,sep,i,result) =
|
||||
i == len(sep) ? concat(result,[str]) :
|
||||
let(
|
||||
pos = search(sep[i], str),
|
||||
end = pos==[] ? len(str) : pos[0]
|
||||
)
|
||||
_str_split_recurse(
|
||||
substr(str,end+1),
|
||||
sep, i+1,
|
||||
concat(result, [substr(str,0,end)])
|
||||
);
|
||||
i == len(sep) ? concat(result,[str]) :
|
||||
let(
|
||||
pos = search(sep[i], str),
|
||||
end = pos==[] ? len(str) : pos[0]
|
||||
)
|
||||
_str_split_recurse(
|
||||
substr(str,end+1),
|
||||
sep, i+1,
|
||||
concat(result, [substr(str,0,end)])
|
||||
);
|
||||
|
||||
function _remove_empty_strs(list) =
|
||||
list_remove(list, search([""], list,0)[0]);
|
||||
list_remove(list, search([""], list,0)[0]);
|
||||
|
||||
|
||||
// _str_cmp(str,sindex,pattern)
|
||||
@@ -276,11 +276,11 @@ function _remove_empty_strs(list) =
|
||||
// cuts run time in half when the string is long. Two other string
|
||||
// comparison methods were slower.
|
||||
function _str_cmp(str,sindex,pattern) =
|
||||
len(str)-sindex <len(pattern)? false :
|
||||
_str_cmp_recurse(str,sindex,pattern,len(pattern));
|
||||
len(str)-sindex <len(pattern)? false :
|
||||
_str_cmp_recurse(str,sindex,pattern,len(pattern));
|
||||
|
||||
function _str_cmp_recurse(str,sindex,pattern,plen,pindex=0,) =
|
||||
pindex < plen && pattern[pindex]==str[sindex] ? _str_cmp_recurse(str,sindex+1,pattern,plen,pindex+1): (pindex==plen);
|
||||
pindex < plen && pattern[pindex]==str[sindex] ? _str_cmp_recurse(str,sindex+1,pattern,plen,pindex+1): (pindex==plen);
|
||||
|
||||
|
||||
// Function: str_find()
|
||||
@@ -315,25 +315,25 @@ function _str_cmp_recurse(str,sindex,pattern,plen,pindex=0,) =
|
||||
// str_find("abc123def123abc","1234",all=true); // Returns []
|
||||
// str_find("abc","",all=true); // Returns [0,1,2]
|
||||
function str_find(str,pattern,start=undef,last=false,all=false) =
|
||||
all? _str_find_all(str,pattern) :
|
||||
let( start = first_defined([start,last?len(str)-len(pattern):0]) )
|
||||
pattern==""? start :
|
||||
last? _str_find_last(str,pattern,start) :
|
||||
_str_find_first(str,pattern,len(str)-len(pattern),start);
|
||||
all? _str_find_all(str,pattern) :
|
||||
let( start = first_defined([start,last?len(str)-len(pattern):0]) )
|
||||
pattern==""? start :
|
||||
last? _str_find_last(str,pattern,start) :
|
||||
_str_find_first(str,pattern,len(str)-len(pattern),start);
|
||||
|
||||
function _str_find_first(str,pattern,max_sindex,sindex) =
|
||||
sindex<=max_sindex && !_str_cmp(str,sindex, pattern)?
|
||||
_str_find_first(str,pattern,max_sindex,sindex+1) :
|
||||
(sindex <= max_sindex ? sindex : undef);
|
||||
sindex<=max_sindex && !_str_cmp(str,sindex, pattern)?
|
||||
_str_find_first(str,pattern,max_sindex,sindex+1) :
|
||||
(sindex <= max_sindex ? sindex : undef);
|
||||
|
||||
function _str_find_last(str,pattern,sindex) =
|
||||
sindex>=0 && !_str_cmp(str,sindex, pattern)?
|
||||
_str_find_last(str,pattern,sindex-1) :
|
||||
(sindex >=0 ? sindex : undef);
|
||||
sindex>=0 && !_str_cmp(str,sindex, pattern)?
|
||||
_str_find_last(str,pattern,sindex-1) :
|
||||
(sindex >=0 ? sindex : undef);
|
||||
|
||||
function _str_find_all(str,pattern) =
|
||||
pattern == "" ? list_range(len(str)) :
|
||||
[for(i=[0:1:len(str)-len(pattern)]) if (_str_cmp(str,i,pattern)) i];
|
||||
pattern == "" ? list_range(len(str)) :
|
||||
[for(i=[0:1:len(str)-len(pattern)]) if (_str_cmp(str,i,pattern)) i];
|
||||
|
||||
|
||||
// Function: starts_with()
|
||||
@@ -369,12 +369,12 @@ function ends_with(str,pattern) = _str_cmp(str,len(str)-len(pattern),pattern);
|
||||
|
||||
|
||||
function _str_count_leading(s,c,_i=0) =
|
||||
(_i>=len(s)||!in_list(s[_i],[each c]))? _i :
|
||||
_str_count_leading(s,c,_i=_i+1);
|
||||
(_i>=len(s)||!in_list(s[_i],[each c]))? _i :
|
||||
_str_count_leading(s,c,_i=_i+1);
|
||||
|
||||
function _str_count_trailing(s,c,_i=0) =
|
||||
(_i>=len(s)||!in_list(s[len(s)-1-_i],[each c]))? _i :
|
||||
_str_count_trailing(s,c,_i=_i+1);
|
||||
(_i>=len(s)||!in_list(s[len(s)-1-_i],[each c]))? _i :
|
||||
_str_count_trailing(s,c,_i=_i+1);
|
||||
|
||||
|
||||
// Function: str_strip_leading()
|
||||
@@ -435,15 +435,15 @@ function str_strip(s,c) = str_strip_trailing(str_strip_leading(s,c),c);
|
||||
// fmt_int(123456789012345); // Returns "123456789012345"
|
||||
// fmt_int(-123456789012345); // Returns "-123456789012345"
|
||||
function fmt_int(i,mindigits=1) =
|
||||
i<0? str("-", fmt_int(-i)) :
|
||||
let(i=floor(i), e=floor(log(i)))
|
||||
i==0? "0" :
|
||||
str_join(
|
||||
concat(
|
||||
[for (j=[0:1:mindigits-e-2]) "0"],
|
||||
[for (j=[e:-1:0]) str(floor(i/pow(10,j)%10))]
|
||||
)
|
||||
);
|
||||
i<0? str("-", fmt_int(-i)) :
|
||||
let(i=floor(i), e=floor(log(i)))
|
||||
i==0? "0" :
|
||||
str_join(
|
||||
concat(
|
||||
[for (j=[0:1:mindigits-e-2]) "0"],
|
||||
[for (j=[e:-1:0]) str(floor(i/pow(10,j)%10))]
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// Function: fmt_fixed()
|
||||
@@ -455,19 +455,19 @@ function fmt_int(i,mindigits=1) =
|
||||
// f = The floating point number to format.
|
||||
// digits = The number of digits after the decimal to show. Default: 6
|
||||
function fmt_fixed(f,digits=6) =
|
||||
assert(is_int(digits))
|
||||
assert(digits>0)
|
||||
is_list(f)? str("[",str_join(sep=", ", [for (g=f) fmt_fixed(g,digits=digits)]),"]") :
|
||||
str(f)=="nan"? "nan" :
|
||||
str(f)=="inf"? "inf" :
|
||||
f<0? str("-",fmt_fixed(-f,digits=digits)) :
|
||||
assert(is_num(f))
|
||||
let(
|
||||
sc = pow(10,digits),
|
||||
scaled = floor(f * sc + 0.5),
|
||||
whole = floor(scaled/sc),
|
||||
part = floor(scaled-(whole*sc))
|
||||
) str(fmt_int(whole),".",fmt_int(part,digits));
|
||||
assert(is_int(digits))
|
||||
assert(digits>0)
|
||||
is_list(f)? str("[",str_join(sep=", ", [for (g=f) fmt_fixed(g,digits=digits)]),"]") :
|
||||
str(f)=="nan"? "nan" :
|
||||
str(f)=="inf"? "inf" :
|
||||
f<0? str("-",fmt_fixed(-f,digits=digits)) :
|
||||
assert(is_num(f))
|
||||
let(
|
||||
sc = pow(10,digits),
|
||||
scaled = floor(f * sc + 0.5),
|
||||
whole = floor(scaled/sc),
|
||||
part = floor(scaled-(whole*sc))
|
||||
) str(fmt_int(whole),".",fmt_int(part,digits));
|
||||
|
||||
|
||||
// Function: fmt_float()
|
||||
@@ -485,34 +485,34 @@ function fmt_fixed(f,digits=6) =
|
||||
// fmt_float(PI,12); // Returns: "3.14159265359"
|
||||
// fmt_float([PI,-16.75],12); // Returns: "[3.14159265359, -16.75]"
|
||||
function fmt_float(f,sig=12) =
|
||||
assert(is_int(sig))
|
||||
assert(sig>0)
|
||||
is_list(f)? str("[",str_join(sep=", ", [for (g=f) fmt_float(g,sig=sig)]),"]") :
|
||||
f==0? "0" :
|
||||
str(f)=="nan"? "nan" :
|
||||
str(f)=="inf"? "inf" :
|
||||
f<0? str("-",fmt_float(-f,sig=sig)) :
|
||||
assert(is_num(f))
|
||||
let(
|
||||
e = floor(log(f)),
|
||||
mv = sig - e - 1
|
||||
) mv == 0? fmt_int(floor(f + 0.5)) :
|
||||
(e<-sig/2||mv<0)? str(fmt_float(f*pow(10,-e),sig=sig),"e",e) :
|
||||
let(
|
||||
ff = f + pow(10,-mv)*0.5,
|
||||
whole = floor(ff),
|
||||
part = floor((ff-whole) * pow(10,mv))
|
||||
)
|
||||
str_join([
|
||||
str(whole),
|
||||
str_strip_trailing(
|
||||
str_join([
|
||||
".",
|
||||
fmt_int(part, mindigits=mv)
|
||||
]),
|
||||
"0."
|
||||
)
|
||||
]);
|
||||
assert(is_int(sig))
|
||||
assert(sig>0)
|
||||
is_list(f)? str("[",str_join(sep=", ", [for (g=f) fmt_float(g,sig=sig)]),"]") :
|
||||
f==0? "0" :
|
||||
str(f)=="nan"? "nan" :
|
||||
str(f)=="inf"? "inf" :
|
||||
f<0? str("-",fmt_float(-f,sig=sig)) :
|
||||
assert(is_num(f))
|
||||
let(
|
||||
e = floor(log(f)),
|
||||
mv = sig - e - 1
|
||||
) mv == 0? fmt_int(floor(f + 0.5)) :
|
||||
(e<-sig/2||mv<0)? str(fmt_float(f*pow(10,-e),sig=sig),"e",e) :
|
||||
let(
|
||||
ff = f + pow(10,-mv)*0.5,
|
||||
whole = floor(ff),
|
||||
part = floor((ff-whole) * pow(10,mv))
|
||||
)
|
||||
str_join([
|
||||
str(whole),
|
||||
str_strip_trailing(
|
||||
str_join([
|
||||
".",
|
||||
fmt_int(part, mindigits=mv)
|
||||
]),
|
||||
"0."
|
||||
)
|
||||
]);
|
||||
|
||||
|
||||
// Function: escape_html()
|
||||
@@ -521,14 +521,14 @@ function fmt_float(f,sig=12) =
|
||||
// Description:
|
||||
// Converts "<", ">", "&", and double-quote chars to their entity encoding so that echoing the strong will show it verbatim.
|
||||
function escape_html(s) =
|
||||
str_join([
|
||||
for (c=s)
|
||||
c=="<"? "<" :
|
||||
c==">"? ">" :
|
||||
c=="&"? "&" :
|
||||
c=="\""? """ :
|
||||
c
|
||||
]);
|
||||
str_join([
|
||||
for (c=s)
|
||||
c=="<"? "<" :
|
||||
c==">"? ">" :
|
||||
c=="&"? "&" :
|
||||
c=="\""? """ :
|
||||
c
|
||||
]);
|
||||
|
||||
|
||||
// Function: is_lower()
|
||||
@@ -537,10 +537,10 @@ function escape_html(s) =
|
||||
// Description:
|
||||
// Returns true if all the characters in the given string are lowercase letters. (a-z)
|
||||
function is_lower(s) =
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_lower(v)]) :
|
||||
let(v = ord(s[0])) (v>=ord("a") && v<=ord("z"));
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_lower(v)]) :
|
||||
let(v = ord(s[0])) (v>=ord("a") && v<=ord("z"));
|
||||
|
||||
|
||||
// Function: is_upper()
|
||||
@@ -549,10 +549,10 @@ function is_lower(s) =
|
||||
// Description:
|
||||
// Returns true if all the characters in the given string are uppercase letters. (A-Z)
|
||||
function is_upper(s) =
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_upper(v)]) :
|
||||
let(v = ord(s[0])) (v>=ord("A") && v<=ord("Z"));
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_upper(v)]) :
|
||||
let(v = ord(s[0])) (v>=ord("A") && v<=ord("Z"));
|
||||
|
||||
|
||||
// Function: is_digit()
|
||||
@@ -561,10 +561,10 @@ function is_upper(s) =
|
||||
// Description:
|
||||
// Returns true if all the characters in the given string are digits. (0-9)
|
||||
function is_digit(s) =
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_digit(v)]) :
|
||||
let(v = ord(s[0])) (v>=ord("0") && v<=ord("9"));
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_digit(v)]) :
|
||||
let(v = ord(s[0])) (v>=ord("0") && v<=ord("9"));
|
||||
|
||||
|
||||
// Function: is_hexdigit()
|
||||
@@ -573,13 +573,13 @@ function is_digit(s) =
|
||||
// Description:
|
||||
// Returns true if all the characters in the given string are valid hexadecimal digits. (0-9 or a-f or A-F))
|
||||
function is_hexdigit(s) =
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_hexdigit(v)]) :
|
||||
let(v = ord(s[0]))
|
||||
(v>=ord("0") && v<=ord("9")) ||
|
||||
(v>=ord("A") && v<=ord("F")) ||
|
||||
(v>=ord("a") && v<=ord("f"));
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
len(s)>1? all([for (v=s) is_hexdigit(v)]) :
|
||||
let(v = ord(s[0]))
|
||||
(v>=ord("0") && v<=ord("9")) ||
|
||||
(v>=ord("A") && v<=ord("F")) ||
|
||||
(v>=ord("a") && v<=ord("f"));
|
||||
|
||||
|
||||
// Function: is_letter()
|
||||
@@ -588,9 +588,9 @@ function is_hexdigit(s) =
|
||||
// Description:
|
||||
// Returns true if all the characters in the given string are standard ASCII letters. (A-Z or a-z)
|
||||
function is_letter(s) =
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
all([for (v=s) is_lower(v) || is_upper(v)]);
|
||||
assert(is_string(s))
|
||||
s==""? false :
|
||||
all([for (v=s) is_lower(v) || is_upper(v)]);
|
||||
|
||||
|
||||
// Function: str_format()
|
||||
@@ -630,54 +630,54 @@ function is_letter(s) =
|
||||
// str_format("{:-10s}{:.3f}", ["plecostamus",27.43982]); // Returns: "plecostamus27.440"
|
||||
// str_format("{:-10.9s}{:.3f}", ["plecostamus",27.43982]); // Returns: "plecostam 27.440"
|
||||
function str_format(fmt, vals, use_nbsp=false) =
|
||||
let(
|
||||
parts = str_split(fmt,"{")
|
||||
) str_join([
|
||||
for(i = idx(parts))
|
||||
let(
|
||||
found_brace = i==0 || [for (c=parts[i]) if(c=="}") c] != [],
|
||||
err = assert(found_brace, "Unbalanced { in format string."),
|
||||
p = i==0? [undef,parts[i]] : str_split(parts[i],"}"),
|
||||
fmta = p[0],
|
||||
raw = p[1]
|
||||
) each [
|
||||
assert(i<99)
|
||||
is_undef(fmta)? "" : let(
|
||||
fmtb = str_split(fmta,":"),
|
||||
num = is_digit(fmtb[0])? str_int(fmtb[0]) : (i-1),
|
||||
left = fmtb[1][0] == "-",
|
||||
fmtb1 = default(fmtb[1],""),
|
||||
fmtc = left? substr(fmtb1,1) : fmtb1,
|
||||
zero = fmtc[0] == "0",
|
||||
lch = fmtc==""? "" : fmtc[len(fmtc)-1],
|
||||
hastyp = is_letter(lch),
|
||||
typ = hastyp? lch : "s",
|
||||
fmtd = hastyp? substr(fmtc,0,len(fmtc)-1) : fmtc,
|
||||
fmte = str_split((zero? substr(fmtd,1) : fmtd), "."),
|
||||
wid = str_int(fmte[0]),
|
||||
prec = str_int(fmte[1]),
|
||||
val = assert(num>=0&&num<len(vals)) vals[num],
|
||||
unpad = typ=="s"? (
|
||||
let( sval = str(val) )
|
||||
is_undef(prec)? sval :
|
||||
substr(sval, 0, min(len(sval)-1, prec))
|
||||
) :
|
||||
(typ=="d" || typ=="i")? fmt_int(val) :
|
||||
typ=="b"? (val? "true" : "false") :
|
||||
typ=="B"? (val? "TRUE" : "FALSE") :
|
||||
typ=="f"? downcase(fmt_fixed(val,default(prec,6))) :
|
||||
typ=="F"? upcase(fmt_fixed(val,default(prec,6))) :
|
||||
typ=="g"? downcase(fmt_float(val,default(prec,6))) :
|
||||
typ=="G"? upcase(fmt_float(val,default(prec,6))) :
|
||||
assert(false,str("Unknown format type: ",typ)),
|
||||
padlen = max(0,wid-len(unpad)),
|
||||
padfill = str_join([for (i=[0:1:padlen-1]) zero? "0" : use_nbsp? " " : " "]),
|
||||
out = left? str(unpad, padfill) : str(padfill, unpad)
|
||||
)
|
||||
out, raw
|
||||
]
|
||||
]);
|
||||
|
||||
let(
|
||||
parts = str_split(fmt,"{")
|
||||
) str_join([
|
||||
for(i = idx(parts))
|
||||
let(
|
||||
found_brace = i==0 || [for (c=parts[i]) if(c=="}") c] != [],
|
||||
err = assert(found_brace, "Unbalanced { in format string."),
|
||||
p = i==0? [undef,parts[i]] : str_split(parts[i],"}"),
|
||||
fmta = p[0],
|
||||
raw = p[1]
|
||||
) each [
|
||||
assert(i<99)
|
||||
is_undef(fmta)? "" : let(
|
||||
fmtb = str_split(fmta,":"),
|
||||
num = is_digit(fmtb[0])? str_int(fmtb[0]) : (i-1),
|
||||
left = fmtb[1][0] == "-",
|
||||
fmtb1 = default(fmtb[1],""),
|
||||
fmtc = left? substr(fmtb1,1) : fmtb1,
|
||||
zero = fmtc[0] == "0",
|
||||
lch = fmtc==""? "" : fmtc[len(fmtc)-1],
|
||||
hastyp = is_letter(lch),
|
||||
typ = hastyp? lch : "s",
|
||||
fmtd = hastyp? substr(fmtc,0,len(fmtc)-1) : fmtc,
|
||||
fmte = str_split((zero? substr(fmtd,1) : fmtd), "."),
|
||||
wid = str_int(fmte[0]),
|
||||
prec = str_int(fmte[1]),
|
||||
val = assert(num>=0&&num<len(vals)) vals[num],
|
||||
unpad = typ=="s"? (
|
||||
let( sval = str(val) )
|
||||
is_undef(prec)? sval :
|
||||
substr(sval, 0, min(len(sval)-1, prec))
|
||||
) :
|
||||
(typ=="d" || typ=="i")? fmt_int(val) :
|
||||
typ=="b"? (val? "true" : "false") :
|
||||
typ=="B"? (val? "TRUE" : "FALSE") :
|
||||
typ=="f"? downcase(fmt_fixed(val,default(prec,6))) :
|
||||
typ=="F"? upcase(fmt_fixed(val,default(prec,6))) :
|
||||
typ=="g"? downcase(fmt_float(val,default(prec,6))) :
|
||||
typ=="G"? upcase(fmt_float(val,default(prec,6))) :
|
||||
assert(false,str("Unknown format type: ",typ)),
|
||||
padlen = max(0,wid-len(unpad)),
|
||||
padfill = str_join([for (i=[0:1:padlen-1]) zero? "0" : use_nbsp? " " : " "]),
|
||||
out = left? str(unpad, padfill) : str(padfill, unpad)
|
||||
)
|
||||
out, raw
|
||||
]
|
||||
]);
|
||||
|
||||
|
||||
// Function&Module: echofmt()
|
||||
// Usage:
|
||||
@@ -699,4 +699,4 @@ function echofmt(fmt, vals, use_nbsp=false) = echo(str_format(fmt,vals,use_nbsp)
|
||||
module echofmt(fmt, vals, use_nbsp=false) echo(str_format(fmt,vals,use_nbsp));
|
||||
|
||||
|
||||
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
||||
|
Reference in New Issue
Block a user