Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions java/src/json/ext/SWARBasicStringEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ void encode(ByteList src) throws IOException {
byte[] scratch = aux;

byte[] ptrBytes = src.unsafeBytes();
int ptr = src.begin();
int len = src.realSize();

int beg = 0;
int pos = 0;
int beg = src.begin();
int pos = beg;
int len = beg + src.realSize();

// There are optimizations in JRuby where ptr > 0 will be true. For example, if we
// slice a string, the underlying byte array is the same, but the
// begin index and real size are different. When reading from the ptrBytes
// array, we need to always add ptr to the index.
ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, ptr + len);
ByteBuffer bb = ByteBuffer.wrap(ptrBytes, 0, len);

while (pos + 8 <= len) {
long x = bb.getLong(ptr + pos);
long x = bb.getLong(pos);
if (skipChunk(x)) {
pos += 8;
continue;
}
int chunkEnd = ptr + pos + 8;
while (ptr + pos < chunkEnd) {
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
int chunkEnd = pos + 8;
while (pos < chunkEnd) {
int ch = Byte.toUnsignedInt(ptrBytes[pos]);
int ch_len = ESCAPE_TABLE[ch];
if (ch_len > 0) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
} else {
pos++;
Expand All @@ -49,25 +47,25 @@ void encode(ByteList src) throws IOException {
}

if (pos + 4 <= len) {
int x = bb.getInt(ptr + pos);
int x = bb.getInt(pos);
if (skipChunk(x)) {
pos += 4;
}
}

while (pos < len) {
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
int ch = Byte.toUnsignedInt(ptrBytes[pos]);
int ch_len = ESCAPE_TABLE[ch];
if (ch_len > 0) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
} else {
pos++;
}
}

if (beg < len) {
append(ptrBytes, ptr + beg, len - beg);
append(ptrBytes, beg, len - beg);
}
}

Expand Down
51 changes: 24 additions & 27 deletions java/src/json/ext/StringEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,55 +251,54 @@ void encodeBasic(ByteList src) throws IOException {
byte[] scratch = aux;

byte[] ptrBytes = src.unsafeBytes();
int ptr = src.begin();
int len = src.realSize();

int beg = 0;
int pos = 0;
int beg = src.begin();
int pos = beg;

while (pos < len) {
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
int ch = Byte.toUnsignedInt(ptrBytes[pos]);
int ch_len = ESCAPE_TABLE[ch];
if (ch_len > 0) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
} else {
pos++;
}
}

if (beg < len) {
append(ptrBytes, ptr + beg, len - beg);
append(ptrBytes, beg, len - beg);
}
}

protected boolean isUnsafeScriptTag(byte[] ptrBytes, int pos, int len, int ptr) {
protected boolean isUnsafeScriptTag(byte[] ptrBytes, int pos, int len) {
int remaining = len - pos;

if (remaining < 4) {
return false;
}

if (ByteBuffer.wrap(ptrBytes, ptr + pos, 4).equals(HTML_COMMENT)) {
if (ByteBuffer.wrap(ptrBytes, pos, 4).equals(HTML_COMMENT)) {
return true;
}

pos++;
remaining--;

if (ptrBytes[ptr + pos] == '/') {
if (ptrBytes[pos] == '/') {
pos++;
remaining--;
}

return (
remaining >= 6 &&
(ptrBytes[ptr + pos + 0] | 0x20) == 's' &&
(ptrBytes[ptr + pos + 1] | 0x20) == 'c' &&
(ptrBytes[ptr + pos + 2] | 0x20) == 'r' &&
(ptrBytes[ptr + pos + 3] | 0x20) == 'i' &&
(ptrBytes[ptr + pos + 4] | 0x20) == 'p' &&
(ptrBytes[ptr + pos + 5] | 0x20) == 't'
(ptrBytes[pos + 0] | 0x20) == 's' &&
(ptrBytes[pos + 1] | 0x20) == 'c' &&
(ptrBytes[pos + 2] | 0x20) == 'r' &&
(ptrBytes[pos + 3] | 0x20) == 'i' &&
(ptrBytes[pos + 4] | 0x20) == 'p' &&
(ptrBytes[pos + 5] | 0x20) == 't'
);
}

Expand All @@ -315,14 +314,12 @@ void encode(ByteList src) throws IOException {
byte[] escapeTable = this.escapeTable;

byte[] ptrBytes = src.unsafeBytes();
int ptr = src.begin();
int len = src.realSize();

int beg = 0;
int pos = 0;
int beg = src.begin();
int pos = beg;
int len = beg + src.realSize();

while (pos < len) {
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
int ch = Byte.toUnsignedInt(ptrBytes[pos]);
int ch_len = escapeTable[ch];
/* JSON encoding */

Expand All @@ -331,13 +328,13 @@ void encode(ByteList src) throws IOException {
pos++;
break;
case 9: {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
break;
}
case 25: {
if (isUnsafeScriptTag(ptrBytes, pos, len, ptr)) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
if (isUnsafeScriptTag(ptrBytes, pos, len)) {
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
} else {
pos++; // nothing to escape, resume search.
Expand All @@ -351,12 +348,12 @@ void encode(ByteList src) throws IOException {
}

if (beg < len) {
append(ptrBytes, ptr + beg, len - beg);
append(ptrBytes, beg, len - beg);
}
}

protected int flushPos(int pos, int beg, byte[] ptrBytes, int ptr, int size) throws IOException {
if (pos > beg) { append(ptrBytes, ptr + beg, pos - beg); }
protected int flushPos(int pos, int beg, byte[] ptrBytes, int size) throws IOException {
if (pos > beg) { append(ptrBytes, beg, pos - beg); }
return pos + size;
}

Expand Down
27 changes: 13 additions & 14 deletions java/src/json/ext/StringEncoderAsciiOnly.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,27 @@ void encode(ByteList src) throws IOException {
byte[] escapeTable = this.escapeTable;

byte[] ptrBytes = src.unsafeBytes();
int ptr = src.begin();
int len = src.realSize();

int beg = 0;
int pos = 0;
int beg = src.begin();
int pos = beg;
int len = beg + src.realSize();

while (pos < len) {
int ch = Byte.toUnsignedInt(ptrBytes[ptr + pos]);
int ch = Byte.toUnsignedInt(ptrBytes[pos]);
int ch_len = escapeTable[ch];

switch (ch_len) {
case 0:
pos++;
break;
case 9: {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
break;
}
case 25: {
if (isUnsafeScriptTag(ptrBytes, pos, len, ptr)) {
beg = pos = flushPos(pos, beg, ptrBytes, ptr, 1);
if (isUnsafeScriptTag(ptrBytes, pos, len)) {
beg = pos = flushPos(pos, beg, ptrBytes, 1);
escapeAscii(ch, scratch, hexdig);
} else {
pos++; // nothing to escape, resume search.
Expand All @@ -71,21 +70,21 @@ void encode(ByteList src) throws IOException {

switch(ch_len) {
case 2:
wchar = ptrBytes[ptr + pos] & 0x1F;
wchar = ptrBytes[pos] & 0x1F;
break;
case 3:
wchar = ptrBytes[ptr + pos] & 0x0F;
wchar = ptrBytes[pos] & 0x0F;
break;
case 4:
wchar = ptrBytes[ptr + pos] & CHAR_LENGTH_MASK;
wchar = ptrBytes[pos] & CHAR_LENGTH_MASK;
break;
}

for (short i = 1; i < ch_len; i++) {
wchar = (wchar << 6) | (ptrBytes[ptr + pos +i] & 0x3F);
wchar = (wchar << 6) | (ptrBytes[pos + i] & 0x3F);
}

beg = pos = flushPos(pos, beg, ptrBytes, ptr, ch_len);
beg = pos = flushPos(pos, beg, ptrBytes, ch_len);

if (wchar <= 0xFFFF) {
scratch[2] = hexdig[wchar >> 12];
Expand Down Expand Up @@ -118,7 +117,7 @@ void encode(ByteList src) throws IOException {
}

if (beg < len) {
append(ptrBytes, ptr + beg, len - beg);
append(ptrBytes, beg, len - beg);
}
}
}
Loading