summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-26 10:41:32 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-03-26 10:41:32 -0700
commit27653477b64ce60f15c538854d24943d2a3f62ec (patch)
treeb41678d0ec5804314c9f89d2c8f207ac74919f29
parent7892a9800b16ed13e59f92f236dfc373762c0048 (diff)
downloadtxr-27653477b64ce60f15c538854d24943d2a3f62ec.tar.gz
txr-27653477b64ce60f15c538854d24943d2a3f62ec.tar.bz2
txr-27653477b64ce60f15c538854d24943d2a3f62ec.zip
* 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.
-rw-r--r--ChangeLog17
-rw-r--r--eval.c1
-rw-r--r--filter.c9
-rw-r--r--filter.h1
-rw-r--r--lib.c8
-rw-r--r--lib.h1
-rw-r--r--txr.139
7 files changed, 71 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index c57dd97d..3c3010ec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/eval.c b/eval.c
index 644998f0..4a028ca4 100644
--- a/eval.c
+++ b/eval.c
@@ -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));
diff --git a/filter.c b/filter.c
index 44e83936..3fc262a7 100644
--- a/filter.c
+++ b/filter.c
@@ -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)));
}
diff --git a/filter.h b/filter.h
index 806965c6..8c1c0df4 100644
--- a/filter.h
+++ b/filter.h
@@ -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);
diff --git a/lib.c b/lib.c
index a0872cf8..d738237e 100644
--- a/lib.c
+++ b/lib.c
@@ -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;
diff --git a/lib.h b/lib.h
index 5706aa6a..52695229 100644
--- a/lib.h
+++ b/lib.h
@@ -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);
diff --git a/txr.1 b/txr.1
index 4441503e..50e063b8 100644
--- a/txr.1
+++ b/txr.1
@@ -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 '&quot;' will turn to '&QUOT;' 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