PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/u64.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 105 lines | 64 code | 15 blank | 26 comment | 6 complexity | 9a0a2884206b0eb3776c70679d6c6777 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file u64.cpp
  3. * @brief Utilities to deal with U64s.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "u64.h"
  28. U64 str_to_U64(const std::string& str)
  29. {
  30. U64 result = 0;
  31. const char *aptr = strpbrk(str.c_str(),"0123456789");
  32. if (!aptr)
  33. {
  34. llwarns << "str_to_U64: Bad string to U64 conversion attempt: format\n" << llendl;
  35. }
  36. else
  37. {
  38. while ((*aptr >= '0') && (*aptr <= '9'))
  39. {
  40. result = result*10 + (*aptr++ - '0');
  41. }
  42. }
  43. return (result);
  44. }
  45. std::string U64_to_str(U64 value)
  46. {
  47. std::string res;
  48. U32 part1,part2,part3;
  49. part3 = (U32)(value % (U64)10000000);
  50. value /= 10000000;
  51. part2 = (U32)(value % (U64)10000000);
  52. value /= 10000000;
  53. part1 = (U32)(value % (U64)10000000);
  54. // three cases to avoid leading zeroes unless necessary
  55. if (part1)
  56. {
  57. res = llformat("%u%07u%07u",part1,part2,part3);
  58. }
  59. else if (part2)
  60. {
  61. res = llformat("%u%07u",part2,part3);
  62. }
  63. else
  64. {
  65. res = llformat("%u",part3);
  66. }
  67. return res;
  68. }
  69. char* U64_to_str(U64 value, char* result, S32 result_size)
  70. {
  71. std::string res = U64_to_str(value);
  72. LLStringUtil::copy(result, res.c_str(), result_size);
  73. return result;
  74. }
  75. F64 U64_to_F64(const U64 value)
  76. {
  77. S64 top_bits = (S64)(value >> 1);
  78. F64 result = (F64)top_bits;
  79. result *= 2.f;
  80. result += (U32)(value & 0x01);
  81. return result;
  82. }
  83. U64 llstrtou64(const char* str, char** end, S32 base)
  84. {
  85. #ifdef LL_WINDOWS
  86. return _strtoui64(str,end,base);
  87. #else
  88. return strtoull(str,end,base);
  89. #endif
  90. }