Std.hx 2.8 KB

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