nfsantos commented on code in PR #1599:
URL: https://github.com/apache/jackrabbit-oak/pull/1599#discussion_r1694767284
##########
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/LongUtils.java:
##########
@@ -51,4 +52,83 @@ public static long safeAdd(long a, long b) {
public static long calculateExpirationTime(long expiration) {
return LongUtils.safeAdd(expiration, new Date().getTime());
}
+
+ /*
+ * Taken from
https://github.com/google/guava/blob/0c33dd12b193402cdf6962d43d69743521aa2f76/guava/src/com/google/common/primitives/Longs.java#L334
+ */
+ private static final class AsciiDigits {
+ private AsciiDigits() {
+ }
+
+ private static final byte[] asciiDigits;
+
+ static {
+ byte[] result = new byte[128];
+ Arrays.fill(result, (byte) -1);
+ for (int i = 0; i < 10; i++) {
+ result['0' + i] = (byte) i;
+ }
+ for (int i = 0; i < 26; i++) {
+ result['A' + i] = (byte) (10 + i);
+ result['a' + i] = (byte) (10 + i);
+ }
+ asciiDigits = result;
+ }
+
+ static int digit(char c) {
+ return (c < 128) ? asciiDigits[c] : -1;
+ }
+ }
+
+ /**
+ * Parses the specified string as a signed decimal long value. Unlike
{@link Long#parseLong(String)}, this method
+ * returns {@code null} instead of throwing an exception if parsing fails.
This can be significantly more efficient
+ * when the string to be parsed is often invalid, as raising an exception
is an expensive operation.
+ *<p>
+ * This is a simplified version of
Review Comment:
I did not look carefully at the algorithm, I just assumed that it is
correct, given that it is part of Guava. So I did not want to make more changes
than those that seemed totally obvious and safe. And even so, I introduced a
bug when replacing the logic for checking for null/empty strings. I agree it
would be nice to get rid of AsciiDigits, but I'm not sure if it is worth the
effort and risk.
--
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]