String.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C)2005-2012 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;
  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 p = try __dollar__sfind(this.__s,if( startIndex == null ) 0 else startIndex,str.__s) catch( e : Dynamic ) null;
  56. if( p == null )
  57. return -1;
  58. return p;
  59. }
  60. }
  61. public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  62. untyped {
  63. var last = -1;
  64. if( startIndex == null )
  65. startIndex = __dollar__ssize(this.__s);
  66. while( true ) {
  67. var p = try __dollar__sfind(this.__s,last+1,str.__s) catch( e : Dynamic ) null;
  68. if( p == null || p > startIndex )
  69. return last;
  70. last = p;
  71. }
  72. return null;
  73. }
  74. }
  75. public function split( delimiter : String ) : Array<String> {
  76. untyped {
  77. var l = __split(this.__s,delimiter.__s);
  78. var a = new Array<String>();
  79. if( l == null ) {
  80. a.push("");
  81. return a;
  82. }
  83. do {
  84. a.push(new String(l[0]));
  85. l = l[1];
  86. } while( l != null );
  87. return a;
  88. }
  89. }
  90. public function substr( pos : Int, ?len : Int ) : String {
  91. if( len == 0 ) return "";
  92. var sl = length;
  93. if( len == null ) len = sl;
  94. if( pos == null ) pos = 0;
  95. if( pos != 0 && len < 0 ){
  96. return "";
  97. }
  98. if( pos < 0 ){
  99. pos = sl + pos;
  100. if( pos < 0 ) pos = 0;
  101. }else if( len < 0 ){
  102. len = sl + len - pos;
  103. }
  104. if( pos + len > sl ){
  105. len = sl - pos;
  106. }
  107. if( pos < 0 || len <= 0 ) return "";
  108. return new String(untyped __dollar__ssub(this.__s,pos,len));
  109. }
  110. public function substring( startIndex : Int, ?endIndex : Int ) : String {
  111. if ( endIndex == null) {
  112. endIndex = length;
  113. } else if ( endIndex < 0 ) {
  114. endIndex = 0;
  115. } else if ( endIndex > length ) {
  116. endIndex = length;
  117. }
  118. if ( startIndex < 0 ) {
  119. startIndex = 0;
  120. } else if ( startIndex > length ) {
  121. startIndex = length;
  122. }
  123. if ( startIndex > endIndex ) {
  124. var tmp = startIndex;
  125. startIndex = endIndex;
  126. endIndex = tmp;
  127. }
  128. return substr( startIndex, endIndex - startIndex );
  129. }
  130. public function toLowerCase() : String {
  131. untyped {
  132. var s = this.__s;
  133. var l = this.length;
  134. var s2 = __dollar__scopy(s);
  135. var i = 0;
  136. while( i < l ) {
  137. var c = __dollar__sget(s,i);
  138. if( c >= 65 && c <= 90 )
  139. __dollar__sset(s2,i,c-65+97);
  140. i++;
  141. }
  142. return new String(s2);
  143. }
  144. }
  145. public function toUpperCase() : String {
  146. untyped {
  147. var s = this.__s;
  148. var l = this.length;
  149. var s2 = __dollar__scopy(s);
  150. var i = 0;
  151. while( i < l ) {
  152. var c = __dollar__sget(s,i);
  153. if( c >= 97 && c <= 122 )
  154. __dollar__sset(s2,i,c-97+65);
  155. i++;
  156. }
  157. return new String(s2);
  158. }
  159. }
  160. public function toString() : String {
  161. return this;
  162. }
  163. /* NEKO INTERNALS */
  164. private function __compare(o:String) : Int {
  165. return untyped __dollar__compare(this.__s,o.__s);
  166. }
  167. private function __add(s:Dynamic) : String {
  168. return new String(untyped this.__s+__dollar__string(s));
  169. }
  170. private function __radd(s:Dynamic) : String {
  171. return new String(untyped __dollar__string(s)+this.__s);
  172. }
  173. public static function fromCharCode( code : Int ) : String untyped {
  174. var s = __dollar__smake(1);
  175. __dollar__sset(s,0,code);
  176. return new String(s);
  177. }
  178. }