summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-30 00:17:14 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-03-30 00:17:14 -0700
commit4eb00c73e67e8e41eaa3b6f1a1fb5b6ba542b0f5 (patch)
treea730670cf0e41445eecd42490eb22ead1a6e64ec
parent0a252c30582c4134c7c1d8d7cacab7cf00f3c2c3 (diff)
downloadtxr-4eb00c73e67e8e41eaa3b6f1a1fb5b6ba542b0f5.tar.gz
txr-4eb00c73e67e8e41eaa3b6f1a1fb5b6ba542b0f5.tar.bz2
txr-4eb00c73e67e8e41eaa3b6f1a1fb5b6ba542b0f5.zip
* lib.c (num_str): Much more accurate test for deciding whether
to treat the number as floating or integer. We can't just look for the presence of E, e or . because these coudl be part of trailing junk for instance "123XYZE." should convert to the integer 123, where "XYZE." is trailing junk. * txr.1: Documented int-str, flo-str and num-str.
-rw-r--r--ChangeLog10
-rw-r--r--lib.c9
-rw-r--r--txr.145
3 files changed, 57 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 6efe4902..25da9e32 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2012-03-30 Kaz Kylheku <kaz@kylheku.com>
+
+ * lib.c (num_str): Much more accurate test for deciding whether
+ to treat the number as floating or integer. We can't just look
+ for the presence of E, e or . because these coudl be part of
+ trailing junk for instance "123XYZE." should convert
+ to the integer 123, where "XYZE." is trailing junk.
+
+ * txr.1: Documented int-str, flo-str and num-str.
+
2012-03-29 Kaz Kylheku <kaz@kylheku.com>
* arith.c (numeq): Fix misplaced parenthesis.
diff --git a/lib.c b/lib.c
index eb756086..186a4a8c 100644
--- a/lib.c
+++ b/lib.c
@@ -1998,9 +1998,12 @@ val flo_str(val str)
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);
+ const wchar_t *nws = wcs + wcsspn(wcs, L"\f\n\r\t\v");
+ const wchar_t *dig = nws + wcsspn(wcs, L"+-");
+
+ if (wcsspn(dig, L"0123456789") == wcsspn(dig, L"0123456789eE."))
+ return int_str(str, nil);
+ return flo_str(str);
}
val chrp(val chr)
diff --git a/txr.1 b/txr.1
index 13724a41..3bcd65e9 100644
--- a/txr.1
+++ b/txr.1
@@ -7112,6 +7112,47 @@ however, the original unconverted values are returned. For instance
If three or more arguments are given, max and min are left-associative.
Thus (max a b c) is (max (max a b) c).
+.SS Functions int-str, flo-str and num-str
+
+.TP
+Syntax:
+
+ (int-str <string> <radix>)
+ (flo-str <string>)
+ (num-str <string>)
+
+.TP
+Description:
+
+These functions extract numbers from a string. Leading whitespace, if
+any, is skipped. If no digits can be successfully extracted, then nil is
+returned. Trailing material which does not contribute to the number
+is ignored.
+
+The int-str function converts a string of digits in the specified
+radix to an integer value. For radices above 10, letters of the alphabet
+are used for digits: A represent a digit whose value is 10, B represents 11 and
+so forth until Z. For values of radix above 36, the returned value is
+unspecified. Upper and lower case letters are recognized.
+Any character which is not a digit of the specified radix is regarded
+as the start of trailing junk at which the extraction of the digits stops.
+
+The flo-str function converts a floating-point decimal notation to a nearby
+floating point value. The material which contributes to the value
+is the longest match for optional leading space, followed by a
+mantissa which consists of an optional sign followed by a mixture of at least
+one digit, and at most one decimal point, optionally followed by an exponent
+part denoted by the letter E or e, an optional sign and one or more optional
+exponent digits.
+
+The num-str function converts a decimal notation to either an integer as if by
+a radix 10 application of int-str, or to a floating point value as if by
+flo-str. The floating point interpretation is chosen if the possibly empty
+initial sequence of digits (following any whitespace and optional sign) is
+followed by a period, e or E.
+
+.SS Functions int-flo and flo-int
+
.SS Functions search-regex and match-regex
.SS Function regsub
@@ -7230,10 +7271,6 @@ Thus (max a b c) is (max (max a b) c).
.SS Function string-lt
-.SS Functions int-str, flo-str and num-str
-
-.SS Functions int-flo and flo-int
-
.SS Function chrp
.SS Function chr-isalnum