StringExt.hx 7.0 KB

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