Std.hx 3.6 KB

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