String.hx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 = len ?? sl;
  115. if (len == 0)
  116. return "";
  117. if (pos < 0) {
  118. pos = sl + pos;
  119. if (pos < 0)
  120. pos = 0;
  121. }
  122. if (len < 0) {
  123. len = sl + len - pos;
  124. if (len < 0)
  125. return "";
  126. }
  127. if (((pos + len) : UInt) > (sl : UInt))
  128. len = sl - pos;
  129. if (pos < 0 || len <= 0)
  130. return "";
  131. if( len == 1 )
  132. return __char__(bytes.getUI16(pos << 1));
  133. var b = new hl.Bytes((len + 1) << 1);
  134. b.blit(0, bytes, pos << 1, len << 1);
  135. b.setUI16(len << 1, 0);
  136. return __alloc__(b, len);
  137. }
  138. public function substring(startIndex:Int, ?endIndex:Int):String {
  139. var end:Int;
  140. if (endIndex == null)
  141. end = length;
  142. else {
  143. end = endIndex;
  144. if (end < 0)
  145. end = 0;
  146. else if (end > length)
  147. end = length;
  148. }
  149. if (startIndex < 0)
  150. startIndex = 0;
  151. else if (startIndex > length)
  152. startIndex = length;
  153. if (startIndex > end) {
  154. var tmp = startIndex;
  155. startIndex = end;
  156. end = tmp;
  157. }
  158. return substr(startIndex, end - startIndex);
  159. }
  160. public function toString():String {
  161. return this;
  162. }
  163. public static function fromCharCode(code:Int):String {
  164. if (code >= 0 && code < 0x10000) {
  165. if (code >= 0xD800 && code <= 0xDFFF)
  166. throw "Invalid unicode char " + code;
  167. return __char__(code);
  168. } else if (code < 0x110000) {
  169. var b = new hl.Bytes(6);
  170. code -= 0x10000;
  171. b.setUI16(0, (code >> 10) + 0xD800);
  172. b.setUI16(2, (code & 1023) + 0xDC00);
  173. b.setUI16(4, 0);
  174. return __alloc__(b, 2); // UTF16 encoding but UCS2 API (same as JS)
  175. } else
  176. throw "Invalid unicode char " + code;
  177. }
  178. function toUtf8():hl.Bytes {
  179. return bytes.utf16ToUtf8(0, null);
  180. }
  181. @:keep function __string():hl.Bytes {
  182. return bytes;
  183. }
  184. @:keep function __compare(v:Dynamic):Int {
  185. var s = Std.downcast(v, String);
  186. if (s == null)
  187. return hl.Api.comparePointer(this, v);
  188. #if (hl_ver >= version("1.10.0"))
  189. var v = bytes.compare16(s.bytes, length < s.length ? length : s.length);
  190. #else
  191. var v = bytes.compare(0, s.bytes, 0, (length < s.length ? length : s.length) << 1);
  192. #end
  193. return v == 0 ? length - s.length : v;
  194. }
  195. @:keep static inline function __alloc__(b:hl.Bytes, length:Int):String {
  196. var s:String = untyped $new(String);
  197. s.bytes = b;
  198. s.length = length;
  199. return s;
  200. }
  201. @:keep static function call_toString(v:Dynamic):hl.Bytes {
  202. var s:String = v.toString();
  203. return s.bytes;
  204. }
  205. inline static function fromUCS2(b:hl.Bytes):String {
  206. var s:String = untyped $new(String);
  207. s.bytes = b;
  208. s.length = @:privateAccess b.ucs2Length(0);
  209. return s;
  210. }
  211. @:keep static function fromUTF8(b:hl.Bytes):String {
  212. var outLen = 0;
  213. var b2 = @:privateAccess b.utf8ToUtf16(0, outLen);
  214. if( outLen == 2 ) {
  215. var c = b2.getUI16(0);
  216. if( c < 256 ) return __char__(c);
  217. }
  218. return __alloc__(b2, outLen >> 1);
  219. }
  220. static var CHARS = new hl.NativeArray<String>(256);
  221. @:keep static function __char__(code:Int):String {
  222. if( code < 256 ) {
  223. var s = CHARS[code];
  224. if( s == null ) {
  225. var b = new hl.Bytes(4);
  226. b.setUI16(0, code);
  227. b.setUI16(2, 0);
  228. s = __alloc__(b, 1);
  229. CHARS[code] = s;
  230. }
  231. return s;
  232. }
  233. var b = new hl.Bytes(4);
  234. b.setUI16(0, code);
  235. b.setUI16(2, 0);
  236. return __alloc__(b, 1);
  237. }
  238. @:keep static function __add__(a:String, b:String):String {
  239. if (a == null)
  240. a = "null";
  241. if (b == null)
  242. b = "null";
  243. if( a.length == 0 )
  244. return b;
  245. if( b.length == 0 )
  246. return a;
  247. var asize = a.length << 1, bsize = b.length << 1, tot = asize + bsize;
  248. var bytes = new hl.Bytes(tot + 2);
  249. bytes.blit(0, a.bytes, 0, asize);
  250. bytes.blit(asize, b.bytes, 0, bsize);
  251. bytes.setUI16(tot, 0);
  252. return __alloc__(bytes, tot >> 1);
  253. }
  254. }