Std.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if (Global.is_numeric(x)) {
  49. return Global.intval(x, 10);
  50. } else {
  51. x = Global.ltrim(x);
  52. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  53. var firstCharCode = x.charCodeAt(firstCharIndex);
  54. if (!isDigitCode(firstCharCode)) {
  55. return null;
  56. }
  57. var secondChar = x.charAt(firstCharIndex + 1);
  58. if (secondChar == 'x' || secondChar == 'X') {
  59. return Global.intval(x, 0);
  60. } else {
  61. return Global.intval(x, 10);
  62. }
  63. }
  64. }
  65. public static function parseFloat(x:String):Float {
  66. var result = Global.floatval(x);
  67. if (result != 0)
  68. return result;
  69. x = Global.ltrim(x);
  70. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  71. var charCode = x.charCodeAt(firstCharIndex);
  72. if (charCode == '.'.code) {
  73. charCode = x.charCodeAt(firstCharIndex + 1);
  74. }
  75. if (isDigitCode(charCode)) {
  76. return 0.0;
  77. } else {
  78. return Const.NAN;
  79. }
  80. }
  81. public static inline function random(x:Int):Int {
  82. return x <= 1 ? 0 : Global.mt_rand(0, x - 1);
  83. }
  84. static inline function isDigitCode(charCode:Null<Int>):Bool {
  85. return charCode != null && charCode >= '0'.code && charCode <= '9'.code;
  86. }
  87. }