@@ -54,7 +54,7 @@ String.prototype.regionMatches = function (ignoreCase, toffset,
5454 return false ;
5555 }
5656 var s1 = this . substring ( toffset , toffset + len ) ;
57- var s2 = this . substring ( ooffset , ooffset + len ) ;
57+ var s2 = other . substring ( ooffset , ooffset + len ) ;
5858 if ( ignoreCase ) {
5959 s1 = s1 . toLowerCase ( ) ;
6060 s2 = s2 . toLowerCase ( ) ;
@@ -143,7 +143,7 @@ String.prototype.endsWith = function (suffix) {
143143} ;
144144
145145String . prototype . equals = function ( anObject ) {
146- return this == anObject ;
146+ return this . valueOf ( ) == anObject ;
147147} ;
148148
149149String . prototype . equalsIgnoreCase = function ( anotherString ) {
@@ -169,15 +169,50 @@ String.prototype.hashCode = function () {
169169} ;
170170
171171String . prototype . getBytes = function ( ) {
172- var utf8Str = Encoding . convert2UTF8 ( this ) ;
173- var arrs = new Array ( utf8Str . length ) ;
174- for ( var i = 0 ; i < utf8Str . length ; i ++ ) {
175- arrs [ i ] = utf8Str . charCodeAt ( i ) ;
172+ if ( arguments . length == 4 ) {
173+ return this . getChars ( arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] , arguments [ 3 ] ) ;
174+ }
175+ var s = this ;
176+ if ( arguments . length == 1 ) {
177+ var cs = arguments [ 0 ] . toString ( ) . toLowerCase ( ) ;
178+ var charset = [
179+ "utf-8" , "UTF8" , "us-ascii" , "iso-8859-1" , "8859_1" , "gb2312" , "gb18030" , "gbk"
180+ ] ;
181+ var existed = false ;
182+ for ( var i = 0 ; i < charset . length ; i ++ ) {
183+ if ( charset [ i ] == cs ) {
184+ existed = true ;
185+ break ;
186+ }
187+ }
188+ if ( ! existed ) {
189+ throw new java . io . UnsupportedEncodingException ( ) ;
190+ }
191+ if ( cs == "utf-8" || cs == "utf8" ) {
192+ s = Encoding . convert2UTF8 ( this ) ;
193+ }
194+ }
195+ var arrs = new Array ( s . length ) ;
196+ var c = 0 , ii = 0 ;
197+ for ( var i = 0 ; i < s . length ; i ++ ) {
198+ c = s . charCodeAt ( i ) ;
199+ if ( c > 255 ) {
200+ arrs [ ii ] = 0x1a ;
201+ arrs [ ii + 1 ] = c & 0xff ;
202+ arrs [ ii + 2 ] = ( c & 0xff00 ) >> 8 ;
203+ ii += 2 ;
204+ } else {
205+ arrs [ ii ] = c ;
206+ }
207+ ii ++ ;
176208 }
177209 return arrs ;
178210} ;
179211
180212String . prototype . compareTo = function ( anotherString ) {
213+ if ( anotherString == null ) {
214+ throw new java . lang . NullPointerException ( ) ;
215+ }
181216 var len1 = this . length ;
182217 var len2 = anotherString . length ;
183218 var n = Math . min ( len1 , len2 ) ;
@@ -202,6 +237,19 @@ String.prototype.toCharArray = function () {
202237} ;
203238
204239String . valueOf = function ( o ) {
240+ if ( o instanceof Array ) {
241+ if ( arguments . length == 1 ) {
242+ return o . join ( '' ) ;
243+ } else {
244+ var off = arguments [ 1 ] ;
245+ var len = arguments [ 2 ] ;
246+ var oo = new Array ( len ) ;
247+ for ( var i = 0 ; i < len ; i ++ ) {
248+ oo [ i ] = o [ off + i ] ;
249+ }
250+ return oo . join ( '' ) ;
251+ }
252+ }
205253 return "" + o ;
206254} ;
207255
@@ -210,12 +258,23 @@ String.prototype.subSequence = function (beginIndex, endIndex) {
210258} ;
211259
212260String . prototype . compareToIgnoreCase = function ( str ) {
213- if ( this == str ) {
261+ if ( str == null ) {
262+ throw new NullPointerException ( ) ;
263+ }
264+ var s1 = this . toUpperCase ( ) ;
265+ var s2 = str . toUpperCase ( ) ;
266+ if ( s1 == s2 ) {
214267 return 0 ;
215- } else if ( this > str ) {
216- return 1 ;
217268 } else {
218- return - 1 ;
269+ var s1 = this . toLowerCase ( ) ;
270+ var s2 = str . toLowerCase ( ) ;
271+ if ( s1 == s2 ) {
272+ return 0 ;
273+ } else if ( s1 > s2 ) {
274+ return 1 ;
275+ } else {
276+ return - 1 ;
277+ }
219278 }
220279} ;
221280
@@ -226,7 +285,7 @@ String.prototype.contentEquals = function (sb) {
226285 var v = sb . getValue ( ) ;
227286 var i = 0 ;
228287 var j = 0 ;
229- var n = count ;
288+ var n = this . length ;
230289 while ( n -- != 0 ) {
231290 if ( this . charCodeAt ( i ++ ) != v [ j ++ ] ) {
232291 return false ;
@@ -245,11 +304,37 @@ String.prototype.getChars = function (srcBegin, srcEnd, dst, dstBegin) {
245304 if ( srcBegin > srcEnd ) {
246305 throw new StringIndexOutOfBoundsException ( srcEnd - srcBegin ) ;
247306 }
307+ if ( dst == null ) {
308+ throw new NullPointerException ( ) ;
309+ }
248310 for ( var i = 0 ; i < srcEnd - srcBegin ; i ++ ) {
249311 dst [ dstBegin + i ] = this . charAt ( srcBegin + i ) ;
250312 }
251313} ;
252-
314+ String . prototype . $concat = String . prototype . concat ;
315+ String . prototype . concat = function ( s ) {
316+ if ( s == null ) {
317+ throw new NullPointerException ( ) ;
318+ }
319+ return this . $concat ( s ) ;
320+ } ;
321+ String . prototype . $lastIndexOf = String . prototype . lastIndexOf ;
322+ String . prototype . lastIndexOf = function ( s , last ) {
323+ if ( last + this . length <= 0 ) {
324+ return - 1 ;
325+ }
326+ return this . $lastIndexOf ( s , last ) ;
327+ } ;
328+ String . prototype . intern = function ( ) {
329+ return this . valueOf ( ) ;
330+ } ;
331+ String . copyValueOf = String . prototype . copyValueOf = function ( ) {
332+ if ( arguments . length == 1 ) {
333+ return String . instantialize ( arguments [ 0 ] ) ;
334+ } else {
335+ return String . instantialize ( arguments [ 0 ] , arguments [ 1 ] , arguments [ 2 ] ) ;
336+ }
337+ } ;
253338String . indexOf = function ( source , sourceOffset , sourceCount ,
254339 target , targetOffset , targetCount , fromIndex ) {
255340 if ( fromIndex >= sourceCount ) {
@@ -323,15 +408,31 @@ String.instantialize = function () {
323408 }
324409 } else if ( arguments . length == 2 ) {
325410 var x = arguments [ 0 ] ;
326- var y = arguments [ 1 ] ;
327- return String . instantialize ( x , 0 , x . length , y ) ;
411+ var hibyte = arguments [ 1 ] ;
412+ if ( typeof hibyte == "string" ) {
413+ return String . instantialize ( x , 0 , x . length , hibyte ) ;
414+ } else {
415+ return String . instantialize ( x , hibyte , 0 , x . length ) ;
416+ }
328417 } else if ( arguments . length == 3 ) {
329418 var bytes = arguments [ 0 ] ;
330419 var offset = arguments [ 1 ] ;
331420 var length = arguments [ 2 ] ;
332421 var arr = new Array ( length ) ;
333- for ( var i = 0 ; i < length ; i ++ ) {
334- arr [ i ] = bytes [ offset + i ] ;
422+ if ( offset < 0 || length + offset > bytes . length ) {
423+ throw new IndexOutOfBoundsException ( ) ;
424+ }
425+ if ( length > 0 ) {
426+ var isChar = ( bytes [ offset ] . length != null ) ;
427+ if ( isChar ) {
428+ for ( var i = 0 ; i < length ; i ++ ) {
429+ arr [ i ] = bytes [ offset + i ] ;
430+ }
431+ } else {
432+ for ( var i = 0 ; i < length ; i ++ ) {
433+ arr [ i ] = String . fromCharCode ( bytes [ offset + i ] ) ;
434+ }
435+ }
335436 }
336437 return arr . join ( '' ) ;
337438 } else if ( arguments . length == 4 ) {
@@ -347,22 +448,25 @@ String.instantialize = function () {
347448 arr [ i ] = String . fromCharCode ( arr [ i ] & 0xff ) ;
348449 }
349450 }
350- if ( y . toLowerCase ( ) == "utf-8" ) {
451+ var cs = y . toLowerCase ( ) ;
452+ if ( cs == "utf-8" || cs == "utf8" ) {
351453 return Encoding . readUTF8 ( arr . join ( '' ) ) ;
352454 } else {
353455 return arr . join ( '' ) ;
354456 }
355457 } else {
458+ var count = arguments [ 3 ] ;
459+ var offset = arguments [ 2 ] ;
460+ var hibyte = arguments [ 1 ] ;
356461 var value = new Array ( count ) ;
357-
358462 if ( hibyte == 0 ) {
359463 for ( var i = count ; i -- > 0 ; ) {
360- value [ i ] = String . fromCharCode ( ascii [ i + offset ] & 0xff ) ;
464+ value [ i ] = String . fromCharCode ( bytes [ i + offset ] & 0xff ) ;
361465 }
362466 } else {
363467 hibyte <<= 8 ;
364468 for ( var i = count ; i -- > 0 ; ) {
365- value [ i ] = String . fromCharCode ( hibyte | ( ascii [ i + offset ] & 0xff ) ) ;
469+ value [ i ] = String . fromCharCode ( hibyte | ( bytes [ i + offset ] & 0xff ) ) ;
366470 }
367471 }
368472 return value . join ( '' ) ;
0 commit comments