Created
October 22, 2015 20:30
-
-
Save gettalong/0266a0c335720e4e6cfc to your computer and use it in GitHub Desktop.
Possible `StringScanner#scan_float` method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static VALUE | |
strscan_scan_float(VALUE self) | |
{ | |
struct strscanner *p; | |
double retval; | |
char *start; | |
char *end; | |
GET_SCANNER(self, p); | |
if (EOS_P(p)) | |
return Qnil; | |
start = S_PBEG(p) + p->curr; | |
while (ISSPACE(*start)) start++; | |
if (start[0] == '0' && (start[1] == 'x' || start[1] == 'X')) | |
return Qnil; | |
retval = strtod(start, &end); | |
if (errno == ERANGE) { | |
errno = 0; | |
rb_invalid_str(S_PBEG(p), "scan_float"); | |
} | |
if (start == end) | |
return Qnil; | |
CLEAR_MATCH_STATUS(p); | |
p->prev = p->curr; | |
p->curr += end - S_PBEG(p) - p->prev; | |
MATCHED(p); | |
adjust_registers_to_matched(p); | |
return DBL2NUM(retval); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment