String.hx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. @:coreApi
  23. class String {
  24. var bytes:hl.Bytes;
  25. public var length(default, null):Int;
  26. public function new(string:String):Void {
  27. bytes = string.bytes;
  28. length = string.length;
  29. }
  30. public function toUpperCase():String {
  31. return __alloc__(@:privateAccess bytes.ucs2Upper(0, length), length);
  32. }
  33. public function toLowerCase():String {
  34. return __alloc__(@:privateAccess bytes.ucs2Lower(0, length), length);
  35. }
  36. public function charAt(index:Int):String {
  37. if ((index : UInt) >= (length : UInt))
  38. return "";
  39. var b = new hl.Bytes(4);
  40. b.setUI16(0, bytes.getUI16(index << 1));
  41. b.setUI16(2, 0);
  42. return __alloc__(b, 1);
  43. }
  44. public function charCodeAt(index:Int):Null<Int> {
  45. var idx:UInt = index;
  46. if (idx >= (length : UInt))
  47. return null;
  48. return bytes.getUI16(index << 1);
  49. }
  50. inline function findChar(start:Int, len:Int, src:hl.Bytes, srcLen:Int):Int {
  51. var p = 0;
  52. while (true) {
  53. p = bytes.find(start, len - start, src, 0, srcLen);
  54. if (p < 0 || p & 1 == 0)
  55. break;
  56. start = p + 1;
  57. }
  58. return p;
  59. }
  60. public function indexOf(str:String, ?startIndex:Int):Int {
  61. var startByte = 0;
  62. if (startIndex != null && startIndex > 0) {
  63. if (startIndex >= length)
  64. return str == '' ? length : -1;
  65. startByte = startIndex << 1;
  66. }
  67. var p = findChar(startByte, length << 1, str.bytes, str.length << 1);
  68. if (p > 0)
  69. p >>= 1;
  70. return p;
  71. }
  72. public function lastIndexOf(str:String, ?startIndex:Int):Int {
  73. var max = this.length;
  74. if (startIndex != null) {
  75. max = startIndex + str.length;
  76. if (max < 0)
  77. max = 0;
  78. if (max > this.length)
  79. max = this.length;
  80. }
  81. var pos = max - str.length;
  82. var slen = str.length << 1;
  83. while (pos >= 0) {
  84. if (bytes.compare(pos << 1, str.bytes, 0, slen) == 0)
  85. return pos;
  86. pos--;
  87. }
  88. return -1;
  89. }
  90. public function split(delimiter:String):Array<String> {
  91. var out = [];
  92. if (length == 0) {
  93. out.push("");
  94. return out;
  95. }
  96. if (delimiter.length == 0) {
  97. for (i in 0...length)
  98. out.push(substr(i, 1));
  99. return out;
  100. }
  101. var pos = 0;
  102. var dlen = delimiter.length;
  103. while (true) {
  104. var p = findChar(pos << 1, length << 1, delimiter.bytes, dlen << 1);
  105. if (p < 0) {
  106. out.push(substr(pos, length - pos));
  107. break;
  108. }
  109. p >>= 1;
  110. out.push(substr(pos, p - pos));
  111. pos = p + dlen;
  112. }
  113. return out;
  114. }
  115. public function substr(pos:Int, ?len:Int):String@:privateAccess {
  116. var sl = length;
  117. var len:Int = if (len == null) sl else len;
  118. if (len == 0)
  119. return "";
  120. if (pos != 0 && len < 0)
  121. return "";
  122. if (pos < 0) {
  123. pos = sl + pos;
  124. if (pos < 0)
  125. pos = 0;
  126. } else if (len < 0) {
  127. len = sl + len - pos;
  128. if (len < 0)
  129. return "";
  130. }
  131. if (((pos + len) : UInt) > (sl : UInt))
  132. len = sl - pos;
  133. if (pos < 0 || len <= 0)
  134. return "";
  135. var b = new hl.Bytes((len + 1) << 1);
  136. b.blit(0, bytes, pos << 1, len << 1);
  137. b.setUI16(len << 1, 0);
  138. return __alloc__(b, len);
  139. }
  140. public function substring(startIndex:Int, ?endIndex:Int):String {
  141. var end:Int;
  142. if (endIndex == null)
  143. end = length;
  144. else {
  145. end = endIndex;
  146. if (end < 0)
  147. end = 0;
  148. else if (end > length)
  149. end = length;
  150. }
  151. if (startIndex < 0)
  152. startIndex = 0;
  153. else if (startIndex > length)
  154. startIndex = length;
  155. if (startIndex > end) {
  156. var tmp = startIndex;
  157. startIndex = end;
  158. end = tmp;
  159. }
  160. return substr(startIndex, end - startIndex);
  161. }
  162. public function toString():String {
  163. return this;
  164. }
  165. public static function fromCharCode(code:Int):String {
  166. if (code >= 0 && code < 0x10000) {
  167. if (code >= 0xD800 && code <= 0xDFFF)
  168. throw "Invalid unicode char " + code;
  169. var b = new hl.Bytes(4);
  170. b.setUI16(0, code);
  171. b.setUI16(2, 0);
  172. return __alloc__(b, 1);
  173. } else if (code < 0x110000) {
  174. var b = new hl.Bytes(6);
  175. code -= 0x10000;
  176. b.setUI16(0, (code >> 10) + 0xD800);
  177. b.setUI16(2, (code & 1023) + 0xDC00);
  178. b.setUI16(4, 0);
  179. return __alloc__(b, 2); // UTF16 encoding but UCS2 API (same as JS)
  180. } else
  181. throw "Invalid unicode char " + code;
  182. }
  183. function toUtf8():hl.Bytes {
  184. return bytes.utf16ToUtf8(0, null);
  185. }
  186. @:keep function __string():hl.Bytes {
  187. return bytes;
  188. }
  189. @:keep function __compare(v:Dynamic):Int {
  190. var s = Std.downcast(v, String);
  191. if (s == null)
  192. return hl.Api.comparePointer(this, v);
  193. #if (hl_ver >= version("1.10.0"))
  194. var v = bytes.compare16(s.bytes, length < s.length ? length : s.length);
  195. #else
  196. var v = bytes.compare(0, s.bytes, 0, (length < s.length ? length : s.length) << 1);
  197. #end
  198. return v == 0 ? length - s.length : v;
  199. }
  200. @:keep static inline function __alloc__(b:hl.Bytes, length:Int):String {
  201. var s:String = untyped $new(String);
  202. s.bytes = b;
  203. s.length = length;
  204. return s;
  205. }
  206. @:keep static function call_toString(v:Dynamic):hl.Bytes {
  207. var s:String = v.toString();
  208. return s.bytes;
  209. }
  210. inline static function fromUCS2(b:hl.Bytes):String {
  211. var s:String = untyped $new(String);
  212. s.bytes = b;
  213. s.length = @:privateAccess b.ucs2Length(0);
  214. return s;
  215. }
  216. @:keep static function fromUTF8(b:hl.Bytes):String {
  217. var outLen = 0;
  218. var b2 = @:privateAccess b.utf8ToUtf16(0, outLen);
  219. return __alloc__(b2, outLen >> 1);
  220. }
  221. @:keep static function __add__(a:String, b:String):String {
  222. if (a == null)
  223. a = "null";
  224. if (b == null)
  225. b = "null";
  226. var asize = a.length << 1, bsize = b.length << 1, tot = asize + bsize;
  227. var bytes = new hl.Bytes(tot + 2);
  228. bytes.blit(0, a.bytes, 0, asize);
  229. bytes.blit(asize, b.bytes, 0, bsize);
  230. bytes.setUI16(tot, 0);
  231. return __alloc__(bytes, tot >> 1);
  232. }
  233. }