Std.hx 3.6 KB

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