Std.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. import lua.Boot;
  23. import lua.NativeStringTools;
  24. @:keepInit
  25. @:coreApi class Std {
  26. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  27. return untyped lua.Boot.__instanceof(v, t);
  28. }
  29. public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  30. return untyped lua.Boot.__instanceof(value, c) ? cast value : null;
  31. }
  32. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  33. public static inline function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  34. return downcast(value, c);
  35. }
  36. @:keep
  37. public static function string(s:Dynamic):String {
  38. return untyped lua.Boot.__string_rec(s);
  39. }
  40. public static function int(x:Float):Int {
  41. if (!Math.isFinite(x) || Math.isNaN(x))
  42. return 0;
  43. else
  44. return lua.Boot.clampInt32(x);
  45. }
  46. public static function parseInt(x:String):Null<Int> {
  47. if (x == null)
  48. return null;
  49. var hexMatch = NativeStringTools.match(x, "^[ \t\r\n]*([%-+]*0[xX][%da-fA-F]*)");
  50. if (hexMatch != null) {
  51. var sign = switch StringTools.fastCodeAt(hexMatch, 0) {
  52. case '-'.code: -1;
  53. case '+'.code: 1;
  54. case _: 0;
  55. }
  56. return (sign == -1 ? -1 : 1) * lua.Lua.tonumber(hexMatch.substr(sign == 0 ? 2 : 3), 16);
  57. } else {
  58. var intMatch = NativeStringTools.match(x, "^ *[%-+]?%d*");
  59. if (intMatch != null) {
  60. return lua.Lua.tonumber(intMatch);
  61. } else {
  62. return null;
  63. }
  64. }
  65. }
  66. public static function parseFloat(x:String):Float {
  67. if (x == null || x == "")
  68. return Math.NaN;
  69. var digitMatch = NativeStringTools.match(x, "^ *[%.%-+]?[0-9]%d*");
  70. if (digitMatch == null) {
  71. return Math.NaN;
  72. }
  73. x = x.substr(digitMatch.length);
  74. var decimalMatch = NativeStringTools.match(x, "^%.%d*");
  75. if (decimalMatch == null)
  76. decimalMatch = "";
  77. x = x.substr(decimalMatch.length);
  78. var eMatch = NativeStringTools.match(x, "^[eE][+%-]?%d+");
  79. if (eMatch == null)
  80. eMatch = "";
  81. var result = lua.Lua.tonumber(digitMatch + decimalMatch + eMatch);
  82. return result != null ? result : Math.NaN;
  83. }
  84. public static function random(x:Int):Int {
  85. return untyped x <= 0 ? 0 : Math.floor(Math.random() * x);
  86. }
  87. static function __init__():Void
  88. untyped {
  89. __feature__("lua.Boot.isClass", String.__name__ = __feature__("Type.getClassName", "String", true));
  90. __feature__("Type.resolveClass", _hxClasses["Array"] = Array);
  91. __feature__("lua.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
  92. }
  93. }