Std.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import php.Boot;
  2. /*
  3. * Copyright (C)2005-2019 Haxe Foundation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. import php.Global;
  24. import php.Const;
  25. import php.Syntax;
  26. @:coreApi class Std {
  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 Boot.isOfType(v, t);
  32. }
  33. public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  34. return Boot.isOfType(value, cast 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 Boot.isOfType(value, cast c) ? cast value : null;
  39. }
  40. public static function string(s:Dynamic):String {
  41. return Boot.stringify(s);
  42. }
  43. public static inline function int(x:Float):Int {
  44. return Syntax.int(x);
  45. }
  46. public static function parseInt(x:String):Null<Int> {
  47. if (Global.is_numeric(x)) {
  48. return Global.intval(x, 10);
  49. } else {
  50. x = Global.ltrim(x);
  51. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  52. var firstCharCode = x.charCodeAt(firstCharIndex);
  53. if (!isDigitCode(firstCharCode)) {
  54. return null;
  55. }
  56. var secondChar = x.charAt(firstCharIndex + 1);
  57. if (secondChar == 'x' || secondChar == 'X') {
  58. return Global.intval(x, 0);
  59. } else {
  60. return Global.intval(x, 10);
  61. }
  62. }
  63. }
  64. public static function parseFloat(x:String):Float {
  65. var result = Global.floatval(x);
  66. if (result != 0)
  67. return result;
  68. x = Global.ltrim(x);
  69. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  70. var charCode = x.charCodeAt(firstCharIndex);
  71. if (charCode == '.'.code) {
  72. charCode = x.charCodeAt(firstCharIndex + 1);
  73. }
  74. if (isDigitCode(charCode)) {
  75. return 0.0;
  76. } else {
  77. return Const.NAN;
  78. }
  79. }
  80. public static inline function random(x:Int):Int {
  81. return x <= 1 ? 0 : Global.mt_rand(0, x - 1);
  82. }
  83. static inline function isDigitCode(charCode:Null<Int>):Bool {
  84. return charCode != null && charCode >= '0'.code && charCode <= '9'.code;
  85. }
  86. }