Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ============================================================================
- // String-to-integer
- function int atoi (str a) {
- int val, neg;
- int mul = 1;
- for (int i = StrLen (a) - 1; i >= 0; i--) {
- int c = GetChar (a, i);
- if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
- continue; // Skip past whitespace
- // Minus sign
- if (c == '-' && !neg && val != 0) {
- neg = true;
- continue;
- }
- // Number values
- if (c >= '0' && c <= '9') {
- val += mul * (c - '0');
- mul *= 10;
- continue;
- }
- // If there's any junk, we stop now.
- return 0;
- }
- if (neg)
- val *= -1;
- return val;
- }
- function str itoa (int a) {
- return strparam (d:a);
- }
Advertisement
Add Comment
Please, Sign In to add comment