diff --git a/java/src/json/ext/SWARBasicStringEncoder.java b/java/src/json/ext/SWARBasicStringEncoder.java index bd0d1a70..5ec967b9 100644 --- a/java/src/json/ext/SWARBasicStringEncoder.java +++ b/java/src/json/ext/SWARBasicStringEncoder.java @@ -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++; @@ -49,17 +47,17 @@ 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++; @@ -67,7 +65,7 @@ void encode(ByteList src) throws IOException { } if (beg < len) { - append(ptrBytes, ptr + beg, len - beg); + append(ptrBytes, beg, len - beg); } } diff --git a/java/src/json/ext/StringEncoder.java b/java/src/json/ext/StringEncoder.java index 38fa3642..a3715825 100644 --- a/java/src/json/ext/StringEncoder.java +++ b/java/src/json/ext/StringEncoder.java @@ -251,17 +251,16 @@ 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++; @@ -269,37 +268,37 @@ void encodeBasic(ByteList src) throws IOException { } 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' ); } @@ -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 */ @@ -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. @@ -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; } diff --git a/java/src/json/ext/StringEncoderAsciiOnly.java b/java/src/json/ext/StringEncoderAsciiOnly.java index 636d2dcb..463d0698 100644 --- a/java/src/json/ext/StringEncoderAsciiOnly.java +++ b/java/src/json/ext/StringEncoderAsciiOnly.java @@ -37,14 +37,13 @@ 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) { @@ -52,13 +51,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. @@ -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]; @@ -118,7 +117,7 @@ void encode(ByteList src) throws IOException { } if (beg < len) { - append(ptrBytes, ptr + beg, len - beg); + append(ptrBytes, beg, len - beg); } } }