Skip to content

Commit 4e766cc

Browse files
YakoYako
authored andcommitted
bugfix for substring
1 parent ffa5ae4 commit 4e766cc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/main/java/com/alibaba/fastjson/parser/JSONScanner.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.alibaba.fastjson.JSON;
2626
import com.alibaba.fastjson.JSONException;
27+
import com.alibaba.fastjson.util.ASMUtils;
2728
import com.alibaba.fastjson.util.Base64;
2829

2930
//这个类,为了性能优化做了很多特别处理,一切都是为了性能!!!
@@ -167,17 +168,26 @@ public byte[] bytesValue() {
167168
*/
168169
public final String stringVal() {
169170
if (!hasSpecial) {
170-
// return new String(buf, np + 1, sp);
171-
return text.substring(np + 1, np + 1 + sp);
171+
//return text.substring(np + 1, np + 1 + sp);
172+
return this.subString(np + 1, sp);
172173
} else {
173174
return new String(sbuf, 0, sp);
174175
}
175176
}
176177

177178
public final String subString(int offset, int count) {
178-
return text.substring(offset, offset + count);
179+
//if (ASMUtils.isAndroid()) {
180+
if (true) {
181+
char[] chars = new char[count];
182+
for (int i = offset; i < offset + count; ++i) {
183+
chars[i - offset] = text.charAt(i);
184+
}
185+
return new String(chars);
186+
} else {
187+
return text.substring(offset, offset + count);
188+
}
179189
}
180-
190+
181191
public final String numberString() {
182192
char chLocal = charAt(np + sp - 1);
183193

@@ -186,8 +196,8 @@ public final String numberString() {
186196
sp--;
187197
}
188198

189-
return text.substring(np, np + sp);
190-
// return new String(buf, np, sp);
199+
// return text.substring(np, np + sp);
200+
return this.subString(np, sp);
191201
}
192202

193203
public final int ISO8601_LEN_0 = "0000-00-00".length();
@@ -881,8 +891,8 @@ public Collection<String> scanFieldStringArray(char[] fieldName, Class<?> type)
881891
for (;;) {
882892
ch = charAt(index++);
883893
if (ch == '\"') {
884-
strVal = text.substring(start, index - 1);
885-
// strVal = new String(buf, start, index - start - 1);
894+
strVal = this.subString(start, index - start - 1);
895+
// strVal = text.substring(start, index - 1);
886896
list.add(strVal);
887897
ch = charAt(index++);
888898
break;

0 commit comments

Comments
 (0)