Skip to content

Instantly share code, notes, and snippets.

@LYP951018
Created December 9, 2016 13:50
Show Gist options
  • Select an option

  • Save LYP951018/ca7a240a8e8f66e8eb8c5bb693243a53 to your computer and use it in GitHub Desktop.

Select an option

Save LYP951018/ca7a240a8e8f66e8eb8c5bb693243a53 to your computer and use it in GitHub Desktop.
bool SkipIfChar(const char* str, const char** newPos, char c)
{
const char* pos = str;
while(*pos == c)
++pos;
*newPos = pos;
return pos != str;
}
bool SkipIfNextChar(const char* str, const char** newPos, char c)
{
if (*str == c)
{
*newPos = ++str;
return true;
}
return false;
}
bool SkipIfSpace(const char* str, const char** newPos)
{
return SkipIfChar(str, newPos, ' ');
}
bool SkipIfDigit(const char* str, const char** newPos)
{
const char* pos = str;
while(*pos <= '9' && *pos >= '0')
++pos;
*newPos = pos;
return pos != str;
}
bool SkipIfSign(const char* str, const char** newPos)
{
const char* pos = str;
if(*pos == '+' || *pos == '-')
++pos;
*newPos = pos;
return pos != str;
}
bool SkipIfExp(const char* str, const char** newPos)
{
SkipIfSign(str, &str);
if (SkipIfDigit(str, newPos))
return true;
return false;
}
bool SkipIfInteger(const char* str, const char** newPos)
{
return SkipIfDigit(str, newPos) && (!SkipIfNextChar(*newPos, newPos, 'e') || SkipIfExp(*newPos, newPos));
}
bool IsEnd(const char* str)
{
SkipIfSpace(str, &str);
return *str == '\0';
}
bool isNumber(const char* s)
{
SkipIfSpace(s, &s);
SkipIfSign(s, &s);
if (SkipIfNextChar(s, &s, '.'))
{
return SkipIfInteger(s, &s) && IsEnd(s);
}
else if (SkipIfDigit(s, &s))
{
if (SkipIfNextChar(s, &s, '.'))
{
SkipIfDigit(s, &s);
return (!SkipIfNextChar(s, &s, 'e') || SkipIfExp(s, &s)) && IsEnd(s);
}
else if (SkipIfNextChar(s, &s, 'e'))
return SkipIfExp(s, &s) && IsEnd(s);
return IsEnd(s);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment