StringExt.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. package cs.internal;
  23. import cs.internal.Function;
  24. private typedef NativeString = String;
  25. @:keep @:nativeGen @:native("haxe.lang.StringExt") private class StringExt
  26. {
  27. @:functionCode('
  28. if ( ((uint) index) >= me.Length)
  29. return "";
  30. else
  31. return new string(me[index], 1);
  32. ')
  33. public static function charAt(me:NativeString, index:Int):NativeString
  34. {
  35. return null;
  36. }
  37. public static function charCodeAt(me:NativeString, index:Int):Null<Int>
  38. {
  39. if (cast(index,UInt) >= me.length)
  40. return null;
  41. else
  42. return cast me[index];
  43. }
  44. public static function indexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
  45. {
  46. var sIndex:Int = startIndex != null ? startIndex : 0;
  47. if (sIndex >= me.length)
  48. return -1;
  49. return @:privateAccess me.IndexOf(str, sIndex, cs.system.StringComparison.Ordinal);
  50. }
  51. @:functionCode('
  52. int sIndex = (startIndex.hasValue) ? (startIndex.@value) : (me.Length - 1);
  53. if (sIndex >= me.Length)
  54. sIndex = me.Length - 1;
  55. else if (sIndex < 0)
  56. return -1;
  57. //TestBaseTypes.hx@133 fix
  58. if (startIndex.hasValue)
  59. {
  60. for(int i = sIndex; i >= 0; i--)
  61. {
  62. bool found = true;
  63. for(int j = 0; j < str.Length; j++)
  64. {
  65. if(me[i + j] != str[j])
  66. {
  67. found = false;
  68. break;
  69. }
  70. }
  71. if (found)
  72. return i;
  73. }
  74. return -1;
  75. } else {
  76. return me.LastIndexOf(str, sIndex, System.StringComparison.Ordinal);
  77. }
  78. ')
  79. public static function lastIndexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
  80. {
  81. return -1;
  82. }
  83. @:functionCode('
  84. string[] native;
  85. if (delimiter.Length == 0)
  86. {
  87. int len = me.Length;
  88. native = new string[len];
  89. for (int i = 0; i < len; i++)
  90. native[i] = new string(me[i], 1);
  91. } else {
  92. native = me.Split(new string[] { delimiter }, System.StringSplitOptions.None);
  93. }
  94. return new Array<object>(native);
  95. ')
  96. public static function split(me:NativeString, delimiter:NativeString):Array<NativeString>
  97. {
  98. return null;
  99. }
  100. @:functionCode('
  101. int meLen = me.Length;
  102. int targetLen = meLen;
  103. if (len.hasValue)
  104. {
  105. targetLen = len.@value;
  106. if (targetLen == 0)
  107. return "";
  108. if( pos != 0 && targetLen < 0 ){
  109. return "";
  110. }
  111. }
  112. if( pos < 0 ){
  113. pos = meLen + pos;
  114. if( pos < 0 ) pos = 0;
  115. } else if( targetLen < 0 ){
  116. targetLen = meLen + targetLen - pos;
  117. }
  118. if( pos + targetLen > meLen ){
  119. targetLen = meLen - pos;
  120. }
  121. if ( pos < 0 || targetLen <= 0 ) return "";
  122. return me.Substring(pos, targetLen);
  123. ')
  124. public static function substr(me:NativeString, pos:Int, ?len:Int):NativeString
  125. {
  126. return null;
  127. }
  128. @:functionCode('
  129. int endIdx;
  130. int len = me.Length;
  131. if ( !endIndex.hasValue ) {
  132. endIdx = len;
  133. } else if ( (endIdx = endIndex.@value) < 0 ) {
  134. endIdx = 0;
  135. } else if ( endIdx > len ) {
  136. endIdx = len;
  137. }
  138. if ( startIndex < 0 ) {
  139. startIndex = 0;
  140. } else if ( startIndex > len ) {
  141. startIndex = len;
  142. }
  143. if ( startIndex > endIdx ) {
  144. int tmp = startIndex;
  145. startIndex = endIdx;
  146. endIdx = tmp;
  147. }
  148. return me.Substring(startIndex, endIdx - startIndex);
  149. ')
  150. public static function substring(me:NativeString, startIndex:Int, ?endIndex:Int):NativeString
  151. {
  152. return null;
  153. }
  154. public static function toString(me:NativeString):NativeString
  155. {
  156. return me;
  157. }
  158. @:functionCode('
  159. return me.ToLowerInvariant();
  160. ')
  161. public static function toLowerCase(me:NativeString):NativeString
  162. {
  163. return null;
  164. }
  165. @:functionCode('
  166. return me.ToUpperInvariant();
  167. ')
  168. public static function toUpperCase(me:NativeString):NativeString
  169. {
  170. return null;
  171. }
  172. public static function toNativeString(me:NativeString):NativeString
  173. {
  174. return me;
  175. }
  176. @:functionCode('
  177. return new string( (char) code, 1 );
  178. ')
  179. public static function fromCharCode(code:Int):NativeString
  180. {
  181. return null;
  182. }
  183. }
  184. @:keep @:nativeGen @:native('haxe.lang.StringRefl') class StringRefl
  185. {
  186. public static var fields = ["length", "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring"];
  187. public static function handleGetField(str:NativeString, f:NativeString, throwErrors:Bool):Dynamic
  188. {
  189. switch(f)
  190. {
  191. case "length": return str.length;
  192. case "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring":
  193. return new Closure(str, f, 0);
  194. default:
  195. if (throwErrors)
  196. throw "Field not found: '" + f + "' in String";
  197. else
  198. return null;
  199. }
  200. }
  201. public static function handleCallField(str:NativeString, f:NativeString, args:Array<Dynamic>):Dynamic
  202. {
  203. var _args:Array<Dynamic> = [str];
  204. if (args == null)
  205. args = _args;
  206. else
  207. args = _args.concat(args);
  208. return Runtime.slowCallField(StringExt, f, args);
  209. }
  210. }