String.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api @:final class String {
  26. static var __is_String;
  27. private static var __split : Dynamic = neko.Lib.load("std","string_split",2);
  28. static function __init__() : Void {
  29. __is_String = true;
  30. }
  31. public var length(default,null) : Int;
  32. public function new(s:String) : Void {
  33. untyped {
  34. if( __dollar__typeof(s) != __dollar__tstring )
  35. s = __dollar__string(s);
  36. this.__s = s;
  37. this.length = __dollar__ssize(s);
  38. }
  39. }
  40. public function charAt(index:Int) : String {
  41. untyped {
  42. try {
  43. var s = __dollar__smake(1);
  44. __dollar__sset(s,0,__dollar__sget(this.__s,index));
  45. return new String(s);
  46. } catch( e : Dynamic ) {
  47. return "";
  48. }
  49. }
  50. }
  51. public function charCodeAt(index : Int) : Null<Int> {
  52. untyped {
  53. return __dollar__sget(this.__s,index);
  54. }
  55. }
  56. public function indexOf( str : String, ?startIndex : Int ) : Int {
  57. untyped {
  58. var p = try __dollar__sfind(this.__s,if( startIndex == null ) 0 else startIndex,str.__s) catch( e : Dynamic ) null;
  59. if( p == null )
  60. return -1;
  61. return p;
  62. }
  63. }
  64. public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
  65. untyped {
  66. var last = -1;
  67. if( startIndex == null )
  68. startIndex = __dollar__ssize(this.__s);
  69. while( true ) {
  70. var p = try __dollar__sfind(this.__s,last+1,str.__s) catch( e : Dynamic ) null;
  71. if( p == null || p > startIndex )
  72. return last;
  73. last = p;
  74. }
  75. return null;
  76. }
  77. }
  78. public function split( delimiter : String ) : Array<String> {
  79. untyped {
  80. var l = __split(this.__s,delimiter.__s);
  81. var a = new Array<String>();
  82. if( l == null ) {
  83. a.push("");
  84. return a;
  85. }
  86. do {
  87. a.push(new String(l[0]));
  88. l = l[1];
  89. } while( l != null );
  90. return a;
  91. }
  92. }
  93. public function substr( pos : Int, ?len : Int ) : String {
  94. if( len == 0 ) return "";
  95. var sl = length;
  96. if( len == null ) len = sl;
  97. if( pos == null ) pos = 0;
  98. if( pos != 0 && len < 0 ){
  99. return "";
  100. }
  101. if( pos < 0 ){
  102. pos = sl + pos;
  103. if( pos < 0 ) pos = 0;
  104. }else if( len < 0 ){
  105. len = sl + len - pos;
  106. }
  107. if( pos + len > sl ){
  108. len = sl - pos;
  109. }
  110. if( pos < 0 || len <= 0 ) return "";
  111. return new String(untyped __dollar__ssub(this.__s,pos,len));
  112. }
  113. public function toLowerCase() : String {
  114. untyped {
  115. var s = this.__s;
  116. var l = this.length;
  117. var s2 = __dollar__scopy(s);
  118. var i = 0;
  119. while( i < l ) {
  120. var c = __dollar__sget(s,i);
  121. if( c >= 65 && c <= 90 )
  122. __dollar__sset(s2,i,c-65+97);
  123. i++;
  124. }
  125. return new String(s2);
  126. }
  127. }
  128. public function toUpperCase() : String {
  129. untyped {
  130. var s = this.__s;
  131. var l = this.length;
  132. var s2 = __dollar__scopy(s);
  133. var i = 0;
  134. while( i < l ) {
  135. var c = __dollar__sget(s,i);
  136. if( c >= 97 && c <= 122 )
  137. __dollar__sset(s2,i,c-97+65);
  138. i++;
  139. }
  140. return new String(s2);
  141. }
  142. }
  143. public function toString() : String {
  144. return this;
  145. }
  146. /* NEKO INTERNALS */
  147. private function __compare(o:String) : Int {
  148. return untyped __dollar__compare(this.__s,o.__s);
  149. }
  150. private function __add(s:Dynamic) : String {
  151. return new String(untyped this.__s+__dollar__string(s));
  152. }
  153. private function __radd(s:Dynamic) : String {
  154. return new String(untyped __dollar__string(s)+this.__s);
  155. }
  156. public static function fromCharCode( code : Int ) : String untyped {
  157. var s = __dollar__smake(1);
  158. __dollar__sset(s,0,code);
  159. return new String(s);
  160. }
  161. }