String.hx 5.2 KB

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