StringExt.hx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (C)2005-2019 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 java.internal;
  23. import java.internal.Function;
  24. import haxe.iterators.StringIterator;
  25. import haxe.iterators.StringKeyValueIterator;
  26. private typedef NativeString = String;
  27. @:keep @:nativeGen @:native("haxe.lang.StringExt") private class StringExt
  28. {
  29. @:functionCode('
  30. if ( index >= me.length() || index < 0 )
  31. return "";
  32. else
  33. return java.lang.Character.toString(me.charAt(index));
  34. ')
  35. public static function charAt(me:NativeString, index:Int):NativeString
  36. {
  37. return null;
  38. }
  39. @:functionCode('
  40. if ( index >= me.length() || index < 0 )
  41. return null;
  42. else
  43. return (int) me.charAt(index);
  44. ')
  45. public static function charCodeAt(me:NativeString, index:Int):Null<Int>
  46. {
  47. return null;
  48. }
  49. @:functionCode('
  50. int sIndex = (startIndex != null ) ? (haxe.lang.Runtime.toInt(startIndex)) : 0;
  51. if (sIndex >= me.length() || sIndex < 0)
  52. return -1;
  53. return me.indexOf(str, sIndex);
  54. ')
  55. public static function indexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
  56. {
  57. return -1;
  58. }
  59. @:functionCode('
  60. int sIndex = (startIndex != null ) ? (haxe.lang.Runtime.toInt(startIndex)) : (me.length() - 1);
  61. if (sIndex > me.length() || sIndex < 0)
  62. sIndex = me.length() - 1;
  63. else if (sIndex < 0)
  64. return -1;
  65. return me.lastIndexOf(str, sIndex);
  66. ')
  67. public static function lastIndexOf(me:NativeString, str:NativeString, ?startIndex:Int):Int
  68. {
  69. return -1;
  70. }
  71. @:functionCode('
  72. Array<java.lang.String> ret = new Array<java.lang.String>();
  73. int slen = delimiter.length();
  74. if (slen == 0)
  75. {
  76. int len = me.length();
  77. for (int i = 0; i < len; i++)
  78. {
  79. ret.push(me.substring(i, i + 1));
  80. }
  81. } else {
  82. int start = 0;
  83. int pos = me.indexOf(delimiter, start);
  84. while (pos >= 0)
  85. {
  86. ret.push(me.substring(start, pos));
  87. start = pos + slen;
  88. pos = me.indexOf(delimiter, start);
  89. }
  90. ret.push(me.substring(start));
  91. }
  92. return ret;
  93. ')
  94. public static function split(me:NativeString, delimiter:NativeString):Array<NativeString>
  95. {
  96. return null;
  97. }
  98. @:functionCode('
  99. int meLen = me.length();
  100. int targetLen = meLen;
  101. if (len != null)
  102. {
  103. targetLen = haxe.lang.Runtime.toInt(len);
  104. if (targetLen == 0)
  105. return "";
  106. if( pos != 0 && targetLen < 0 ){
  107. return "";
  108. }
  109. }
  110. if( pos < 0 ){
  111. pos = meLen + pos;
  112. if( pos < 0 ) pos = 0;
  113. } else if( targetLen < 0 ){
  114. targetLen = meLen + targetLen - pos;
  115. }
  116. if( pos + targetLen > meLen ){
  117. targetLen = meLen - pos;
  118. }
  119. if ( pos < 0 || targetLen <= 0 ) return "";
  120. return me.substring(pos, pos + targetLen);
  121. ')
  122. public static function substr(me:NativeString, pos:Int, ?len:Int):NativeString
  123. {
  124. return null;
  125. }
  126. @:functionCode('
  127. int endIdx;
  128. int len = me.length();
  129. if ( endIndex == null) {
  130. endIdx = len;
  131. } else if ( (endIdx = haxe.lang.Runtime.toInt(endIndex)) < 0 ) {
  132. endIdx = 0;
  133. } else if ( endIdx > len ) {
  134. endIdx = len;
  135. }
  136. if ( startIndex < 0 ) {
  137. startIndex = 0;
  138. } else if ( startIndex > len ) {
  139. startIndex = len;
  140. }
  141. if ( startIndex > endIdx ) {
  142. int tmp = startIndex;
  143. startIndex = endIdx;
  144. endIdx = tmp;
  145. }
  146. return me.substring(startIndex, endIdx);
  147. ')
  148. public static function substring(me:NativeString, startIndex:Int, ?endIndex:Int):NativeString
  149. {
  150. return null;
  151. }
  152. public static function toString(me:NativeString):NativeString
  153. {
  154. return me;
  155. }
  156. @:functionCode('
  157. return me.toLowerCase();
  158. ')
  159. public static function toLowerCase(me:NativeString):NativeString
  160. {
  161. return null;
  162. }
  163. @:functionCode('
  164. return me.toUpperCase();
  165. ')
  166. public static function toUpperCase(me:NativeString):NativeString
  167. {
  168. return null;
  169. }
  170. public static function toNativeString(me:NativeString):NativeString
  171. {
  172. return me;
  173. }
  174. public static function fromCharCode(code:Int):String
  175. {
  176. return new String(java.lang.Character.toChars(code));
  177. }
  178. public static function iterator(me:NativeString):StringIterator
  179. {
  180. return new StringIterator(me);
  181. }
  182. public static function keyValueIterator(me:NativeString):StringKeyValueIterator
  183. {
  184. return new StringKeyValueIterator(me);
  185. }
  186. }
  187. @:keep @:nativeGen @:native('haxe.lang.StringRefl') private class StringRefl
  188. {
  189. public static var fields = ["length", "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring"];
  190. public static function handleGetField(str:NativeString, f:NativeString, throwErrors:Bool):Dynamic
  191. {
  192. switch(f)
  193. {
  194. case "length": return str.length;
  195. case "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring":
  196. return new Closure(str, f);
  197. default:
  198. if (throwErrors)
  199. throw "Field not found: '" + f + "' in String";
  200. else
  201. return null;
  202. }
  203. }
  204. public static function handleCallField(str:NativeString, f:NativeString, args:java.NativeArray<Dynamic>):Dynamic
  205. {
  206. var _args:java.NativeArray<Dynamic>;
  207. if (args == null) {
  208. _args = java.NativeArray.make(str);
  209. } else {
  210. _args = new java.NativeArray(args.length + 1);
  211. _args[0] = str;
  212. for (i in 0...args.length)
  213. _args[i + 1] = args[i];
  214. }
  215. return Runtime.slowCallField(StringExt, f, _args);
  216. }
  217. }
  218. @:keep @:native('haxe.lang.NativeString') private extern class JavaString
  219. {
  220. //name collides with Haxe's
  221. function _charAt(idx:Int):java.StdTypes.Char16;
  222. function codePointAt(idx:Int):Int;
  223. function codePointBefore(idx:Int):Int;
  224. function codePointCount(begin:Int, end:Int):Int;
  225. function offsetByCodePoints(index:Int, codePointOffset:Int):Int;
  226. function getChars(srcBegin:Int, srcEnd:Int, dst:java.NativeArray<java.StdTypes.Char16>, dstBegin:Int):Void;
  227. function startsWith(prefix:String):Bool;
  228. function endsWith(suffix:String):Bool;
  229. function _indexOf(str:String, fromIndex:Int):Int;
  230. function _lastIndexOf(str:String, fromIndex:Int):Int;
  231. function _substring(begin:Int, end:Int):String;
  232. function replace(old:String, nw:String):String;
  233. function _split(regex:String):java.NativeArray<String>;
  234. function trim():String;
  235. }