StringToInt.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Common/StringToInt.cpp
  2. #include "StdAfx.h"
  3. #include "StringToInt.h"
  4. UInt64 ConvertStringToUInt64(const char *s, const char **end)
  5. {
  6. UInt64 result = 0;
  7. for (;;)
  8. {
  9. char c = *s;
  10. if (c < '0' || c > '9')
  11. {
  12. if (end != NULL)
  13. *end = s;
  14. return result;
  15. }
  16. result *= 10;
  17. result += (c - '0');
  18. s++;
  19. }
  20. }
  21. UInt64 ConvertOctStringToUInt64(const char *s, const char **end)
  22. {
  23. UInt64 result = 0;
  24. for (;;)
  25. {
  26. char c = *s;
  27. if (c < '0' || c > '7')
  28. {
  29. if (end != NULL)
  30. *end = s;
  31. return result;
  32. }
  33. result <<= 3;
  34. result += (c - '0');
  35. s++;
  36. }
  37. }
  38. UInt64 ConvertStringToUInt64(const wchar_t *s, const wchar_t **end)
  39. {
  40. UInt64 result = 0;
  41. for (;;)
  42. {
  43. wchar_t c = *s;
  44. if (c < '0' || c > '9')
  45. {
  46. if (end != NULL)
  47. *end = s;
  48. return result;
  49. }
  50. result *= 10;
  51. result += (c - '0');
  52. s++;
  53. }
  54. }
  55. Int64 ConvertStringToInt64(const char *s, const char **end)
  56. {
  57. if (*s == '-')
  58. return -(Int64)ConvertStringToUInt64(s + 1, end);
  59. return ConvertStringToUInt64(s, end);
  60. }