2
0

StringExt.hx 6.2 KB

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