StringExt.hx 6.7 KB

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