StringExt.hx 6.0 KB

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