summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorRay <[email protected]>2020-01-24 17:58:19 +0100
committerRay <[email protected]>2020-01-24 17:58:19 +0100
commit7ae426c377e9ae74119dc11c5ec73f74035843dc (patch)
tree0ea481d660a88587d7d23706d821431aa3f894f9 /src
parentfa0d5bbf9e6b37f5436a4f540571ad3347d531ae (diff)
downloadraylib-7ae426c377e9ae74119dc11c5ec73f74035843dc.tar.gz
raylib-7ae426c377e9ae74119dc11c5ec73f74035843dc.zip
Redesigned TextToInteger()
Diffstat (limited to 'src')
-rw-r--r--src/text.c19
1 files changed, 9 insertions, 10 deletions
diff --git a/src/text.c b/src/text.c
index 242b4fa8..78a3a105 100644
--- a/src/text.c
+++ b/src/text.c
@@ -1396,22 +1396,21 @@ const char *TextToPascal(const char *text)
}
// Get integer value from text
-// NOTE: Negative values not supported
+// NOTE: This function replaces atoi() [stdlib.h]
int TextToInteger(const char *text)
{
- int result = 0;
- int len = strlen(text);
- int units = 1;
+ int value = 0;
+ int sign = 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;
- else { result = -1; break; }
-
- units *= 10;
+ if (text[0] == '-') sign = -1;
+ text++;
}
+
+ for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); ++i) value = value*10 + (int)(text[i] - '0');
- return result;
+ return value*sign;
}
// Encode text codepoint into utf8 text (memory must be freed!)