123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- /*
- * Copyright (c) 2005, The haXe Project Contributors
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- */
- @:core_api @:final class String {
- static var __is_String;
- private static var __split : Dynamic = neko.Lib.load("std","string_split",2);
- static function __init__() : Void {
- __is_String = true;
- }
- public var length(default,null) : Int;
- public function new(s:String) : Void {
- untyped {
- if( __dollar__typeof(s) != __dollar__tstring )
- s = __dollar__string(s);
- this.__s = s;
- this.length = __dollar__ssize(s);
- }
- }
- public function charAt(index:Int) : String {
- untyped {
- try {
- var s = __dollar__smake(1);
- __dollar__sset(s,0,__dollar__sget(this.__s,index));
- return new String(s);
- } catch( e : Dynamic ) {
- return "";
- }
- }
- }
- public function charCodeAt(index : Int) : Null<Int> {
- untyped {
- return __dollar__sget(this.__s,index);
- }
- }
- public function indexOf( str : String, ?startIndex : Int ) : Int {
- untyped {
- var p = try __dollar__sfind(this.__s,if( startIndex == null ) 0 else startIndex,str.__s) catch( e : Dynamic ) null;
- if( p == null )
- return -1;
- return p;
- }
- }
- public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
- untyped {
- var last = -1;
- if( startIndex == null )
- startIndex = __dollar__ssize(this.__s);
- while( true ) {
- var p = try __dollar__sfind(this.__s,last+1,str.__s) catch( e : Dynamic ) null;
- if( p == null || p > startIndex )
- return last;
- last = p;
- }
- return null;
- }
- }
- public function split( delimiter : String ) : Array<String> {
- untyped {
- var l = __split(this.__s,delimiter.__s);
- var a = new Array<String>();
- if( l == null ) {
- a.push("");
- return a;
- }
- do {
- a.push(new String(l[0]));
- l = l[1];
- } while( l != null );
- return a;
- }
- }
- public function substr( pos : Int, ?len : Int ) : String {
- if( len == 0 ) return "";
- var sl = length;
- if( len == null ) len = sl;
- if( pos == null ) pos = 0;
- if( pos != 0 && len < 0 ){
- return "";
- }
- if( pos < 0 ){
- pos = sl + pos;
- if( pos < 0 ) pos = 0;
- }else if( len < 0 ){
- len = sl + len - pos;
- }
- if( pos + len > sl ){
- len = sl - pos;
- }
- if( pos < 0 || len <= 0 ) return "";
- return new String(untyped __dollar__ssub(this.__s,pos,len));
- }
- public function toLowerCase() : String {
- untyped {
- var s = this.__s;
- var l = this.length;
- var s2 = __dollar__scopy(s);
- var i = 0;
- while( i < l ) {
- var c = __dollar__sget(s,i);
- if( c >= 65 && c <= 90 )
- __dollar__sset(s2,i,c-65+97);
- i++;
- }
- return new String(s2);
- }
- }
- public function toUpperCase() : String {
- untyped {
- var s = this.__s;
- var l = this.length;
- var s2 = __dollar__scopy(s);
- var i = 0;
- while( i < l ) {
- var c = __dollar__sget(s,i);
- if( c >= 97 && c <= 122 )
- __dollar__sset(s2,i,c-97+65);
- i++;
- }
- return new String(s2);
- }
- }
- public function toString() : String {
- return this;
- }
- /* NEKO INTERNALS */
- private function __compare(o:String) : Int {
- return untyped __dollar__compare(this.__s,o.__s);
- }
- private function __add(s:Dynamic) : String {
- return new String(untyped this.__s+__dollar__string(s));
- }
- private function __radd(s:Dynamic) : String {
- return new String(untyped __dollar__string(s)+this.__s);
- }
- public static function fromCharCode( code : Int ) : String untyped {
- var s = __dollar__smake(1);
- __dollar__sset(s,0,code);
- return new String(s);
- }
- }
|