HxOverrides.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. @:noDoc
  23. class HxOverrides {
  24. static function dateStr( date :Date ) : String {
  25. var m = date.getMonth() + 1;
  26. var d = date.getDate();
  27. var h = date.getHours();
  28. var mi = date.getMinutes();
  29. var s = date.getSeconds();
  30. return date.getFullYear()
  31. +"-"+(if( m < 10 ) "0"+m else ""+m)
  32. +"-"+(if( d < 10 ) "0"+d else ""+d)
  33. +" "+(if( h < 10 ) "0"+h else ""+h)
  34. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  35. +":"+(if( s < 10 ) "0"+s else ""+s);
  36. }
  37. static function strDate( s : String ) : Date {
  38. switch( s.length ) {
  39. case 8: // hh:mm:ss
  40. var k = s.split(":");
  41. var d : Date = untyped __new__(Date);
  42. untyped d["setTime"](0);
  43. untyped d["setUTCHours"](k[0]);
  44. untyped d["setUTCMinutes"](k[1]);
  45. untyped d["setUTCSeconds"](k[2]);
  46. return d;
  47. case 10: // YYYY-MM-DD
  48. var k = s.split("-");
  49. return new Date(cast k[0],cast untyped k[1] - 1,cast k[2],0,0,0);
  50. case 19: // YYYY-MM-DD hh:mm:ss
  51. var k = s.split(" ");
  52. var y = k[0].split("-");
  53. var t = k[1].split(":");
  54. return new Date(cast y[0],cast untyped y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
  55. default:
  56. throw "Invalid date format : " + s;
  57. }
  58. }
  59. static function cca( s : String, index : Int ) : Null<Int> {
  60. #if mt
  61. var x = (cast s).cca(index);
  62. #else
  63. var x = (cast s).charCodeAt(index);
  64. #end
  65. if( x != x ) // fast isNaN
  66. return untyped undefined; // isNaN will still return true
  67. return x;
  68. }
  69. static function substr( s : String, pos : Int, ?len : Int ) : String {
  70. if( pos != null && pos != 0 && len != null && len < 0 ) return "";
  71. if( len == null ) len = s.length;
  72. if( pos < 0 ){
  73. pos = s.length + pos;
  74. if( pos < 0 ) pos = 0;
  75. }else if( len < 0 ){
  76. len = s.length + len - pos;
  77. }
  78. return (untyped s).substr(pos, len);
  79. }
  80. static function remove<T>( a : Array<T>, obj : T ) {
  81. var i = 0;
  82. var l = a.length;
  83. while( i < l ) {
  84. if( a[i] == obj ) {
  85. a.splice(i,1);
  86. return true;
  87. }
  88. i++;
  89. }
  90. return false;
  91. }
  92. static function iter<T>( a : Array<T> ) : Iterator<T> untyped {
  93. return {
  94. cur : 0,
  95. arr : a,
  96. hasNext : function() {
  97. return __this__.cur < __this__.arr.length;
  98. },
  99. next : function() {
  100. return __this__.arr[__this__.cur++];
  101. }
  102. };
  103. }
  104. static function __init__() untyped {
  105. __feature__('HxOverrides.remove',
  106. if( Array.prototype.indexOf ) __js__('HxOverrides').remove = function(a,o) {
  107. var i = a.indexOf(o);
  108. if( i == -1 ) return false;
  109. a.splice(i,1);
  110. return true;
  111. }
  112. );
  113. #if mt
  114. if( String.prototype.cca == null ) String.prototype.cca = String.prototype.charCodeAt;
  115. #end
  116. }
  117. }