History

str->num: ignore leading and trailing whitespace

Sigrid Solveig Haflínudóttir committed 3 months ago

Commit fa39cec1 parent 09b46d94 files · +34 −17Browse files

Showing the changes to src (3 of 4 files). Show all

Unified · Split

3 files
  1. src/docs.sl +2 −2
  2. src/read.c +22 −14
  3. src/str.c +9 −0
src/docs.sl+2−2
@@ -482,8 +482,8 @@ · 481 unmodified lines
482482 (defdoc (str->num s (:base NIL))
483483 «Return the number using the radix `base` in the string `s`.
484484
485− Returns `NIL` if the number could not be parsed or there were
486− left-over trailing characters.»
485+ Returns `NIL` if the number could not be parsed. Any number of leading
486+ and/or trailing whitespace runes is allowed and those will be ignored.»
487487 :doc-example
488488 «(str->num "1.2")
489489 ; ↳ 1.2»
src/read.c+22−14
@@ -42,19 +42,19 @@ sl_read_numtok(const char *tok, sl_v *pval, int base) · 41 unmodified lines
4242 s64int i64;
4343 double d;
4444
45− if(sl_unlikely(*tok == '\0'))
45+ if(sl_unlikely(*tok == 0))
4646 return false;
4747 if(!((tok[0] == '0' && tok[1] == 'x') || (base >= 15)) && strpbrk(tok, ".eEpP") != nil){
4848 d = strtod(tok, &end);
49− if(sl_unlikely(*end == '\0')){
49+ if(sl_unlikely(*end == 0)){
5050 if(pval != nil)
5151 *pval = mk_f64(d);
5252 return true;
5353 }
5454 // floats can end in f or f0
5555 if(end > tok && end[0] == 'f' &&
56− (end[1] == '\0' ||
57− (end[1] == '0' && end[2] == '\0'))){
56+ (end[1] == 0 ||
57+ (end[1] == '0' && end[2] == 0))){
5858 if(pval != nil)
5959 *pval = mk_f32((float)d);
6060 return true;
@@ -77,8 +77,12 @@ sl_read_numtok(const char *tok, sl_v *pval, int base) · 16 unmodified lines
7777 i64 = INT64_MAX;
7878 else{
7979 i64 = strtoll(tok, &end, base);
80− if(*end != '\0')
81− return false;
80+ if(*end != 0){
81+ Rune r;
82+ chartorune(&r, end);
83+ if(!isspacerune(r))
84+ return false;
85+ }
8286 }
8387 if(sl_unlikely(pval == nil))
8488 return true;
@@ -89,9 +93,13 @@ sl_read_numtok(const char *tok, sl_v *pval, int base) · 4 unmodified lines
8993 mpint *m = strtomp(tok, &end, base, nil);
9094 if(m == nil)
9195 return false;
92− if(end != tok+strlen(tok)){
93− mpfree(m);
94− return false;
96+ if(*end != 0){
97+ Rune r;
98+ chartorune(&r, end);
99+ if(!isspacerune(r)){
100+ mpfree(m);
101+ return false;
102+ }
95103 }
96104 if(mpsignif(m) >= FIXNUM_BITS){
97105 *pval = mk_bignum(m);
@@ -207,7 +215,7 @@ read_token(Rune c, bool digits) · 109 unmodified lines
207215 if(sl_unlikely(nc == 0))
208216 cthrow(parse_error("unexpected char: #x%"PRIx32, c), c);
209217 terminate:
210− RS->buf[i++] = '\0';
218+ RS->buf[i++] = 0;
211219 return issym;
212220 }
213221
@@ -357,7 +365,7 @@ again: · 143 unmodified lines
357365 }else
358366 cthrow(parse_error( "invalid label: #%s%c", RS->buf, ch), dummy);
359367 x = strtoll(RS->buf, &end, 10);
360− if(*end != '\0')
368+ if(*end != 0)
361369 cthrow(parse_error("invalid label: #%s%c", RS->buf, ch), dummy);
362370 RS->tokval = fixnum(x);
363371 }else if(sl_unlikely(c == '!')){
@@ -410,7 +418,7 @@ again: · 46 unmodified lines
410418 RS->tokval = mk_sym(RS->buf, true);
411419 }else{
412420 x = strtol(RS->buf, &end, 10);
413− if(*end != '\0' || RS->buf[0] == '\0')
421+ if(*end != 0 || RS->buf[0] == 0)
414422 cthrow(parse_error("invalid gensym label: #%s", RS->buf), dummy);
415423 RS->tokval = fixnum(x);
416424 }
@@ -636,7 +644,7 @@ read_str(int dummy) · 219 unmodified lines
636644 break;
637645 ios_getc(IO);
638646 }
639− eseq[j] = '\0';
647+ eseq[j] = 0;
640648 r = strtol(eseq, nil, 8);
641649 // \DDD and \xXX read bytes, not runes
642650 buf[i++] = r;
@@ -648,7 +656,7 @@ read_str(int dummy) · 5 unmodified lines
648656 eseq[j++] = c;
649657 ios_getc(IO);
650658 }
651− eseq[j] = '\0';
659+ eseq[j] = 0;
652660 if(j)
653661 r = strtol(eseq, nil, 16);
654662 if(!j || r > Runemax){
src/str.c+9−0
@@ -724,6 +724,15 @@ BUILTIN("str->num", str_2_num) · 723 unmodified lines
724724 if(nargs != 1)
725725 argcount(1);
726726 char *s = tostr(args[0]), *e;
727+ for(;;){
728+ Rune r;
729+ int n = chartorune(&r, s);
730+ if(r == Runeerror)
731+ return sl_nil;
732+ if(!isspacerune(r))
733+ break;
734+ s += n;
735+ }
727736 sl_v n;
728737 int base = 10;
729738 if(nonnil(values[0]))

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t