String.hx 6.6 KB

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