arezey

atoi in acs

Jan 20th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. // ============================================================================
  2. // String-to-integer
  3. function int atoi (str a) {
  4. int val, neg;
  5. int mul = 1;
  6.  
  7. for (int i = StrLen (a) - 1; i >= 0; i--) {
  8. int c = GetChar (a, i);
  9. if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
  10. continue; // Skip past whitespace
  11.  
  12. // Minus sign
  13. if (c == '-' && !neg && val != 0) {
  14. neg = true;
  15. continue;
  16. }
  17.  
  18. // Number values
  19. if (c >= '0' && c <= '9') {
  20. val += mul * (c - '0');
  21. mul *= 10;
  22. continue;
  23. }
  24.  
  25. // If there's any junk, we stop now.
  26. return 0;
  27. }
  28.  
  29. if (neg)
  30. val *= -1;
  31. return val;
  32. }
  33.  
  34. function str itoa (int a) {
  35. return strparam (d:a);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment