String.hx 6.4 KB

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