thomasmueller commented on code in PR #1599:
URL: https://github.com/apache/jackrabbit-oak/pull/1599#discussion_r1694838649


##########
oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/LongUtilsTest.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.commons;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+public class LongUtilsTest {
+
+    @Test
+    public void tryParse() {
+        assertParsingSuccess("-1");
+        assertParsingSuccess("0");
+        assertParsingSuccess("1");
+        assertParsingSuccess(String.valueOf(Long.MAX_VALUE));
+        assertParsingSuccess(String.valueOf(Long.MIN_VALUE));
+        assertParsingSuccess(String.valueOf(Integer.MIN_VALUE));
+        assertParsingSuccess(String.valueOf(Integer.MAX_VALUE));
+        assertParsingSuccess("-0");
+        for (long i = Long.MIN_VALUE; i < Long.MIN_VALUE + 100; i++) {
+            assertParsingSuccess(String.valueOf(i));
+        }
+        for (long i = Long.MAX_VALUE; i > Long.MAX_VALUE - 100; i--) {
+            assertParsingSuccess(String.valueOf(i));
+        }
+
+        assertParsingFailure("0.1");
+        assertParsingFailure("1.1");
+        assertParsingFailure("-1.1");
+        assertParsingFailure("1.1e3");
+        assertParsingFailure("foo");
+        assertParsingFailure("");
+        assertParsingFailure("-");
+        assertParsingFailure("9223372036854775808"); // Long.MAX_VALUE + 1
+        assertParsingFailure("9223372036854775809"); // Long.MAX_VALUE + 2
+        assertParsingFailure("-9223372036854775809"); // Long.MIN_VALUE - 1
+        assertParsingFailure("-9223372036854775810"); // Long.MIN_VALUE - 2
+        assertParsingFailure("122229223372036854775809");
+        assertParsingFailure("-122229223372036854775809");
+    }
+
+    private void assertParsingSuccess(String value) {
+        assertEquals(Long.parseLong(value), 
LongUtils.tryParse(value).longValue());
+    }
+
+    private void assertParsingFailure(String value) {
+        try {
+            Long.parseLong(value);

Review Comment:
   Could we verify that the parsed value matches the one from Long.parseLong ?



##########
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 think replacing "radix" with a constant RADIX should be simple (given that 
the value is fixed). That would be a compromise.



##########
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
+     * <a 
href="https://github.com/google/guava/blob/0c33dd12b193402cdf6962d43d69743521aa2f76/guava/src/com/google/common/primitives/Longs.java#L400";>Longs.tryParse()</a>
+     * in Guava. This version is hardcoded to only support radix 10.
+     * <p>
+     *
+     * @see 
org.apache.jackrabbit.guava.common.primitives.Longs#tryParse(String)
+     */
+    public static Long tryParse(String string) {
+        if (string == null) {
+            return null;
+        }
+        int radix = 10;
+        boolean negative = string.charAt(0) == '-';
+        int index = negative ? 1 : 0;
+        if (index == string.length()) {
+            return null;
+        }
+        int digit = AsciiDigits.digit(string.charAt(index++));
+        if (digit < 0 || digit >= radix) {
+            return null;
+        }
+        long accum = -digit;

Review Comment:
   Well I think we should be able to understand it... if not, then more tests 
are needed.



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