String.hx 6.9 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. import haxe.iterators.StringIterator;
  23. import haxe.iterators.StringKeyValueIterator;
  24. @:coreApi
  25. class String {
  26. var bytes : hl.Bytes;
  27. public var length(default,null) : Int;
  28. public function new(string:String) : Void {
  29. bytes = string.bytes;
  30. length = string.length;
  31. }
  32. public function toUpperCase() : String {
  33. return __alloc__(@:privateAccess bytes.ucs2Upper(0,length), length);
  34. }
  35. public function toLowerCase() : String {
  36. return __alloc__(@:privateAccess bytes.ucs2Lower(0,length), length);
  37. }
  38. public function charAt(index : Int) : String {
  39. if( (index:UInt) >= (length:UInt) ) return "";
  40. var b = new hl.Bytes(4);
  41. b.setUI16(0, bytes.getUI16(index<<1));
  42. b.setUI16(2,0);
  43. return __alloc__(b,1);
  44. }
  45. public function charCodeAt( index : Int) : Null<Int> {
  46. var idx : UInt = index;
  47. if( idx >= (length:UInt) )
  48. return null;
  49. return bytes.getUI16(index << 1);
  50. }
  51. inline function findChar(start:Int,len:Int,src:hl.Bytes,srcLen:Int) : Int {
  52. var p = 0;
  53. while( true ) {
  54. p = bytes.find(start,len-start,src,0,srcLen);
  55. if( p < 0 || p & 1 == 0 ) 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 -1;
  65. startByte = startIndex << 1;
  66. }
  67. var p = findChar(startByte, length << 1, str.bytes, str.length << 1);
  68. if( p > 0 ) p >>= 1;
  69. return p;
  70. }
  71. public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  72. var max = this.length;
  73. if( startIndex != null ) {
  74. max = startIndex + str.length;
  75. if( max < 0 ) max = 0;
  76. if( max > this.length ) 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 ) return "";
  116. if( pos != 0 && len < 0 )
  117. return "";
  118. if( pos < 0 ) {
  119. pos = sl + pos;
  120. if( pos < 0 ) pos = 0;
  121. } else if( len < 0 ) {
  122. len = sl + len - pos;
  123. if( len < 0 ) return "";
  124. }
  125. if( ((pos + len) : UInt) > (sl:UInt) )
  126. len = sl - pos;
  127. if( pos < 0 || len <= 0 ) return "";
  128. var b = new hl.Bytes((len + 1) << 1);
  129. b.blit(0, bytes, pos<<1, len << 1);
  130. b.setUI16(len<<1,0);
  131. return __alloc__(b, len);
  132. }
  133. public function substring( startIndex : Int, ?endIndex : Int ) : String {
  134. var end : Int;
  135. if( endIndex == null )
  136. end = length;
  137. else {
  138. end = endIndex;
  139. if( end < 0 )
  140. end = 0;
  141. else if ( end > length )
  142. end = length;
  143. }
  144. if( startIndex < 0 )
  145. startIndex = 0;
  146. else if ( startIndex > length )
  147. startIndex = length;
  148. if( startIndex > end ) {
  149. var tmp = startIndex;
  150. startIndex = end;
  151. end = tmp;
  152. }
  153. return substr( startIndex, end - startIndex );
  154. }
  155. public function toString() : String {
  156. return this;
  157. }
  158. public inline function iterator() : StringIterator {
  159. return new StringIterator(this);
  160. }
  161. public inline function keyValueIterator() : StringKeyValueIterator {
  162. return new StringKeyValueIterator(this);
  163. }
  164. public static function fromCharCode( code : Int ) : String {
  165. if( code >= 0 && code < 0x10000 ) {
  166. if( code >= 0xD800 && code <= 0xDFFF ) throw "Invalid unicode char " + code;
  167. var b = new hl.Bytes(4);
  168. b.setUI16(0, code);
  169. b.setUI16(2, 0);
  170. return __alloc__(b, 1);
  171. } else if( code < 0x110000 ) {
  172. var b = new hl.Bytes(6);
  173. code -= 0x10000;
  174. b.setUI16(0, (code >> 10) + 0xD800);
  175. b.setUI16(2, (code & 1023) + 0xDC00);
  176. b.setUI16(4, 0);
  177. return __alloc__(b, 2); // UTF16 encoding but UCS2 API (same as JS)
  178. } else
  179. throw "Invalid unicode char " + code;
  180. }
  181. function toUtf8() : hl.Bytes {
  182. return bytes.utf16ToUtf8(0, null);
  183. }
  184. @:keep function __string() : hl.Bytes {
  185. return bytes;
  186. }
  187. @:keep function __compare( v : Dynamic ) : Int {
  188. var s = Std.instance(v, String);
  189. if( s == null )
  190. return hl.Api.comparePointer(this, v);
  191. #if (hl_ver >= version("1.10"))
  192. var v = bytes.compare16(s.bytes, length < s.length ? length : s.length);
  193. #else
  194. var v = bytes.compare(0, s.bytes, 0, (length < s.length ? length : s.length) << 1);
  195. #end
  196. return v == 0 ? length - s.length : v;
  197. }
  198. @:keep static inline function __alloc__( b : hl.Bytes, length : Int ) : String {
  199. var s : String = untyped $new(String);
  200. s.bytes = b;
  201. s.length = length;
  202. return s;
  203. }
  204. @:keep static function call_toString( v : Dynamic ) : hl.Bytes {
  205. var s : String = v.toString();
  206. return s.bytes;
  207. }
  208. inline static function fromUCS2( b : hl.Bytes ) : String {
  209. var s : String = untyped $new(String);
  210. s.bytes = b;
  211. s.length = @:privateAccess b.ucs2Length(0);
  212. return s;
  213. }
  214. @:keep static function fromUTF8( b : hl.Bytes ) : String {
  215. var outLen = 0;
  216. var b2 = @:privateAccess b.utf8ToUtf16(0, outLen);
  217. return __alloc__(b2, outLen>>1);
  218. }
  219. @:keep static function __add__( a : String, b : String ) : String {
  220. if( a == null ) a = "null";
  221. if( b == null ) b = "null";
  222. var asize = a.length << 1, bsize = b.length << 1, tot = asize + bsize;
  223. var bytes = new hl.Bytes(tot+2);
  224. bytes.blit(0, a.bytes, 0, asize);
  225. bytes.blit(asize,b.bytes,0,bsize);
  226. bytes.setUI16(tot, 0);
  227. return __alloc__(bytes, tot>>1);
  228. }
  229. }