Created
June 25, 2017 07:43
-
-
Save syfluqs/43e5129e94b9688cf562bb61cace9d39 to your computer and use it in GitHub Desktop.
This file contains 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
// returns the first valid (long) integer value from the current position. | |
// lookahead determines how parseInt looks ahead in the stream. | |
// See LookaheadMode enumeration at the top of the file. | |
// Lookahead is terminated by the first character that is not a valid part of an integer. | |
// Once parsing commences, 'ignore' will be skipped in the stream. | |
long Stream::parseInt(LookaheadMode lookahead, char ignore) | |
{ | |
bool isNegative = false; | |
long value = 0; | |
int c; | |
c = peekNextDigit(lookahead, false); | |
// ignore non numeric leading characters | |
if(c < 0) | |
return 0; // zero returned if timeout | |
do{ | |
if(c == ignore) | |
; // ignore this character | |
else if(c == '-') | |
isNegative = true; | |
else if(c >= '0' && c <= '9') // is c a digit? | |
value = value * 10 + c - '0'; | |
read(); // consume the character we got with peek | |
c = timedPeek(); | |
} | |
while( (c >= '0' && c <= '9') || c == ignore ); | |
if(isNegative) | |
value = -value; | |
return value; | |
} | |
// as parseInt but returns a floating point value | |
float Stream::parseFloat(LookaheadMode lookahead, char ignore) | |
{ | |
bool isNegative = false; | |
bool isFraction = false; | |
long value = 0; | |
int c; | |
float fraction = 1.0; | |
c = peekNextDigit(lookahead, true); | |
// ignore non numeric leading characters | |
if(c < 0) | |
return 0; // zero returned if timeout | |
do{ | |
if(c == ignore) | |
; // ignore | |
else if(c == '-') | |
isNegative = true; | |
else if (c == '.') | |
isFraction = true; | |
else if(c >= '0' && c <= '9') { // is c a digit? | |
value = value * 10 + c - '0'; | |
if(isFraction) | |
fraction *= 0.1; | |
} | |
read(); // consume the character we got with peek | |
c = timedPeek(); | |
} | |
while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore ); | |
if(isNegative) | |
value = -value; | |
if(isFraction) | |
return value * fraction; | |
else | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment