Int64Helper.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe;
  23. using haxe.Int64;
  24. import StringTools;
  25. /**
  26. Helper for parsing to `Int64` instances.
  27. **/
  28. class Int64Helper {
  29. /**
  30. Create `Int64` from given string.
  31. **/
  32. public static function parseString(sParam:String):Int64 {
  33. var base = Int64.ofInt(10);
  34. var current = Int64.ofInt(0);
  35. var multiplier = Int64.ofInt(1);
  36. var sIsNegative = false;
  37. var s = StringTools.trim(sParam);
  38. if (s.charAt(0) == "-") {
  39. sIsNegative = true;
  40. s = s.substring(1, s.length);
  41. }
  42. var len = s.length;
  43. for (i in 0...len) {
  44. var digitInt = s.charCodeAt(len - 1 - i) - '0'.code;
  45. if (digitInt < 0 || digitInt > 9) {
  46. throw "NumberFormatError";
  47. }
  48. if (digitInt != 0) {
  49. var digit:Int64 = Int64.ofInt(digitInt);
  50. if (sIsNegative) {
  51. current = Int64.sub(current, Int64.mul(multiplier, digit));
  52. if (!Int64.isNeg(current)) {
  53. throw "NumberFormatError: Underflow";
  54. }
  55. } else {
  56. current = Int64.add(current, Int64.mul(multiplier, digit));
  57. if (Int64.isNeg(current)) {
  58. throw "NumberFormatError: Overflow";
  59. }
  60. }
  61. }
  62. multiplier = Int64.mul(multiplier, base);
  63. }
  64. return current;
  65. }
  66. /**
  67. Create `Int64` from given float.
  68. **/
  69. public static function fromFloat(f:Float):Int64 {
  70. if (Math.isNaN(f) || !Math.isFinite(f)) {
  71. throw "Number is NaN or Infinite";
  72. }
  73. var noFractions = f - (f % 1);
  74. // 2^53-1 and -2^53+1: these are parsable without loss of precision.
  75. // In theory 2^53 and -2^53 are parsable too, but then there's no way to
  76. // distinguish 2^53 from 2^53+1
  77. // (i.e. trace(9007199254740992. + 1. > 9007199254740992.); // false!)
  78. if (noFractions > 9007199254740991) {
  79. throw "Conversion overflow";
  80. }
  81. if (noFractions < -9007199254740991) {
  82. throw "Conversion underflow";
  83. }
  84. var result = Int64.ofInt(0);
  85. var neg = noFractions < 0;
  86. var rest = neg ? -noFractions : noFractions;
  87. var i = 0;
  88. while (rest >= 1) {
  89. var curr = rest % 2;
  90. rest = rest / 2;
  91. if (curr >= 1) {
  92. result = Int64.add(result, Int64.shl(Int64.ofInt(1), i));
  93. }
  94. i++;
  95. }
  96. if (neg) {
  97. result = Int64.neg(result);
  98. }
  99. return result;
  100. }
  101. }