String.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 final class String {
  25. static var __is_String = true;
  26. private static var __split : Dynamic = neko.Lib.load("std","string_split",2);
  27. static function __init__() : Void {
  28. __is_String = true;
  29. }
  30. public var length(default,null) : Int;
  31. public function new(string:String) : Void {
  32. untyped {
  33. if( __dollar__typeof(string) != __dollar__tstring )
  34. string = __dollar__string(string);
  35. this.__s = string;
  36. this.length = __dollar__ssize(string);
  37. }
  38. }
  39. public function charAt(index:Int) : String {
  40. untyped {
  41. try {
  42. var s = __dollar__smake(1);
  43. __dollar__sset(s,0,__dollar__sget(this.__s,index));
  44. return new String(s);
  45. } catch( e : Dynamic ) {
  46. return "";
  47. }
  48. }
  49. }
  50. public function charCodeAt(index : Int) : Null<Int> {
  51. untyped {
  52. return __dollar__sget(this.__s,index);
  53. }
  54. }
  55. public inline function iterator() : StringIterator {
  56. return new StringIterator(this);
  57. }
  58. public inline function keyValueIterator() : StringKeyValueIterator {
  59. return new StringKeyValueIterator(this);
  60. }
  61. public function indexOf( str : String, ?startIndex : Int ) : Int {
  62. untyped {
  63. var l = __dollar__ssize(this.__s);
  64. if( startIndex == null || startIndex < -l )
  65. startIndex = 0;
  66. if( startIndex > l )
  67. return -1;
  68. if( __dollar__ssize(str.__s) == 0 )
  69. return startIndex < 0 ? l + startIndex : startIndex;
  70. var p = try __dollar__sfind(this.__s,startIndex,str.__s) catch( e : Dynamic ) null;
  71. if( p == null )
  72. return -1;
  73. return p;
  74. }
  75. }
  76. public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  77. untyped {
  78. var last = -1;
  79. var l = __dollar__ssize(this.__s);
  80. if( startIndex == null )
  81. startIndex = l;
  82. if( __dollar__ssize(str.__s) == 0 )
  83. return startIndex > l ? l : startIndex;
  84. while( true ) {
  85. var p = try __dollar__sfind(this.__s,last+1,str.__s) catch( e : Dynamic ) null;
  86. if( p == null || p > startIndex )
  87. return last;
  88. last = p;
  89. }
  90. }
  91. }
  92. public function split( delimiter : String ) : Array<String> {
  93. untyped {
  94. var l = __split(this.__s,delimiter.__s);
  95. var a = new Array<String>();
  96. if( l == null ) {
  97. a.push("");
  98. return a;
  99. }
  100. do {
  101. a.push(new String(l[0]));
  102. l = l[1];
  103. } while( l != null );
  104. return a;
  105. }
  106. }
  107. public function substr( pos : Int, ?len : Int ) : String {
  108. if( len == 0 ) return "";
  109. var sl = length;
  110. if( len == null ) len = sl;
  111. if( pos == null ) pos = 0;
  112. if( pos != 0 && len < 0 ){
  113. return "";
  114. }
  115. if( pos < 0 ){
  116. pos = sl + pos;
  117. if( pos < 0 ) pos = 0;
  118. }else if( len < 0 ){
  119. len = sl + len - pos;
  120. }
  121. if( pos + len > sl ){
  122. len = sl - pos;
  123. }
  124. if( pos < 0 || len <= 0 ) return "";
  125. return new String(untyped __dollar__ssub(this.__s,pos,len));
  126. }
  127. public function substring( startIndex : Int, ?endIndex : Int ) : String {
  128. if ( endIndex == null) {
  129. endIndex = length;
  130. } else if ( endIndex < 0 ) {
  131. endIndex = 0;
  132. } else if ( endIndex > length ) {
  133. endIndex = length;
  134. }
  135. if ( startIndex < 0 ) {
  136. startIndex = 0;
  137. } else if ( startIndex > length ) {
  138. startIndex = length;
  139. }
  140. if ( startIndex > endIndex ) {
  141. var tmp = startIndex;
  142. startIndex = endIndex;
  143. endIndex = tmp;
  144. }
  145. return substr( startIndex, endIndex - startIndex );
  146. }
  147. public function toLowerCase() : String {
  148. untyped {
  149. var s = this.__s;
  150. var l = this.length;
  151. var s2 = __dollar__scopy(s);
  152. var i = 0;
  153. while( i < l ) {
  154. var c = __dollar__sget(s,i);
  155. if( c >= 65 && c <= 90 )
  156. __dollar__sset(s2,i,c-65+97);
  157. i++;
  158. }
  159. return new String(s2);
  160. }
  161. }
  162. public function toUpperCase() : String {
  163. untyped {
  164. var s = this.__s;
  165. var l = this.length;
  166. var s2 = __dollar__scopy(s);
  167. var i = 0;
  168. while( i < l ) {
  169. var c = __dollar__sget(s,i);
  170. if( c >= 97 && c <= 122 )
  171. __dollar__sset(s2,i,c-97+65);
  172. i++;
  173. }
  174. return new String(s2);
  175. }
  176. }
  177. public function toString() : String {
  178. return this;
  179. }
  180. /* NEKO INTERNALS */
  181. private function __compare(o:String) : Int {
  182. return untyped __dollar__compare(this.__s,o.__s);
  183. }
  184. private function __add(s:Dynamic) : String {
  185. return new String(untyped this.__s+__dollar__string(s));
  186. }
  187. private function __radd(s:Dynamic) : String {
  188. return new String(untyped __dollar__string(s)+this.__s);
  189. }
  190. public static function fromCharCode( code : Int ) : String untyped {
  191. var s = __dollar__smake(1);
  192. __dollar__sset(s,0,code);
  193. return new String(s);
  194. }
  195. }