Skip to content

Commit 5ea12c3

Browse files
committed
Fixes in binary memory.
1 parent de2dd8a commit 5ea12c3

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

jphp-runtime/src/php/runtime/memory/BinaryMemory.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package php.runtime.memory;
22

3+
import java.nio.charset.StandardCharsets;
34
import php.runtime.Memory;
45
import php.runtime.env.TraceInfo;
56

@@ -39,7 +40,7 @@ public char toChar() {
3940

4041
@Override
4142
public String toString() {
42-
return new String(bytes, Charset.forName("UTF-8"));
43+
return new String(bytes, StandardCharsets.UTF_8);
4344
}
4445

4546
@Override
@@ -54,28 +55,43 @@ public String toBinaryString(){
5455
@Override
5556
public Memory valueOfIndex(TraceInfo trace, Memory index) {
5657
int i = index.toInteger();
58+
59+
if (i < 0 && Math.abs(i) <= bytes.length) {
60+
i = bytes.length + i;
61+
}
62+
5763
if (i < 0 || i >= bytes.length)
5864
return FALSE;
5965

60-
return new StringMemory((char)(bytes[i] & 0xFF));
66+
return new BinaryMemory(bytes[i]);
6167
}
6268

6369
@Override
6470
public Memory valueOfIndex(TraceInfo trace, long index) {
6571
int i = (int)index;
72+
73+
if (i < 0 && Math.abs(i) <= bytes.length) {
74+
i = bytes.length + i;
75+
}
76+
6677
if (i < 0 || i >= bytes.length)
6778
return FALSE;
6879

69-
return new StringMemory((char)(bytes[i] & 0xFF));
80+
return new BinaryMemory(bytes[i]);
7081
}
7182

7283
@Override
7384
public Memory valueOfIndex(TraceInfo trace, double index) {
74-
int i = (int)index;
85+
int i = (int) index;
86+
87+
if (i < 0 && Math.abs(i) <= bytes.length) {
88+
i = bytes.length + i;
89+
}
90+
7591
if (i < 0 || i >= bytes.length)
7692
return FALSE;
7793

78-
return new StringMemory((char)(bytes[i] & 0xFF));
94+
return new BinaryMemory(bytes[i]);
7995
}
8096

8197
@Override
@@ -84,7 +100,7 @@ public Memory valueOfIndex(TraceInfo trace, boolean index) {
84100
if (i < 0 || i >= bytes.length)
85101
return FALSE;
86102

87-
return new StringMemory((char)(bytes[i] & 0xFF));
103+
return new BinaryMemory(bytes[i]);
88104
}
89105

90106
@Override
@@ -93,10 +109,16 @@ public Memory valueOfIndex(TraceInfo trace, String index) {
93109
if (i == null)
94110
return FALSE;
95111

96-
if (i.toInteger() < 0 || i.toInteger() >= bytes.length)
112+
int toInteger = i.toInteger();
113+
114+
if (toInteger < 0 && Math.abs(toInteger) <= bytes.length) {
115+
toInteger = bytes.length + toInteger;
116+
}
117+
118+
if (toInteger < 0 || toInteger >= bytes.length)
97119
return FALSE;
98120

99-
return new StringMemory((char)(bytes[i.toInteger()] & 0xFF));
121+
return new BinaryMemory(bytes[toInteger]);
100122
}
101123

102124
@Override

jphp-runtime/src/php/runtime/memory/StringMemory.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,11 @@ public Memory valueOfIndex(TraceInfo trace, Memory index) {
577577

578578
String toString = toString();
579579

580-
if (_index < toString.length() && _index >= 0)
580+
int length = toString.length();
581+
if (_index < length && _index >= 0)
581582
return getChar(toString.charAt(_index));
582-
else if (_index < 0 && Math.abs(_index) <= toString.length())
583-
return getChar(toString.charAt(toString.length() + _index));
583+
else if (_index < 0 && Math.abs(_index) <= length)
584+
return getChar(toString.charAt(length + _index));
584585
else
585586
return CONST_EMPTY_STRING;
586587
}
@@ -589,10 +590,11 @@ else if (_index < 0 && Math.abs(_index) <= toString.length())
589590
public Memory valueOfIndex(TraceInfo trace, long index) {
590591
int _index = (int)index;
591592
String string = toString();
592-
if (_index >= 0 && _index < string.length())
593+
int length = string.length();
594+
if (_index >= 0 && _index < length)
593595
return getChar(string.charAt(_index));
594-
else if (_index < 0 && Math.abs(_index) <= string.length())
595-
return getChar(string.charAt(string.length() + _index));
596+
else if (_index < 0 && Math.abs(_index) <= length)
597+
return getChar(string.charAt(length + _index));
596598
else
597599
return CONST_EMPTY_STRING;
598600
}
@@ -601,10 +603,11 @@ else if (_index < 0 && Math.abs(_index) <= string.length())
601603
public Memory valueOfIndex(TraceInfo trace, double index) {
602604
int _index = (int)index;
603605
String string = toString();
604-
if (_index >= 0 && _index < string.length())
606+
int length = string.length();
607+
if (_index >= 0 && _index < length)
605608
return getChar(string.charAt(_index));
606-
else if (_index < 0 && Math.abs(_index) <= string.length())
607-
return getChar(string.charAt(string.length() + _index));
609+
else if (_index < 0 && Math.abs(_index) <= length)
610+
return getChar(string.charAt(length + _index));
608611
else
609612
return CONST_EMPTY_STRING;
610613
}
@@ -613,10 +616,11 @@ else if (_index < 0 && Math.abs(_index) <= string.length())
613616
public Memory valueOfIndex(TraceInfo trace, boolean index) {
614617
int _index = index ? 1 : 0;
615618
String string = toString();
616-
if (_index >= 0 && _index < string.length())
619+
int length = string.length();
620+
if (_index >= 0 && _index < length)
617621
return getChar(string.charAt(_index));
618-
else if (_index < 0 && Math.abs(_index) <= string.length())
619-
return getChar(string.charAt(string.length() + _index));
622+
else if (_index < 0 && Math.abs(_index) <= length)
623+
return getChar(string.charAt(length + _index));
620624
else
621625
return CONST_EMPTY_STRING;
622626
}
@@ -630,10 +634,11 @@ public Memory valueOfIndex(TraceInfo trace, String index) {
630634
_index = tmp.toInteger();
631635

632636
String string = toString();
633-
if (_index >= 0 && _index < string.length())
637+
int length = string.length();
638+
if (_index >= 0 && _index < length)
634639
return getChar(string.charAt(_index));
635-
else if (_index < 0 && Math.abs(_index) <= string.length())
636-
return getChar(string.charAt(string.length() + _index));
640+
else if (_index < 0 && Math.abs(_index) <= length)
641+
return getChar(string.charAt(length + _index));
637642
else
638643
return CONST_EMPTY_STRING;
639644
}

0 commit comments

Comments
 (0)