mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-12-13 19:49:36 +00:00
Add StringToNum implementation
This commit is contained in:
@@ -1,52 +1,52 @@
|
|||||||
#include "PLNumberFormatting.h"
|
#include "PLNumberFormatting.h"
|
||||||
#include "PLPasStr.h"
|
#include "PLPasStr.h"
|
||||||
|
|
||||||
void StringToNum(const PLPasStr &str, long *num)
|
void StringToNum(const PLPasStr &str, long *num)
|
||||||
{
|
{
|
||||||
if (str.Length() == 0)
|
if (str.Length() == 0)
|
||||||
{
|
{
|
||||||
num = 0;
|
num = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_t len = str.Length();
|
const size_t len = str.Length();
|
||||||
const char *chars = str.Chars();
|
const char *chars = str.Chars();
|
||||||
const char *charsEnd = chars + len;
|
const char *charsEnd = chars + len;
|
||||||
|
|
||||||
long result = 0;
|
long result = 0;
|
||||||
|
|
||||||
if (chars[0] == '-')
|
if (chars[0] == '-')
|
||||||
{
|
{
|
||||||
chars++;
|
chars++;
|
||||||
|
|
||||||
while (chars != charsEnd)
|
while (chars != charsEnd)
|
||||||
{
|
{
|
||||||
const char c = *chars++;
|
const char c = *chars++;
|
||||||
|
|
||||||
if (c < '0' || c > '9')
|
if (c < '0' || c > '9')
|
||||||
{
|
{
|
||||||
num = 0;
|
num = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = result * 10 - (c - '0');
|
result = result * 10 - (c - '0');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (chars != charsEnd)
|
while (chars != charsEnd)
|
||||||
{
|
{
|
||||||
const char c = *chars++;
|
const char c = *chars++;
|
||||||
|
|
||||||
if (c < '0' || c > '9')
|
if (c < '0' || c > '9')
|
||||||
{
|
{
|
||||||
num = 0;
|
num = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = result * 10 + (c - '0');
|
result = result * 10 + (c - '0');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*num = result;
|
*num = result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user