dschneider-pivotal commented on a change in pull request #6765:
URL: https://github.com/apache/geode/pull/6765#discussion_r691708291
##########
File path:
geode-apis-compatible-with-redis/src/main/java/org/apache/geode/redis/internal/netty/Coder.java
##########
@@ -471,4 +472,101 @@ public static int narrowLongToInt(long toBeNarrowed) {
}
return doubleBytes;
}
+
+ /**
+ * Takes the given "value" and computes the sequence of ASCII digits it
represents,
+ * appending them to the given "buf".
+ * This code was adapted from the openjdk Long.java getChars methods.
+ */
+ public static void appendAsciiDigitsToByteBuf(long value, ByteBuf buf) {
+ if (value < 0) {
+ buf.writeByte('-');
+ } else {
+ value = -value;
+ }
+ // at this point value <= 0
+
+ if (value > -100) {
+ // it has at most two digits: [0..99]
+ appendSmallAsciiDigitsToByteBuf((int) value, buf);
+ } else {
+ appendLargeAsciiDigitsToByteBuf(value, buf);
+ }
+ }
+
+ private static void appendSmallAsciiDigitsToByteBuf(int value, ByteBuf buf) {
+ int q = value / 10;
+ int r = (q * 10) - value;
+ if (q < 0) {
+ buf.writeByte((byte) ('0' - q));
+ }
+ buf.writeByte((byte) ('0' + r));
Review comment:
done
##########
File path:
geode-apis-compatible-with-redis/src/test/java/org/apache/geode/redis/internal/netty/CoderTest.java
##########
@@ -176,4 +180,25 @@ public void
stripTrailingZeroFromDouble_correctlyStripsTrailingZero(byte[] input
};
}
+ @Test
+ public void verify_appendAsciiDigitsToByteBuf_conversions() {
+ verify_appendAsciiDigitsToByteBuf(Long.MAX_VALUE);
+ verify_appendAsciiDigitsToByteBuf(Long.MIN_VALUE);
+ verify_appendAsciiDigitsToByteBuf(Integer.MAX_VALUE);
+ verify_appendAsciiDigitsToByteBuf(Integer.MIN_VALUE);
+ verify_appendAsciiDigitsToByteBuf(Short.MAX_VALUE);
+ verify_appendAsciiDigitsToByteBuf(Short.MIN_VALUE);
+ for (long i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ verify_appendAsciiDigitsToByteBuf(i);
+ }
+ }
+
+ private void verify_appendAsciiDigitsToByteBuf(long value) {
+ String expected = Long.toString(value);
+ ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
+
+ Coder.appendAsciiDigitsToByteBuf(value, buf);
+
+ assertThat(buf.toString(Charset.defaultCharset())).isEqualTo(expected);
Review comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]