From c5e69709c0c1a452585d79a48411e43ed415ac27 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 19 Sep 2024 08:53:19 -0400 Subject: [PATCH] Query strings once --- .../commons/validator/routines/InetAddressValidator.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java b/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java index 07d90038b..868c251db 100644 --- a/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java +++ b/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java @@ -159,16 +159,19 @@ public boolean isValidInet6Address(String inet6Address) { if (containsCompressedZeroes && inet6Address.indexOf("::") != inet6Address.lastIndexOf("::")) { return false; } - if (inet6Address.startsWith(":") && !inet6Address.startsWith("::") || inet6Address.endsWith(":") && !inet6Address.endsWith("::")) { + final boolean startsWithCompressed = inet6Address.startsWith("::"); + final boolean endsWithCompressed = inet6Address.endsWith("::"); + final boolean endsWithSep = inet6Address.endsWith(":"); + if (inet6Address.startsWith(":") && !startsWithCompressed || endsWithSep && !endsWithCompressed) { return false; } String[] octets = inet6Address.split(":"); if (containsCompressedZeroes) { final List octetList = new ArrayList<>(Arrays.asList(octets)); - if (inet6Address.endsWith("::")) { + if (endsWithCompressed) { // String.split() drops ending empty segments octetList.add(""); - } else if (inet6Address.startsWith("::") && !octetList.isEmpty()) { + } else if (startsWithCompressed && !octetList.isEmpty()) { octetList.remove(0); } octets = octetList.toArray(new String[0]);