dschneider-pivotal commented on a change in pull request #6765:
URL: https://github.com/apache/geode/pull/6765#discussion_r691708615



##########
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));
+  }
+
+  private static void appendLargeAsciiDigitsToByteBuf(long value, ByteBuf buf) 
{
+    long q;
+    int r;
+    final int MAX_DIGITS = 19;
+    int charPos = MAX_DIGITS;
+    byte[] bytes = new byte[MAX_DIGITS];
+
+    // Get 2 digits/iteration using longs until quotient fits into an int
+    while (value <= Integer.MIN_VALUE) {
+      q = value / 100;
+      r = (int) ((q * 100) - value);
+      value = q;
+      bytes[--charPos] = DIGIT_ONES[r];
+      bytes[--charPos] = DIGIT_TENS[r];
+    }
+
+    // Get 2 digits/iteration using ints
+    int q2;
+    int i2 = (int) value;
+    while (i2 <= -100) {
+      q2 = i2 / 100;
+      r = (q2 * 100) - i2;
+      i2 = q2;
+      bytes[--charPos] = DIGIT_ONES[r];
+      bytes[--charPos] = DIGIT_TENS[r];
+    }
+
+    // We know there are at most two digits left at this point.
+    q2 = i2 / 10;
+    r = (q2 * 10) - i2;
+    bytes[--charPos] = (byte) ('0' + r);
+
+    // Whatever left is the remaining digit.
+    if (q2 < 0) {
+      bytes[--charPos] = (byte) ('0' - q2);
+    }
+    buf.writeBytes(bytes, charPos, MAX_DIGITS - charPos);
+  }
+
+  @Immutable
+  private static final byte[] DIGIT_TENS = {

Review comment:
       these static arrays are now initialized with some static code using the 
byte constants.




-- 
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]


Reply via email to