Std.hx 2.9 KB

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