StringExt.hx 6.3 KB

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