Redesigned TextToInteger()
This commit is contained in:
parent
fa0d5bbf9e
commit
7ae426c377
1 changed files with 9 additions and 10 deletions
19
src/text.c
19
src/text.c
|
@ -1396,22 +1396,21 @@ const char *TextToPascal(const char *text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get integer value from text
|
// Get integer value from text
|
||||||
// NOTE: Negative values not supported
|
// NOTE: This function replaces atoi() [stdlib.h]
|
||||||
int TextToInteger(const char *text)
|
int TextToInteger(const char *text)
|
||||||
{
|
{
|
||||||
int result = 0;
|
int value = 0;
|
||||||
int len = strlen(text);
|
int sign = 1;
|
||||||
int units = 1;
|
|
||||||
|
|
||||||
for (int i = len - 1; i >= 0; i--)
|
if ((text[0] == '+') || (text[0] == '-'))
|
||||||
{
|
{
|
||||||
if ((text[i] > 47) && (text[i] < 58)) result += ((int)text[i] - 48)*units;
|
if (text[0] == '-') sign = -1;
|
||||||
else { result = -1; break; }
|
text++;
|
||||||
|
|
||||||
units *= 10;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); ++i) value = value*10 + (int)(text[i] - '0');
|
||||||
|
|
||||||
|
return value*sign;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode text codepoint into utf8 text (memory must be freed!)
|
// Encode text codepoint into utf8 text (memory must be freed!)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue