2
0

String.hx 7.0 KB

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