private Object js_escape ( Object [ ] args ) { final int URL_XALPHAS = 1 , URL_XPALPHAS = 2 , URL_PATH = 4 ; String s = ScriptRuntime . toString ( args , 0 ) ; int mask = URL_XALPHAS | URL_XPALPHAS | URL_PATH ; if ( args . length > 1 ) { // the 'mask' argument. Non-ECMA. double d = ScriptRuntime . toNumber ( args [ 1 ] ) ; if ( d != d || ( ( mask = ( int ) d ) != d ) || 0 != ( mask & ~ ( URL_XALPHAS | URL_XPALPHAS | URL_PATH ) ) ) { throw Context . reportRuntimeError0 ( "msg.bad.esc.mask" ) ; } } StringBuilder sb = null ; for ( int k = 0 , L = s . length ( ) ; k != L ; ++ k ) { int c = s . charAt ( k ) ; if ( mask != 0 && ( ( c >= ' ' && c <= ' ' ) || ( c >= ' ' && c <= ' ' ) || ( c >= ' ' && c <= ' ' ) || c == ' ' || c == ' ' || c == ' ' || c == ' ' || c == ' ' || ( 0 != ( mask & URL_PATH ) && ( c == ' ' || c == ' ' ) ) ) ) { if ( sb != null ) { sb . append ( ( char ) c ) ; } } else { if ( sb == null ) { sb = new StringBuilder ( L + 3 ) ; sb . append ( s ) ; sb . setLength ( k ) ; } int hexSize ; if ( c < 256 ) { if ( c == ' ' && mask == URL_XPALPHAS ) { sb . append ( ' ' ) ; continue ; } sb . append ( ' ' ) ; hexSize = 2 ; } else { sb . append ( ' ' ) ; sb . append ( ' ' ) ; hexSize = 4 ; } // append hexadecimal form of c left-padded with 0 for ( int shift = ( hexSize - 1 ) * 4 ; shift >= 0 ; shift -= 4 ) { int digit = 0xf & ( c >> shift ) ; int hc = ( digit < 10 ) ? ' ' + digit : ' ' - 10 + digit ; sb . append ( ( char ) hc ) ; } } } return ( sb == null ) ? s : sb . toString ( ) ; }
The global method escape as per ECMA - 262 15 . 1 . 2 . 4 .