StringExt.hx 5.9 KB

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