原文在
今天在玩
开源的Script的引擎的时候在解释下面script的时候出现问题,
3GEYE x _:f i ] J
你知道为什么吗
3GEYE9}&`'R$M%\ d Y&a
i = 5
3GEYE1E ^:p ` u
j = 1232323
3GEYE M"U z B z O+A ?"w
h = i + j
3GEYE'r X%I8P&b+L
println(h)
N ~;A7B U5c x0G \ u33GEYE.h h#n I [ L
抛出这个异常
3GEYE a7T m/e v"n j
+`)c,} k3s34 : Illegal statment ,String index out of range: -9
3GEYE3y!r W R%? m:k m P N
java.lang.StringIndexOutOfBoundsException: String index out of range: -9
3GEYE v0m F U x)U m
at java.lang.String.substring(Unknown Source)
@ v&w%U R,M3x.s y3 at gscript.Interpreter.callSub(Interpreter.java:1355)
3GEYE A(k4N Q!I J G a C u
at gscript.Interpreter._sub(Interpreter.java:401)
'_ d.g [ H*I(\ G3 at gscript.Interpreter.callSub(Interpreter.java:1353)
a h z \ f M3 at gscript.Interpreter.start(Interpreter.java:227)
/m/V |,v!d m;@3 at gscript.Interpreter.start(Interpreter.java:198)
L5|"^ E j C3 at ScriptTest.main(ScriptTest.java:12)
(i ]*@ M S \ c,`3a%c3
)N6r H!`+h3
7R W U+h!L1Q(P3后来发现原来是println(h)少了一个换行符号。
U B g ]4b @ d J!F j5Y3
U z _.s J3现在我修改了下源代码。
3GEYE P M \ O#c
我修改了下代码后,这个bug解决了
I ^"M T.` e)B N3看原来的代码
@6}/~ v ^ h N X3 for (int i = 0; i < srcBytes.length; i++) {
3GEYE [ z&G U$@)q z/s-a,C
if (srcBytes[i] == 0x0a || i + 1 == srcBytes.length) { // 行结束,或者
文件结束
3GEYE v G |'y d D
try {
3GEYE4U q z i,V0N z W-R
String s = new String(line.toByteArray()); //
j2me使用
/s d K3?9T U H ^8l \/} ~3 // String s = new String(line.toByteArray(), "GB2312");
.P f H#K6B U X'e F8R3 // //j2se使用
3GEYE#O e u;Q ])S+p.M e
s = s.trim();
3GEYE Y,b E ? D L j O
m'j!g } g8| L3 srcCode[lineCount++] = s;
3GEYE C7o u ~-P a
} catch (Exception ex1) {
#H V p3v M;V V P9|3 }
3GEYE N:u t:r ]-t.U
line.reset();
h p Q&w x3 } else {
3GEYE e |6D1f6x y
line.write(srcBytes[i]);
3GEYE R T T ]0h a
}
b r,d ^.^3 }
3GEYE-{ A#x)\3z4| N
3GEYE p Q-g n j y g3z
修改后的代码
3GEYE.R p,{%P5s&C4Y
:S$N H8a R g3for (int i = 0; i < srcBytes.length; i++) {
l \ f(L O%} y-Y V$L3 if (srcBytes[i] == 0x0a ) { // 行结束,或者文件结束
3GEYE:[ o Q5_ d1J1^ {
try {
3GEYE!y2~ L m R p B!B
String s = new String(line.toByteArray()); // j2me使用
&~$`,Q ` u(A D*u3 // String s = new String(line.toByteArray(), "GB2312");
3GEYE E h0\/| j w X U3V
// //j2se使用
3GEYE k1}4X S K L
s = s.trim();
3GEYE3s h T o#{ z b
//System.out.println(s);
3GEYE u i K&^5_)x6m0S p m*H
srcCode[lineCount++] = s;
3GEYE g ]&D%d P
} catch (Exception ex1) {
?%l/{,V { ^ ~ i3 }
@8B J _ W c1\3 line.reset();
8R e J&J p ~ T3 } else {
3GEYE+S p p5F D:R N6d M/E&z
line.write(srcBytes[i]);
M w$n6b(|;n J3 if(i + 1 == srcBytes.length){
$T9Q*C P3O(_0q3 String s = new String(line.toByteArray()); // j2me使用
3GEYE W A4E;c x0V
// String s = new String(line.toByteArray(), "GB2312");
3GEYE ^ O$]#j/h$C
// //j2se使用
U;S U n u9}3 s = s.trim();
}+^ ? I T q ?3 s = s.trim();
3GEYE } k9A+P Y"x
//System.out.println(s);
i [ S$Y!~3 srcCode[lineCount++] = s;
3GEYE l U d I c I
}
3GEYE { k4~ t L
}
8V | h4Y({9N S v3 }
9U)q9w3v6g V3