diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-03-30 00:17:14 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-03-30 00:17:14 -0700 |
commit | 4eb00c73e67e8e41eaa3b6f1a1fb5b6ba542b0f5 (patch) | |
tree | a730670cf0e41445eecd42490eb22ead1a6e64ec | |
parent | 0a252c30582c4134c7c1d8d7cacab7cf00f3c2c3 (diff) | |
download | txr-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-- | ChangeLog | 10 | ||||
-rw-r--r-- | lib.c | 9 | ||||
-rw-r--r-- | txr.1 | 45 |
3 files changed, 57 insertions, 7 deletions
@@ -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. @@ -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) @@ -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 |