Std.hx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import php.Boot;
  2. /*
  3. * Copyright (C)2005-2018 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 Boot.is(v, t);
  29. }
  30. public static inline function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  31. return Boot.is(value, cast c) ? cast value : null;
  32. }
  33. public static function string( s : Dynamic ) : String {
  34. return Boot.stringify(s);
  35. }
  36. public static inline function int( x : Float ) : Int {
  37. return Syntax.int(x);
  38. }
  39. public static function parseInt( x : String ) : Null<Int> {
  40. if (Global.is_numeric(x)) {
  41. return Global.intval(x, 10);
  42. } else {
  43. x = Global.ltrim(x);
  44. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  45. var firstCharCode = x.charCodeAt(firstCharIndex);
  46. if (!isDigitCode(firstCharCode)) {
  47. return null;
  48. }
  49. var secondChar = x.charAt(firstCharIndex + 1);
  50. if (secondChar == 'x' || secondChar == 'X') {
  51. return Global.intval(x, 0);
  52. } else {
  53. return Global.intval(x, 10);
  54. }
  55. }
  56. }
  57. public static function parseFloat( x : String ) : Float {
  58. var result = Global.floatval(x);
  59. if (result != 0) return result;
  60. x = Global.ltrim(x);
  61. var firstCharIndex = (x.charAt(0) == '-' ? 1 : 0);
  62. var charCode = x.charCodeAt(firstCharIndex);
  63. if (charCode == '.'.code) {
  64. charCode = x.charCodeAt(firstCharIndex + 1);
  65. }
  66. if (isDigitCode(charCode)) {
  67. return 0.0;
  68. } else {
  69. return Const.NAN;
  70. }
  71. }
  72. public static inline function random( x : Int ) : Int {
  73. return x <= 1 ? 0 : Global.mt_rand(0, x - 1);
  74. }
  75. static inline function isDigitCode( charCode:Null<Int> ) : Bool {
  76. return charCode != null && charCode >= '0'.code && charCode <= '9'.code;
  77. }
  78. }