Skip to content

Commit

Permalink
Match C version of fbuffer_append_long
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 15, 2025
1 parent 92b24de commit f7eede3
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions java/src/json/ext/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,8 @@ private static void generateFixnum(Session session, RubyFixnum object, OutputStr
} else if (i == Long.MIN_VALUE) {
buffer.write(MIN_VALUE_BYTES_RADIX_10);
} else {
boolean neg = i < 0;
if (neg) i = -i;
int newSize = sizeWithDecimalString(i, neg, 0);
byte[] charBytes = session.getCharBytes();
writeDecimalDigitsToArray(charBytes, i, neg, 0, 0, newSize);
buffer.write(charBytes, 0, newSize);
appendFixnum(buffer, charBytes, i);
}
}

Expand All @@ -318,32 +314,24 @@ private static void generateFixnum(Session session, RubyFixnum object, OutputStr
MIN_VALUE_BYTES_RADIX_10 = ByteList.plain(Long.toString(Long.MIN_VALUE, 10));
}

private static int sizeWithDecimalString(long i, boolean neg, int baseSize) {
int count = 0;
while (i > 9) {
i /= 10;
count++;
}
int newSize = baseSize + count + 1;

if (neg) newSize++;

return newSize;
// C: fbuffer_append_long
static void appendFixnum(OutputStream buffer, byte[] buf, long number) throws IOException {
int buffer_end = buf.length;
int len = fltoa(number, buf, buffer_end - 1);
buffer.write(buf, buffer_end - len, len);
}

private static void writeDecimalDigitsToArray(byte[] bytes, long i, boolean negative, int begin, int originalSize, int newSize) {
// write digits directly into the prepared byte array
for (int n = newSize - 1; i > 0; n--) {
bytes[begin + n] = decimalByteForDigit(i);
i /= 10;
}
static int fltoa(long number, byte[] buf, int end) {
long sign = number;
int tmp = end;

if (negative) bytes[originalSize] = '-';
if (sign < 0) number = -number;
do buf[tmp--] = (byte) digits[(int) (number % 10)]; while ((number /= 10) != 0);
if (sign < 0) buf[tmp--] = '-';
return end - tmp;
}

private static byte decimalByteForDigit(long i) {
return (byte) (i % 10 + '0');
}
private static final char[] digits = {'0', '1','2','3','4','5','6','7','8','9'};

private static class FloatHandler extends Handler<RubyFloat> {
@Override
Expand Down

0 comments on commit f7eede3

Please sign in to comment.