Move trimV4AddrZeros to libs/net

The utility is @UnsupportedAppUsage, and also used by internal classes
like WifiTrackerLib or Mms, so it needs to be in a shared location.

Bug: 182859030
Test: atest NetworkStaticLibTests
Change-Id: Ia2be2ef62ea1e7dfdff1f54a14d8f2e282d36fca
diff --git a/staticlibs/framework/com/android/net/module/util/Inet4AddressUtils.java b/staticlibs/framework/com/android/net/module/util/Inet4AddressUtils.java
index a1d34a0..87f43d5 100644
--- a/staticlibs/framework/com/android/net/module/util/Inet4AddressUtils.java
+++ b/staticlibs/framework/com/android/net/module/util/Inet4AddressUtils.java
@@ -163,4 +163,30 @@
             throws IllegalArgumentException {
         return intToInet4AddressHTH(prefixLengthToV4NetmaskIntHTH(prefixLength));
     }
+
+    /**
+     * Trim leading zeros from IPv4 address strings
+     * Non-v4 addresses and host names remain unchanged.
+     * For example, 192.168.000.010 -> 192.168.0.10
+     * @param addr a string representing an ip address
+     * @return a string properly trimmed
+     */
+    public static String trimAddressZeros(String addr) {
+        if (addr == null) return null;
+        String[] octets = addr.split("\\.");
+        if (octets.length != 4) return addr;
+        StringBuilder builder = new StringBuilder(16);
+        String result = null;
+        for (int i = 0; i < 4; i++) {
+            try {
+                if (octets[i].length() > 3) return addr;
+                builder.append(Integer.parseInt(octets[i]));
+            } catch (NumberFormatException e) {
+                return addr;
+            }
+            if (i < 3) builder.append('.');
+        }
+        result = builder.toString();
+        return result;
+    }
 }
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java
index 5d5ed91..702bdaf 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/Inet4AddressUtilsTest.java
@@ -26,9 +26,11 @@
 import static com.android.net.module.util.Inet4AddressUtils.netmaskToPrefixLength;
 import static com.android.net.module.util.Inet4AddressUtils.prefixLengthToV4NetmaskIntHTH;
 import static com.android.net.module.util.Inet4AddressUtils.prefixLengthToV4NetmaskIntHTL;
+import static com.android.net.module.util.Inet4AddressUtils.trimAddressZeros;
 
 import static junit.framework.Assert.assertEquals;
 
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
 import android.net.InetAddresses;
@@ -204,6 +206,17 @@
         getPrefixMaskAsInet4Address(-1);
     }
 
+    @Test
+    public void testTrimAddressZeros() {
+        assertNull(trimAddressZeros(null));
+        assertEquals("$invalid&", trimAddressZeros("$invalid&"));
+        assertEquals("example.com", trimAddressZeros("example.com"));
+        assertEquals("a.b.c.d", trimAddressZeros("a.b.c.d"));
+
+        assertEquals("192.0.2.2", trimAddressZeros("192.000.02.2"));
+        assertEquals("192.0.2.2", trimAddressZeros("192.0.2.2"));
+    }
+
     private Inet4Address ipv4Address(String addr) {
         return (Inet4Address) InetAddresses.parseNumericAddress(addr);
     }