diff options
-rw-r--r-- | ChangeLog | 17 | ||||
-rw-r--r-- | eval.c | 1 | ||||
-rw-r--r-- | filter.c | 9 | ||||
-rw-r--r-- | filter.h | 1 | ||||
-rw-r--r-- | lib.c | 8 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | txr.1 | 39 |
7 files changed, 71 insertions, 5 deletions
@@ -1,5 +1,22 @@ 2012-03-26 Kaz Kylheku <kaz@kylheku.com> + * eval.c (eval_init): New intrinsic num-str registered. + + * filter.c (tonumber_k, tointeger_k, tofloat_k, hextoint_k): + New keyword variables. + (filter_init): New variables initialized; new filters registered. + + * filter.h (tonumber_k, tointeger_k, tofloat_k, hextoint_k): + Declared. + + * lib.c (num_str): New function. + + * lib.h (num_str): Declared. + + * txr.1: New filters documented. + +2012-03-26 Kaz Kylheku <kaz@kylheku.com> + * arith.c (to_float): Fix unterminated argument list in throwf. * lib.c (funcall): Likewise. @@ -2302,6 +2302,7 @@ void eval_init(void) reg_fun(intern(lit("string-lt"), user_package), func_n2(string_lt)); reg_fun(intern(lit("int-str"), user_package), func_n2o(int_str, 1)); reg_fun(intern(lit("flo-str"), user_package), func_n1(flo_str)); + reg_fun(intern(lit("num-str"), user_package), func_n1(num_str)); reg_fun(intern(lit("int-flo"), user_package), func_n1(int_flo)); reg_fun(intern(lit("flo-int"), user_package), func_n1(flo_int)); reg_fun(intern(lit("chrp"), user_package), func_n1(chrp)); @@ -45,6 +45,7 @@ val filters; val filter_k, lfilt_k, rfilt_k, to_html_k, from_html_k; val upcase_k, downcase_k, fun_k; val topercent_k, frompercent_k, tourl_k, fromurl_k; +val tonumber_k, tointeger_k, tofloat_k, hextoint_k; static val make_trie(void) { @@ -669,6 +670,10 @@ void filter_init(void) frompercent_k = intern(lit("frompercent"), keyword_package); tourl_k = intern(lit("tourl"), keyword_package); fromurl_k = intern(lit("fromurl"), keyword_package); + tonumber_k = intern(lit("tonumber"), keyword_package); + tointeger_k = intern(lit("toinger"), keyword_package); + tofloat_k = intern(lit("tofloat"), keyword_package); + hextoint_k = intern(lit("hextoint"), keyword_package); sethash(filters, to_html_k, build_filter(to_html_table, t)); { @@ -683,4 +688,8 @@ void filter_init(void) sethash(filters, frompercent_k, curry_12_1(func_n2(url_decode), nil)); sethash(filters, tourl_k, curry_12_1(func_n2(url_encode), t)); sethash(filters, fromurl_k, curry_12_1(func_n2(url_decode), t)); + sethash(filters, tonumber_k, func_n1(num_str)); + sethash(filters, tointeger_k, curry_12_1(func_n2(int_str), nil)); + sethash(filters, tofloat_k, func_n1(flo_str)); + sethash(filters, hextoint_k, curry_12_1(func_n2(int_str), num_fast(16))); } @@ -28,6 +28,7 @@ extern val filters; extern val filter_k, lfilt_k, rfilt_k, to_html_k, from_html_k; extern val upcase_k, downcase_k, fun_k; extern val topercent_k, frompercent_k, tourl_k, fromurl_k; +extern val tonumber_k, tointeger_k, tofloat_k, hextoint_k; val trie_lookup_begin(val trie); val trie_value_at(val node); @@ -1981,6 +1981,14 @@ val flo_str(val str) return flo(value); } +val num_str(val str) +{ + const wchar_t *wcs = c_str(str); + if (wcspbrk(wcs, L".eE")) + return flo_str(str); + return int_str(str, nil); +} + val chrp(val chr) { return (is_chr(chr)) ? t : nil; @@ -462,6 +462,7 @@ val trim_str(val str); val string_lt(val astr, val bstr); val int_str(val str, val base); val flo_str(val str); +val num_str(val str); val int_flo(val f); val flo_int(val i); val chrp(val chr); @@ -3761,7 +3761,11 @@ implements named filters. Built-in filters are named by keywords, given below. User-defined filters are possible, however. See notes on the deffilter directive below. -Built-in filters: +Instead of a filter name, the syntax (fun NAME) can be used. This +denotes that the function called NAME is to be used as a filter. +This is discussed in the next section Function Filters below. + +Built-in filters named by keywords: .IP :to_html Filter text to HTML, representing special characters using HTML @@ -3806,7 +3810,30 @@ Encode to URL encoding, which is like percent encoding except that a space maps to + rather than %20. The + character, being in the reserved set, encodes to %2B. -Example: to escape HTML characters in all variable substitutions occuring in an +.PP :tonumber +Converts strings to numbers. Strings that contain a period, e or E are +converted to floating point as if by the function flo-str. Otherwise +they are converted to integer as if using int-str with a radix of 10. +Non-numeric junk results in the object nil. + +.PP :tointeger +Converts strings to integers as if using int-str with a radix of 10. +Non-numeric junk results in the object nil. + +.PP :tofloat +Converts strings to floating-point values as if using the function flo-str. +Non-numeric junk results in the object nil. + +.PP :hextoint +Converts strings to integers as if using int-str with a radix of 16. +Non-numeric junk results in the object nil. + +.PP + +.TP +Examples + +To escape HTML characters in all variable substitutions occuring in an output clause, specify :filter :to_html in the directive: @(output :filter :to_html) @@ -3833,9 +3860,11 @@ because '"' will turn to '"' which no longer be recognized by the :from_html filter, because the entity names in HTML codes are case-sensitive. -Instead of a filter name, the syntax (fun NAME) can be used. This -denotes that the function called NAME is to be used as a filter. -This is discussed in the next section Function Filters below. +Capture some numeric variables and convert to numbers: + + @date @time @temperature @pressure + @(filter :tofloat temperature pressure) + @;; temperature and pressure can now be used in calculations .SS Function Filters |