2
0

StringExt.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C)2005-2018 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. var i = sIndex + 1;
  60. while (i --> 0)
  61. {
  62. var found = true;
  63. for (j in 0...str.Length)
  64. {
  65. if(me[i + j] != str[j])
  66. {
  67. found = false;
  68. break;
  69. }
  70. }
  71. if (found)
  72. return i;
  73. }
  74. return -1;
  75. } else {
  76. return me.LastIndexOf(untyped str, sIndex, cs.system.StringComparison.Ordinal);
  77. }
  78. return -1;
  79. }
  80. public static function split(me:NativeString, delimiter:NativeString):Array<String>
  81. {
  82. var native:NativeArray<String>;
  83. if (delimiter.Length == 0)
  84. {
  85. var len = me.Length;
  86. native = new NativeArray(len);
  87. for (i in 0...len)
  88. native[i] = untyped new NativeString(me[i],1);
  89. } else {
  90. var str = new NativeArray<String>(1);
  91. str[0] = cast delimiter;
  92. native = me.Split(str, cs.system.StringSplitOptions.None);
  93. }
  94. return cs.Lib.array(native);
  95. }
  96. public static function substr(me:NativeString, pos:Int, ?len:Int):String
  97. {
  98. var meLen = me.Length;
  99. var targetLen = meLen;
  100. if (len != null)
  101. {
  102. targetLen = len;
  103. if (targetLen == 0 || (pos != 0 && targetLen < 0))
  104. return "";
  105. }
  106. if (pos < 0)
  107. {
  108. pos = meLen + pos;
  109. if (pos < 0) pos = 0;
  110. } else if (targetLen < 0) {
  111. targetLen = meLen + targetLen - pos;
  112. }
  113. if (pos + targetLen > meLen)
  114. {
  115. targetLen = meLen - pos;
  116. }
  117. if (pos < 0 || targetLen <= 0) return "";
  118. return me.Substring(pos, targetLen);
  119. }
  120. public static function substring(me:NativeString, startIndex:Int, ?endIndex:Int):String
  121. {
  122. var len = me.Length;
  123. var endIdx:Int;
  124. if (endIndex == null)
  125. endIdx = len;
  126. else if ( (endIdx = endIndex) < 0 )
  127. endIdx = 0;
  128. else if (endIdx > len)
  129. endIdx = len;
  130. if (startIndex < 0)
  131. startIndex = 0;
  132. else if (startIndex > len)
  133. startIndex = len;
  134. if (startIndex > endIdx)
  135. {
  136. var tmp = startIndex;
  137. startIndex = endIdx;
  138. endIdx = tmp;
  139. }
  140. return me.Substring(startIndex, endIdx - startIndex);
  141. }
  142. public static function toString(me:NativeString):NativeString
  143. {
  144. return me;
  145. }
  146. public static function toLowerCase(me:NativeString):String
  147. {
  148. return me.ToLowerInvariant();
  149. }
  150. public static function toUpperCase(me:NativeString):String
  151. {
  152. return me.ToUpperInvariant();
  153. }
  154. public static function toNativeString(me:NativeString):NativeString
  155. {
  156. return me;
  157. }
  158. public static function fromCharCode(code:Int):NativeString
  159. {
  160. return new NativeString( cast(code,cs.StdTypes.Char16), 1 );
  161. }
  162. }
  163. @:keep @:nativeGen @:native('haxe.lang.StringRefl') class StringRefl
  164. {
  165. public static var fields = ["length", "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring"];
  166. public static function handleGetField(str:String, f:String, throwErrors:Bool):Dynamic
  167. {
  168. switch(f)
  169. {
  170. case "length": return str.length;
  171. case "toUpperCase", "toLowerCase", "charAt", "charCodeAt", "indexOf", "lastIndexOf", "split", "substr", "substring":
  172. return new Closure(str, f, 0);
  173. default:
  174. if (throwErrors)
  175. throw "Field not found: '" + f + "' in String";
  176. else
  177. return null;
  178. }
  179. }
  180. public static function handleCallField(str:NativeString, f:String, args:cs.NativeArray<Dynamic>):Dynamic
  181. {
  182. var _args:cs.NativeArray<Dynamic>;
  183. if (args == null) {
  184. _args = cs.NativeArray.make(str);
  185. } else {
  186. _args = new cs.NativeArray(args.length + 1);
  187. for (i in 0...args.length)
  188. _args[i + 1] = args[i];
  189. _args[0] = str;
  190. }
  191. return Runtime.slowCallField(StringExt, f, _args);
  192. }
  193. }