1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
/*
* Copyright (C)2005-2017 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import lua.Lua;
import lua.Table;
import lua.Boot;
import lua.NativeStringTools;
@:coreApi
class String {
static var __oldindex : Table<Dynamic,Dynamic>;
public var length(default,null) : Int;
public function new(string:String) untyped {}
@:keep
static function __index(s:Dynamic, k:Dynamic) : Dynamic {
if (k == "length") return NativeStringTools.len(s);
else if (Reflect.hasField(untyped String.prototype, k)) return untyped String.prototype[k];
else if (__oldindex != null) return __oldindex[k];
else return null;
}
public function toUpperCase() : String return NativeStringTools.upper(this);
public function toLowerCase() : String return NativeStringTools.lower(this);
public function indexOf( str : String, ?startIndex : Int ) : Int {
if (startIndex == null) startIndex = 1;
else startIndex += 1;
var r = NativeStringTools.find(this, str, startIndex, true).begin;
if (r != null && r > 0) return r-1;
else return -1;
}
public function lastIndexOf( str : String, ?startIndex : Int ) : Int {
var i = 0;
var ret = -1;
if( startIndex == null ) startIndex = length;
while( true ) {
var p = indexOf(str, ret+1);
if( p == -1 || p > startIndex ) return ret;
ret = p;
}
}
public function split( delimiter : String ) : Array<String> {
var idx = 1;
var ret = [];
var delim_offset = delimiter.length > 0 ? delimiter.length : 1;
while (idx != null){
var newidx = 0;
if (delimiter.length > 0){
newidx = NativeStringTools.find(this, delimiter, idx, true).begin;
} else if (idx >= this.length){
newidx = null;
} else {
newidx = idx + 1;
}
if (newidx != null){
var match = NativeStringTools.sub(this, idx, newidx-1).match;
ret.push(match);
idx = newidx + delimiter.length;
} else {
ret.push(NativeStringTools.sub(this,idx,NativeStringTools.len(this)).match);
idx = null;
}
}
return ret;
}
public function toString() : String {
return this;
}
public function substring( startIndex : Int, ?endIndex : Int ) : String {
if (endIndex == null) endIndex = this.length;
if (endIndex < 0) endIndex = 0;
if (startIndex < 0) startIndex = 0;
if (endIndex < startIndex) {
// swap the index positions
return NativeStringTools.sub(this, endIndex+1, startIndex).match;
} else {
return NativeStringTools.sub(this, startIndex+1, endIndex).match;
}
}
function get_length() : Int {
return NativeStringTools.len(this);
}
public function charAt( index : Int) : String {
return NativeStringTools.sub(this,index+1, index+1).match;
}
public function charCodeAt( index : Int) : Null<Int> {
return NativeStringTools.byte(this,index+1);
}
public function substr( pos : Int, ?len : Int ) : String {
if (len == null || len > pos + this.length) len = this.length;
else if (len < 0) len = length + len;
if (pos < 0) pos = length + pos;
if (pos < 0) pos = 0;
return NativeStringTools.sub(this, pos + 1, pos+len).match;
}
public inline static function fromCharCode( code : Int ) : String {
return NativeStringTools.char(code);
}
}
|