Std.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. @:deprecated('Std.is is deprecated. Use Std.isOfType instead.')
  28. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  29. return isOfType(v, t);
  30. }
  31. public static inline function isOfType(v:Dynamic, t:Dynamic):Bool {
  32. return Boot.isOfType(v, t);
  33. }
  34. public static inline function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  35. return Boot.isOfType(value, cast c) ? cast value : null;
  36. }
  37. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  38. public static inline function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  39. return Boot.isOfType(value, cast c) ? cast value : null;
  40. }
  41. public static function string(s:Dynamic):String {
  42. return Boot.stringify(s);
  43. }
  44. public static inline function int(x:Float):Int {
  45. return Syntax.int(x);
  46. }
  47. public static function parseInt(x:String):Null<Int> {
  48. x = Global.ltrim(x, " \t\n\x0b\x0c\r");
  49. final digitsOnly = Global.ltrim(x, "+-");
  50. if (Global.str_starts_with(digitsOnly, '0x') || Global.str_starts_with(digitsOnly, '0X')) {
  51. final val = Global.intval(x, 16); // hexadecimal
  52. // if the value was 0, ensure there is only a maximum of one + or - sign
  53. if (val == 0 && digitsOnly.length + 1 < x.length)
  54. return null;
  55. return val;
  56. }
  57. switch Global.stripos(x, 'e') {
  58. case false:
  59. case ePos: x = Global.substr(x, 0, ePos);
  60. }
  61. final val = Global.intval(x, 10);
  62. // if the value was 0, make sure it wasn't because the string had no valid digits
  63. // last check ensures there is only a maximum of one + or - sign
  64. if (val == 0 && (Global.strspn(digitsOnly, "0123456789", 0, 1) == 0 || digitsOnly.length + 1 < x.length))
  65. return null;
  66. return val;
  67. }
  68. public static function parseFloat(x:String):Float {
  69. var result = Global.floatval(x);
  70. if (result != 0)
  71. return result;
  72. x = Global.ltrim(x);
  73. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  74. var charCode = x.charCodeAt(firstCharIndex);
  75. if (charCode == '.'.code) {
  76. charCode = x.charCodeAt(firstCharIndex + 1);
  77. }
  78. if (isDigitCode(charCode)) {
  79. return 0.0;
  80. } else {
  81. return Const.NAN;
  82. }
  83. }
  84. public static inline function random(x:Int):Int {
  85. return x <= 1 ? 0 : Global.mt_rand(0, x - 1);
  86. }
  87. static inline function isDigitCode(charCode:Null<Int>):Bool {
  88. return charCode != null && charCode >= '0'.code && charCode <= '9'.code;
  89. }
  90. }