Skip to content

Commit b09c436

Browse files
committed
CharSequence lambda fix
1 parent da78eb9 commit b09c436

File tree

1 file changed

+39
-59
lines changed

1 file changed

+39
-59
lines changed

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,7 @@ throw Clazz.new_(Clazz.load('java.util.NoSuchElementException'));
19751975

19761976
Clazz.newMeth(C$, ['forEachRemaining$java_util_function_IntConsumer','forEachRemaining$O'], function (block) {
19771977
for (; this.cur < this.b$['CharSequence'].length$(); this.cur++) {
1978-
block.accept$(this.b$['CharSequence'].charAt$I(this.cur).$c());
1978+
block.accept$I(this.b$['CharSequence'].charAt$I(this.cur).$c());
19791979
}
19801980
});
19811981

@@ -2004,14 +2004,14 @@ try {
20042004
while (i < length){
20052005
var c1 = this.b$['CharSequence'].charAt$I(i++);
20062006
if (!Character.isHighSurrogate$C(c1) || i >= length ) {
2007-
block.accept$(c1.$c());
2007+
block.accept$I(c1.$c());
20082008
} else {
20092009
var c2 = this.b$['CharSequence'].charAt$I(i);
20102010
if (Character.isLowSurrogate$C(c2)) {
20112011
i++;
2012-
block.accept$(Character.toCodePoint$C$C(c1, c2));
2012+
block.accept$I(Character.toCodePoint$C$C(c1, c2));
20132013
} else {
2014-
block.accept$(c1.$c());
2014+
block.accept$I(c1.$c());
20152015
}}}
20162016
} finally {
20172017
this.cur=i;
@@ -5528,46 +5528,51 @@ m$(Boolean,"toString",function(){return this.valueOf()?"true":"false";});
55285528
m$(Boolean,"toString$Z",function(b){return "" + b;}, 1);
55295529

55305530

5531-
// this next part is InternetExplorer only, and only UTF8
5532-
55335531
Clazz._Encoding={
5534-
UTF8:"utf-8",
5535-
UTF16:"utf-16", // LE
5532+
UTF8:"utf-8", // EF BB BF
5533+
UTF16:"utf-16", // FF FE (LE)
55365534
ASCII:"ascii"
55375535
};
55385536

5539-
(function(Encoding) {
5537+
(function(E) {
5538+
5539+
var textDecoder = (self.TextDecoder && new TextDecoder() || null);
55405540

5541-
Encoding.guessEncoding=function(str){
5542-
return (str.charCodeAt(0)==0xEF&&str.charCodeAt(1)==0xBB&&str.charCodeAt(2)==0xBF ? Encoding.UTF8
5543-
: str.charCodeAt(0)==0xFF&&str.charCodeAt(1)==0xFE ? Encoding.UTF16 // LE
5544-
: Encoding.ASCII);
5541+
E.guessEncoding=function(str){
5542+
return ((str.charCodeAt(0)&0xFF)==0xEF&&(str.charCodeAt(1)&0xFF)==0xBB&&(str.charCodeAt(2)&0xFF)==0xBF ? E.UTF8
5543+
: (str.charCodeAt(0)&0xFF)==0xFF&&(str.charCodeAt(1)&0xFF)==0xFE ? E.UTF16 // LE
5544+
: E.ASCII);
55455545
};
55465546

5547-
Encoding.guessEncodingArray=function(a, offset){
5548-
return (a[offset]==0xEF&&a[offset + 1]==0xBB&&a[offset + 2]==0xBF ? Encoding.UTF8
5549-
: a[offset + 0]==0xFF&&a[offset + 1]==0xFE ? Encoding.UTF16 : Encoding.ASCII);
5547+
E.guessEncodingArray=function(a, offset){
5548+
return ((a[offset]&0xFF)==0xEF&&(a[offset + 1]&0xFF)==0xBB&&(a[offset + 2]&0xFF)==0xBF ? E.UTF8
5549+
: (a[offset + 0]&0xFF)==0xFF&&(a[offset + 1]&0xFF)==0xFE ? E.UTF16 : E.ASCII);
55505550
};
55515551

5552-
Encoding.readUTF8Array=function(a, offset, length){
5552+
E.readUTF8Array=function(a, offset, length){
55535553
// a will be an Int8Array, UTF8 only
5554-
if (textDecoder) {
5554+
// TextDecoder will accept a BOM or not. Java doesn't
5555+
var encoding=E.guessEncodingArray(a, offset);
5556+
var startIdx=0;
5557+
if(encoding==E.UTF8){
5558+
startIdx=3;
5559+
}else if(encoding==E.UTF16){
5560+
startIdx=2;
5561+
}
5562+
if (textDecoder) {
5563+
offset += startIdx;
5564+
length -= startIdx;
55555565
if (offset == 0 && length == a.length)
55565566
return textDecoder.decode(a);
55575567
var arr=new Uint8Array(length);
55585568
for(var i = 0; i < length; i++){
55595569
arr[i] = a[offset + i];
55605570
}
5561-
return textDecoder.decode(arr);
5562-
}
5571+
// Java needs to see the 0xFEFF byte mark
5572+
var s = textDecoder.decode(arr);
5573+
return (startIdx ? '\ufeff' + s : s);
5574+
}
55635575
// IE only. I don't know where this comes from. Is it Java?
5564-
var encoding=Encoding.guessEncodingArray(a);
5565-
var startIdx=0;
5566-
if(encoding==Encoding.UTF8){
5567-
startIdx=3;
5568-
}else if(encoding==Encoding.UTF16){
5569-
startIdx=2;
5570-
}
55715576
var arrs=new Array();
55725577
for(var i=offset + startIdx, endIdx = offset + length; i < endIdx; i++){
55735578
var charCode=a[i];
@@ -5590,12 +5595,12 @@ return arrs.join('');
55905595
};
55915596

55925597

5593-
Encoding.convert2UTF8=function(str){
5598+
E.convert2UTF8=function(str){
55945599
var encoding=this.guessEncoding(str);
55955600
var startIdx=0;
5596-
if(encoding==Encoding.UTF8){
5601+
if(encoding==E.UTF8){
55975602
return str;
5598-
}else if(encoding==Encoding.UTF16){
5603+
}else if(encoding==E.UTF16){
55995604
startIdx=2;
56005605
}
56015606

@@ -5619,7 +5624,6 @@ arrs[offset+i-startIdx]=String.fromCharCode(c1)+String.fromCharCode(c2)+String.f
56195624
}
56205625
return arrs.join('');
56215626
};
5622-
56235627
if(!String.__PARAMCODE){
56245628

56255629
String.__PARAMCODE = "S";
@@ -5902,7 +5906,7 @@ if(arguments.length==1){
59025906
return cs.encode$S(this.toString()).toArray$();
59035907
}
59045908
if(cs=="utf-8"||cs=="utf8"){
5905-
s=Encoding.convert2UTF8(this);
5909+
s=E.convert2UTF8(this);
59065910
}
59075911
}
59085912
var arrs=[];
@@ -6059,30 +6063,6 @@ sp.trim$ = function() {
60596063

60606064
})(String.prototype);
60616065

6062-
/*
6063-
String(byte[] bytes)
6064-
String(char[] value)
6065-
String(StringBuffer buffer)
6066-
String(StringBuilder builder)
6067-
String(String original)
6068-
6069-
String(char[] value, boolean share) // Java8
6070-
String(byte[] ascii, int hibyte)
6071-
String(byte[] bytes, Charset charset)
6072-
String(byte[] bytes, String charsetName)
6073-
6074-
String(byte[] bytes, int offset, int length)
6075-
String(char[] value, int offset, int count)
6076-
String(int[] codePoints, int offset, int count)
6077-
6078-
String(byte[] bytes, int offset, int length, Charset charset)
6079-
String(byte[] bytes, int offset, int length, String charsetName)
6080-
String(byte[] ascii, int hibyte, int offset, int count)
6081-
*/
6082-
6083-
var textDecoder = (self.TextDecoder && new TextDecoder() || null);
6084-
var textDecoders = {};
6085-
60866066
// Note that of all these constructors, only new String("xxx") and new String(new String())
60876067
// return actual JavaScript String objects (as of 3.2.9.v1)
60886068

@@ -6098,10 +6078,10 @@ case 1:
60986078
// String(StringBuilder builder)
60996079
// String(String original)
61006080
if (x.__BYTESIZE){
6101-
return x.length == 0 ? "" : Encoding.readUTF8Array(x, 0, x.length).toString();
6081+
return x.length == 0 ? "" : E.readUTF8Array(x, 0, x.length).toString();
61026082
}
61036083
if (x instanceof Array){
6104-
return x.length == 0 ? "" : typeof x[0]=="number" ? Encoding.readUTF8Array(new Uint8Array(x), 0, x.length).toString() : x.join('');
6084+
return x.length == 0 ? "" : typeof x[0]=="number" ? E.readUTF8Array(new Uint8Array(x), 0, x.length).toString() : x.join('');
61056085
}
61066086
// raw JavaScript string unless new String(string)
61076087
return (typeof x == "string" || x instanceof String ? new String(x) : x.toString());
@@ -6152,7 +6132,7 @@ case 4:
61526132
var length=arguments[2];
61536133
if (typeof cs == "string") {
61546134
if (",utf8,utf-8,".indexOf("," + cs.toLowerCase() + ",") >= 0)
6155-
return Encoding.readUTF8Array(bytes,offset,length).toString();
6135+
return E.readUTF8Array(bytes,offset,length).toString();
61566136
cs = Clazz.loadClass("java.nio.charset.Charset").forName$S(cs);
61576137
if (!cs)
61586138
throw new java.io.UnsupportedEncodingException();

0 commit comments

Comments
 (0)