Skip to content

Commit

Permalink
[Efficiency] Modifies textToNumericFormatV4 to parse the input String…
Browse files Browse the repository at this point in the history
… in place.

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=325432680
  • Loading branch information
DoublePointer authored and kluever committed Aug 7, 2020
1 parent 70ed7f8 commit 081c486
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void testForStringBogusInput() {
"42.42.42.42.",
"42.42.42.42...",
".42.42.42.42",
".42.42.42",
"...42.42.42.42",
"42.42.42.-0",
"42.42.42.+0",
Expand Down
36 changes: 24 additions & 12 deletions android/guava/src/com/google/common/net/InetAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.CharMatcher;
import com.google.common.base.MoreObjects;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -102,7 +103,8 @@
public final class InetAddresses {
private static final int IPV4_PART_COUNT = 4;
private static final int IPV6_PART_COUNT = 8;
private static final Splitter IPV4_SPLITTER = Splitter.on('.').limit(IPV4_PART_COUNT);
private static final char IPV4_DELIMITER = '.';
private static final CharMatcher IPV4_DELIMITER_MATCHER = CharMatcher.is(IPV4_DELIMITER);
private static final Splitter IPV6_SPLITTER = Splitter.on(':').limit(IPV6_PART_COUNT + 2);
private static final Inet4Address LOOPBACK4 = (Inet4Address) forString("127.0.0.1");
private static final Inet4Address ANY4 = (Inet4Address) forString("0.0.0.0");
Expand Down Expand Up @@ -197,24 +199,38 @@ private static byte[] ipStringToBytes(String ipString) {
}
return textToNumericFormatV6(ipString);
} else if (hasDot) {
if (percentIndex != -1) {
return null; // Scope IDs are not supported for IPV4
}
return textToNumericFormatV4(ipString);
}
return null;
}

@NullableDecl
private static byte[] textToNumericFormatV4(String ipString) {
if (IPV4_DELIMITER_MATCHER.countIn(ipString) + 1 != IPV4_PART_COUNT) {
return null; // Wrong number of parts
}

byte[] bytes = new byte[IPV4_PART_COUNT];
int i = 0;
try {
for (String octet : IPV4_SPLITTER.split(ipString)) {
bytes[i++] = parseOctet(octet);
int start = 0;
// Iterate through the parts of the ip string.
// Invariant: start is always the beginning of an octet.
for (int i = 0; i < IPV4_PART_COUNT; i++) {
int end = ipString.indexOf(IPV4_DELIMITER, start);
if (end == -1) {
end = ipString.length();
}
} catch (NumberFormatException ex) {
return null;
try {
bytes[i] = parseOctet(ipString, start, end);
} catch (NumberFormatException ex) {
return null;
}
start = end + 1;
}

return i == IPV4_PART_COUNT ? bytes : null;
return bytes;
}

@NullableDecl
Expand Down Expand Up @@ -295,10 +311,6 @@ private static String convertDottedQuadToHex(String ipString) {
return initialPart + penultimate + ":" + ultimate;
}

private static byte parseOctet(String ipPart) {
return parseOctet(ipPart, 0, ipPart.length());
}

private static byte parseOctet(String ipString, int start, int end) {
// Note: we already verified that this string contains only hex digits, but the string may still
// contain non-decimal characters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void testForStringBogusInput() {
"42.42.42.42.",
"42.42.42.42...",
".42.42.42.42",
".42.42.42",
"...42.42.42.42",
"42.42.42.-0",
"42.42.42.+0",
Expand Down
36 changes: 24 additions & 12 deletions guava/src/com/google/common/net/InetAddresses.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.base.CharMatcher;
import com.google.common.base.MoreObjects;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -102,7 +103,8 @@
public final class InetAddresses {
private static final int IPV4_PART_COUNT = 4;
private static final int IPV6_PART_COUNT = 8;
private static final Splitter IPV4_SPLITTER = Splitter.on('.').limit(IPV4_PART_COUNT);
private static final char IPV4_DELIMITER = '.';
private static final CharMatcher IPV4_DELIMITER_MATCHER = CharMatcher.is(IPV4_DELIMITER);
private static final Splitter IPV6_SPLITTER = Splitter.on(':').limit(IPV6_PART_COUNT + 2);
private static final Inet4Address LOOPBACK4 = (Inet4Address) forString("127.0.0.1");
private static final Inet4Address ANY4 = (Inet4Address) forString("0.0.0.0");
Expand Down Expand Up @@ -196,23 +198,37 @@ public static boolean isInetAddress(String ipString) {
}
return textToNumericFormatV6(ipString);
} else if (hasDot) {
if (percentIndex != -1) {
return null; // Scope IDs are not supported for IPV4
}
return textToNumericFormatV4(ipString);
}
return null;
}

private static byte @Nullable [] textToNumericFormatV4(String ipString) {
if (IPV4_DELIMITER_MATCHER.countIn(ipString) + 1 != IPV4_PART_COUNT) {
return null; // Wrong number of parts
}

byte[] bytes = new byte[IPV4_PART_COUNT];
int i = 0;
try {
for (String octet : IPV4_SPLITTER.split(ipString)) {
bytes[i++] = parseOctet(octet);
int start = 0;
// Iterate through the parts of the ip string.
// Invariant: start is always the beginning of an octet.
for (int i = 0; i < IPV4_PART_COUNT; i++) {
int end = ipString.indexOf(IPV4_DELIMITER, start);
if (end == -1) {
end = ipString.length();
}
} catch (NumberFormatException ex) {
return null;
try {
bytes[i] = parseOctet(ipString, start, end);
} catch (NumberFormatException ex) {
return null;
}
start = end + 1;
}

return i == IPV4_PART_COUNT ? bytes : null;
return bytes;
}

private static byte @Nullable [] textToNumericFormatV6(String ipString) {
Expand Down Expand Up @@ -291,10 +307,6 @@ public static boolean isInetAddress(String ipString) {
return initialPart + penultimate + ":" + ultimate;
}

private static byte parseOctet(String ipPart) {
return parseOctet(ipPart, 0, ipPart.length());
}

private static byte parseOctet(String ipString, int start, int end) {
// Note: we already verified that this string contains only hex digits, but the string may still
// contain non-decimal characters.
Expand Down

0 comments on commit 081c486

Please sign in to comment.