String.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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( ((pos + len) : UInt) > (sl:UInt) )
  111. len = sl - pos;
  112. if( pos < 0 || len <= 0 ) return "";
  113. var b = new hl.Bytes((len + 1) << 1);
  114. b.blit(0, bytes, pos<<1, len << 1);
  115. b.setUI16(len<<1,0);
  116. return __alloc__(b, len);
  117. }
  118. public function substring( startIndex : Int, ?endIndex : Int ) : String {
  119. var end : Int;
  120. if( endIndex == null )
  121. end = length;
  122. else {
  123. end = endIndex;
  124. if( end < 0 )
  125. end = 0;
  126. else if ( end > length )
  127. end = length;
  128. }
  129. if( startIndex < 0 )
  130. startIndex = 0;
  131. else if ( startIndex > length )
  132. startIndex = length;
  133. if( startIndex > end ) {
  134. var tmp = startIndex;
  135. startIndex = end;
  136. end = tmp;
  137. }
  138. return substr( startIndex, end - startIndex );
  139. }
  140. public function toString() : String {
  141. return this;
  142. }
  143. public static function fromCharCode( code : Int ) : String {
  144. if( code >= 0 && code < 0x10000 ) {
  145. if( code >= 0xD800 && code <= 0xDFFF ) throw "Invalid unicode char " + code;
  146. var b = new hl.Bytes(4);
  147. b.setUI16(0, code);
  148. b.setUI16(2, 0);
  149. return __alloc__(b, 1);
  150. } else if( code < 0x110000 ) {
  151. var b = new hl.Bytes(6);
  152. code -= 0x10000;
  153. b.setUI16(0, (code >> 10) + 0xD800);
  154. b.setUI16(2, (code & 1023) + 0xDC00);
  155. b.setUI16(4, 0);
  156. return __alloc__(b, 2); // UTF16 encoding but UCS2 API (same as JS)
  157. } else
  158. throw "Invalid unicode char " + code;
  159. }
  160. function toUtf8() : hl.Bytes {
  161. return bytes.utf16ToUtf8(0, null);
  162. }
  163. @:keep function __string() : hl.Bytes {
  164. return bytes;
  165. }
  166. @:keep function __compare( s : String ) : Int {
  167. var v = bytes.compare(0, s.bytes, 0, (length < s.length ? length : s.length) << 1);
  168. return v == 0 ? length - s.length : v;
  169. }
  170. @:keep static inline function __alloc__( b : hl.Bytes, length : Int ) : String {
  171. var s : String = untyped $new(String);
  172. s.bytes = b;
  173. s.length = length;
  174. return s;
  175. }
  176. @:keep static function call_toString( v : Dynamic ) : hl.Bytes {
  177. var s : String = v.toString();
  178. return s.bytes;
  179. }
  180. inline static function fromUCS2( b : hl.Bytes ) : String {
  181. var s : String = untyped $new(String);
  182. s.bytes = b;
  183. s.length = @:privateAccess b.ucs2Length(0);
  184. return s;
  185. }
  186. @:keep static function fromUTF8( b : hl.Bytes ) : String {
  187. var outLen = 0;
  188. var b2 = @:privateAccess b.utf8ToUtf16(0, outLen);
  189. return __alloc__(b2, outLen>>1);
  190. }
  191. @:keep static function __add__( a : String, b : String ) : String {
  192. if( a == null ) a = "null";
  193. if( b == null ) b = "null";
  194. var asize = a.length << 1, bsize = b.length << 1, tot = asize + bsize;
  195. var bytes = new hl.Bytes(tot+2);
  196. bytes.blit(0, a.bytes, 0, asize);
  197. bytes.blit(asize,b.bytes,0,bsize);
  198. bytes.setUI16(tot, 0);
  199. return __alloc__(bytes, tot>>1);
  200. }
  201. }