Std.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, "^ *[%-+]*0[xX][%da-fA-F]*");
  50. if (hexMatch != null) {
  51. return lua.Lua.tonumber(hexMatch.substr(2), 16);
  52. } else {
  53. var intMatch = NativeStringTools.match(x, "^ *[%-+]?%d*");
  54. if (intMatch != null) {
  55. return lua.Lua.tonumber(intMatch);
  56. } else {
  57. return null;
  58. }
  59. }
  60. }
  61. public static function parseFloat(x:String):Float {
  62. if (x == null || x == "")
  63. return Math.NaN;
  64. var digitMatch = NativeStringTools.match(x, "^ *[%.%-+]?[0-9]%d*");
  65. if (digitMatch == null) {
  66. return Math.NaN;
  67. }
  68. x = x.substr(digitMatch.length);
  69. var decimalMatch = NativeStringTools.match(x, "^%.%d*");
  70. if (decimalMatch == null)
  71. decimalMatch = "";
  72. x = x.substr(decimalMatch.length);
  73. var eMatch = NativeStringTools.match(x, "^[eE][+%-]?%d+");
  74. if (eMatch == null)
  75. eMatch = "";
  76. var result = lua.Lua.tonumber(digitMatch + decimalMatch + eMatch);
  77. return result != null ? result : Math.NaN;
  78. }
  79. public static function random(x:Int):Int {
  80. return untyped x <= 0 ? 0 : Math.floor(Math.random() * x);
  81. }
  82. static function __init__():Void
  83. untyped {
  84. __feature__("lua.Boot.isClass", String.__name__ = __feature__("Type.getClassName", "String", true));
  85. __feature__("Type.resolveClass", _hxClasses["Array"] = Array);
  86. __feature__("lua.Boot.isClass", Array.__name__ = __feature__("Type.getClassName", "Array", true));
  87. }
  88. }