declare var string ; declare var table ; module JS { type FuncString = ( p : string ) => string ; export class StringHelper { public static getLength ( this ) { return string . len ( this ) ; } public static fromCharCode ( code ) { return string . char ( tonumber ( code ) ) ; } public static charCodeAt ( this , index ) { return string . byte ( this , index + 1 ) ; } public static replace ( this , valOrRegExp , valOrFunc ) { if ( typeof valOrRegExp === 'string' ) { return string . gsub ( this , string . gsub ( valOrRegExp , '[%^%$%(%)%%%.%[%]%*%+%-%?]' , '%%%1' ) , valOrFunc ) ; } if ( ( < any > valOrRegExp ) . nativeHandle ) { let current = 0 ; const size = this . getLength ( ) ; const isFunc = typeof valOrFunc === 'function' ; const result = new Array < string > ( ) ; while ( current < size ) { const matchResult = ( < RegExp > valOrRegExp ) . exec ( this ) ; const position = matchResult ? matchResult . index : - 1 ; if ( position < 0 ) { const rest = this . substring ( current ) ; ArrayHelper . pushOne ( result , rest ) ; return result . join ( ) ; } if ( position > current ) { const part = this . substring ( current , position ) ; ArrayHelper . pushOne ( result , part ) ; } current = position + matchResult [ 0 ] . length ; if ( ! isFunc ) { ArrayHelper . pushOne ( result , valOrFunc ) ; } else { const val = valOrFunc ( matchResult [ 0 ] , table . unpack ( matchResult ) ) ; ArrayHelper . pushOne ( result , val ) ; } } return result . join ( ) ; } return string . gsub ( this , ( < any > valOrRegExp ) . __getLuaPattern ( ) , valOrFunc ) ; } public static substr ( this , begin ? , len ? ) { return string . sub ( this , ( begin || 0 ) + 1 , ( begin || 0 ) + ( len || string . len ( this ) ) ) ; } public static substring ( this , begin ? , _end ? ) { if ( begin == _end && _end == null || begin === _end ) { return '' ; } return string . sub ( this , ( begin || 0 ) + 1 , _end != undefined ? _end : null ) ; } public static slice ( this , start ? , _end ? ) { return string . sub ( this , ( start || 0 ) + 1 , _end != undefined ? _end : null ) ; } public static indexOf ( this , pattern , begin ? ) { return ( table . pack ( string . find ( this , pattern , ( begin || 0 ) + 1 , true ) ) [ 1 ] || 0 ) - 1 ; } public static lastIndexOf ( this , pattern , begin ? ) { let lastFound ; let found ; do { lastFound = found ; found = table . pack ( string . find ( this , pattern , ( begin || found || 0 ) + 1 , true ) ) [ 1 ] ; } while ( found ) ; return lastFound ? lastFound - 1 : - 1 ; } public static search ( this , pattern , begin ? ) { if ( typeof pattern === 'string' ) { table . pack ( string . find ( this , pattern , ( begin || 0 ) + 1 , true ) ) [ 1 ] || - 1 ; } return table . pack ( string . find ( this , ( < any > pattern ) . __getLuaPattern ( ) , ( begin || 0 ) + 1 ) ) [ 1 ] || - 1 ; } public static toLowerCase ( this ) { return string . lower ( this ) ; } public static toUpperCase ( this ) { return string . upper ( this ) ; } public static split ( this , separator ) { let current = 0 ; const size = this . getLength ( ) ; const sizeSeparator = separator . getLength ( ) ; const result = new Array < string > ( ) ; while ( current < size ) { const position = this . indexOf ( separator , current ) ; if ( position < 0 ) { const rest = this . substring ( current ) ; ArrayHelper . pushOne ( result , rest ) ; return result ; } const part = this . substring ( current , position ) ; current = position + sizeSeparator ; ArrayHelper . pushOne ( result , part ) ; } return result ; } } export class String { public constructor ( private constString ) { this . __tostring = function ( ) : string { return this . constString ; } ; this . __concat = function ( left , right ) { return < string > ( ( ( < String > left ) . constString ) || left ) + < string > ( ( ( < String > right ) . constString ) || right ) ; } ; this . __index = function ( _this , indx ) : any { if ( typeof ( indx ) === 'number' ) { return _this . constString [ indx ] ; } return __get_call_undefined__ ( _this , indx ) ; } ; } public replace ( valOrRegExp , valOrFunc ) { return new String ( this . constString . replace ( valOrRegExp , valOrFunc ) ) ; } public charCodeAt ( index ) { return this . constString . charCodeAt ( index ) ; } public substr ( begin ? , len ? ) { return new String ( this . constString . substr ( begin , len ) ) ; } public substring ( begin ? , _end ? ) { return new String ( this . constString . substring ( begin , _end ) ) ; } public indexOf ( pattern , begin ? ) { return this . constString . indexOf ( pattern , begin ) ; } public split ( separator ) { return this . constString . split ( separator ) ; } public slice ( start ? , _end ? ) { return new String ( this . constString . substring ( start , _end ) ) ; } public toLowerCase ( ) { return new String ( this . constString . toLowerCase ( ) ) ; } public toUpperCase ( ) { return new String ( this . constString . toUpperCase ( ) ) ; } public get length ( ) : number { return string . len ( this . constString ) ; } public toString ( ) { return this ; } } }