2
0

StringExt.hx 6.7 KB

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