String.hx 6.1 KB

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