HxOverrides.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2012, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /*
  26. * Static methods that override calls to built-in JS methods.
  27. */
  28. @:noDoc
  29. class HxOverrides {
  30. static function dateStr( date :Date ) : String {
  31. var m = date.getMonth() + 1;
  32. var d = date.getDate();
  33. var h = date.getHours();
  34. var mi = date.getMinutes();
  35. var s = date.getSeconds();
  36. return date.getFullYear()
  37. +"-"+(if( m < 10 ) "0"+m else ""+m)
  38. +"-"+(if( d < 10 ) "0"+d else ""+d)
  39. +" "+(if( h < 10 ) "0"+h else ""+h)
  40. +":"+(if( mi < 10 ) "0"+mi else ""+mi)
  41. +":"+(if( s < 10 ) "0"+s else ""+s);
  42. }
  43. static function strDate( s : String ) : Date {
  44. switch( s.length ) {
  45. case 8: // hh:mm:ss
  46. var k = s.split(":");
  47. var d : Date = untyped __new__(Date);
  48. untyped d["setTime"](0);
  49. untyped d["setUTCHours"](k[0]);
  50. untyped d["setUTCMinutes"](k[1]);
  51. untyped d["setUTCSeconds"](k[2]);
  52. return d;
  53. case 10: // YYYY-MM-DD
  54. var k = s.split("-");
  55. return new Date(cast k[0],cast untyped k[1] - 1,cast k[2],0,0,0);
  56. case 19: // YYYY-MM-DD hh:mm:ss
  57. var k = s.split(" ");
  58. var y = k[0].split("-");
  59. var t = k[1].split(":");
  60. return new Date(cast y[0],cast untyped y[1] - 1,cast y[2],cast t[0],cast t[1],cast t[2]);
  61. default:
  62. throw "Invalid date format : " + s;
  63. }
  64. }
  65. static function cca( s : String, index : Int ) : Null<Int> {
  66. #if mt
  67. var x = (cast s).cca(index);
  68. #else
  69. var x = (cast s).charCodeAt(index);
  70. #end
  71. if( x != x ) // fast isNaN
  72. return untyped undefined; // isNaN will still return true
  73. return x;
  74. }
  75. static function substr( s : String, pos : Int, ?len : Int ) : String {
  76. if( pos != null && pos != 0 && len != null && len < 0 ) return "";
  77. if( len == null ) len = s.length;
  78. if( pos < 0 ){
  79. pos = s.length + pos;
  80. if( pos < 0 ) pos = 0;
  81. }else if( len < 0 ){
  82. len = s.length + len - pos;
  83. }
  84. return (untyped s).substr(pos, len);
  85. }
  86. static function remove<T>( a : Array<T>, obj : T ) {
  87. var i = 0;
  88. var l = a.length;
  89. while( i < l ) {
  90. if( a[i] == obj ) {
  91. a.splice(i,1);
  92. return true;
  93. }
  94. i++;
  95. }
  96. return false;
  97. }
  98. static function iter<T>( a : Array<T> ) : Iterator<T> untyped {
  99. return {
  100. cur : 0,
  101. arr : a,
  102. hasNext : function() {
  103. return __this__.cur < __this__.arr.length;
  104. },
  105. next : function() {
  106. return __this__.arr[__this__.cur++];
  107. }
  108. };
  109. }
  110. static function __init__() untyped {
  111. __feature__('HxOverrides.remove',
  112. if( Array.prototype.indexOf ) __js__('HxOverrides').remove = function(a,o) {
  113. var i = a.indexOf(o);
  114. if( i == -1 ) return false;
  115. a.splice(i,1);
  116. return true;
  117. }
  118. );
  119. #if mt
  120. if( String.prototype.cca == null ) String.prototype.cca = String.prototype.charCodeAt;
  121. #end
  122. }
  123. }