@Test public void differentFormats() { String expected = "[2001:db8::1:0:0:1]"; assertEquals(expected, Hosts.parse("[2001:db8:0:0:1:0:0:1]").name()); assertEquals(expected, Hosts.parse("[2001:0db8:0:0:1:0:0:1]").name()); assertEquals(expected, Hosts.parse("[2001:db8::1:0:0:1]").name()); assertEquals(expected, Hosts.parse("[2001:db8::0:1:0:0:1]").name()); assertEquals(expected, Hosts.parse("[2001:0db8::1:0:0:1]").name()); assertEquals(expected, Hosts.parse("[2001:db8:0:0:1::1]").name()); assertEquals(expected, Hosts.parse("[2001:db8:0000:0:1::1]").name()); assertEquals(expected, Hosts.parse("[2001:DB8:0:0:1::1]").name()); }
static Ip6 parse(String ip) { int start = 0; int end = ip.length(); if (ip.startsWith("[")) { if (!ip.endsWith("]")) { return null; } start++; end--; } switch (end - start) { case 0: case 1: return null; case 2: return "::".equals(ip.substring(start, end)) ? fromAddress(new int[8]) : null; default: } int[] addr = new int[ADDRLEN]; int addrPointer = 0; int compressionStarts = -1; if (ip.charAt(start) == ':') { if (ip.charAt(start + 1) != ':') { return null; } if (ip.charAt(start + 2) == ':') { return null; } compressionStarts = 0; start += 2; } for (int i = start; i < end; i++) { if (addrPointer == ADDRLEN) { return null; } if (ip.charAt(i) == ':') { if (compressionStarts != -1) { return null; } compressionStarts = addrPointer; continue; } int segEnd = Math.min(i + 4, end); int segVal = 0; for (; i < segEnd; i++) { char c = ip.charAt(i); if (c == ':') { break; } int hex = toHex(c); if (hex == -1) { return null; } segVal = segVal * 16 | hex; } addr[addrPointer++] = segVal; if (end == i) { break; } else if (ip.charAt(i) == ':') { if (i == end - 1) { return null; } } else { return null; } } if (compressionStarts == -1 && addrPointer < ADDRLEN - 1) { return null; } else if (compressionStarts > -1) { if (addrPointer == ADDRLEN) { return null; } for (int i = 1; i <= addrPointer - compressionStarts; i++) { addr[ADDRLEN - i] = addr[addrPointer - i]; addr[addrPointer - i] = 0; } } return fromAddress(addr); }
Ip6 implements Host { static Ip6 parse(String ip) { int start = 0; int end = ip.length(); if (ip.startsWith("[")) { if (!ip.endsWith("]")) { return null; } start++; end--; } switch (end - start) { case 0: case 1: return null; case 2: return "::".equals(ip.substring(start, end)) ? fromAddress(new int[8]) : null; default: } int[] addr = new int[ADDRLEN]; int addrPointer = 0; int compressionStarts = -1; if (ip.charAt(start) == ':') { if (ip.charAt(start + 1) != ':') { return null; } if (ip.charAt(start + 2) == ':') { return null; } compressionStarts = 0; start += 2; } for (int i = start; i < end; i++) { if (addrPointer == ADDRLEN) { return null; } if (ip.charAt(i) == ':') { if (compressionStarts != -1) { return null; } compressionStarts = addrPointer; continue; } int segEnd = Math.min(i + 4, end); int segVal = 0; for (; i < segEnd; i++) { char c = ip.charAt(i); if (c == ':') { break; } int hex = toHex(c); if (hex == -1) { return null; } segVal = segVal * 16 | hex; } addr[addrPointer++] = segVal; if (end == i) { break; } else if (ip.charAt(i) == ':') { if (i == end - 1) { return null; } } else { return null; } } if (compressionStarts == -1 && addrPointer < ADDRLEN - 1) { return null; } else if (compressionStarts > -1) { if (addrPointer == ADDRLEN) { return null; } for (int i = 1; i <= addrPointer - compressionStarts; i++) { addr[ADDRLEN - i] = addr[addrPointer - i]; addr[addrPointer - i] = 0; } } return fromAddress(addr); } }
Ip6 implements Host { static Ip6 parse(String ip) { int start = 0; int end = ip.length(); if (ip.startsWith("[")) { if (!ip.endsWith("]")) { return null; } start++; end--; } switch (end - start) { case 0: case 1: return null; case 2: return "::".equals(ip.substring(start, end)) ? fromAddress(new int[8]) : null; default: } int[] addr = new int[ADDRLEN]; int addrPointer = 0; int compressionStarts = -1; if (ip.charAt(start) == ':') { if (ip.charAt(start + 1) != ':') { return null; } if (ip.charAt(start + 2) == ':') { return null; } compressionStarts = 0; start += 2; } for (int i = start; i < end; i++) { if (addrPointer == ADDRLEN) { return null; } if (ip.charAt(i) == ':') { if (compressionStarts != -1) { return null; } compressionStarts = addrPointer; continue; } int segEnd = Math.min(i + 4, end); int segVal = 0; for (; i < segEnd; i++) { char c = ip.charAt(i); if (c == ':') { break; } int hex = toHex(c); if (hex == -1) { return null; } segVal = segVal * 16 | hex; } addr[addrPointer++] = segVal; if (end == i) { break; } else if (ip.charAt(i) == ':') { if (i == end - 1) { return null; } } else { return null; } } if (compressionStarts == -1 && addrPointer < ADDRLEN - 1) { return null; } else if (compressionStarts > -1) { if (addrPointer == ADDRLEN) { return null; } for (int i = 1; i <= addrPointer - compressionStarts; i++) { addr[ADDRLEN - i] = addr[addrPointer - i]; addr[addrPointer - i] = 0; } } return fromAddress(addr); }  }
Ip6 implements Host { static Ip6 parse(String ip) { int start = 0; int end = ip.length(); if (ip.startsWith("[")) { if (!ip.endsWith("]")) { return null; } start++; end--; } switch (end - start) { case 0: case 1: return null; case 2: return "::".equals(ip.substring(start, end)) ? fromAddress(new int[8]) : null; default: } int[] addr = new int[ADDRLEN]; int addrPointer = 0; int compressionStarts = -1; if (ip.charAt(start) == ':') { if (ip.charAt(start + 1) != ':') { return null; } if (ip.charAt(start + 2) == ':') { return null; } compressionStarts = 0; start += 2; } for (int i = start; i < end; i++) { if (addrPointer == ADDRLEN) { return null; } if (ip.charAt(i) == ':') { if (compressionStarts != -1) { return null; } compressionStarts = addrPointer; continue; } int segEnd = Math.min(i + 4, end); int segVal = 0; for (; i < segEnd; i++) { char c = ip.charAt(i); if (c == ':') { break; } int hex = toHex(c); if (hex == -1) { return null; } segVal = segVal * 16 | hex; } addr[addrPointer++] = segVal; if (end == i) { break; } else if (ip.charAt(i) == ':') { if (i == end - 1) { return null; } } else { return null; } } if (compressionStarts == -1 && addrPointer < ADDRLEN - 1) { return null; } else if (compressionStarts > -1) { if (addrPointer == ADDRLEN) { return null; } for (int i = 1; i <= addrPointer - compressionStarts; i++) { addr[ADDRLEN - i] = addr[addrPointer - i]; addr[addrPointer - i] = 0; } } return fromAddress(addr); }   }
Ip6 implements Host { static Ip6 parse(String ip) { int start = 0; int end = ip.length(); if (ip.startsWith("[")) { if (!ip.endsWith("]")) { return null; } start++; end--; } switch (end - start) { case 0: case 1: return null; case 2: return "::".equals(ip.substring(start, end)) ? fromAddress(new int[8]) : null; default: } int[] addr = new int[ADDRLEN]; int addrPointer = 0; int compressionStarts = -1; if (ip.charAt(start) == ':') { if (ip.charAt(start + 1) != ':') { return null; } if (ip.charAt(start + 2) == ':') { return null; } compressionStarts = 0; start += 2; } for (int i = start; i < end; i++) { if (addrPointer == ADDRLEN) { return null; } if (ip.charAt(i) == ':') { if (compressionStarts != -1) { return null; } compressionStarts = addrPointer; continue; } int segEnd = Math.min(i + 4, end); int segVal = 0; for (; i < segEnd; i++) { char c = ip.charAt(i); if (c == ':') { break; } int hex = toHex(c); if (hex == -1) { return null; } segVal = segVal * 16 | hex; } addr[addrPointer++] = segVal; if (end == i) { break; } else if (ip.charAt(i) == ':') { if (i == end - 1) { return null; } } else { return null; } } if (compressionStarts == -1 && addrPointer < ADDRLEN - 1) { return null; } else if (compressionStarts > -1) { if (addrPointer == ADDRLEN) { return null; } for (int i = 1; i <= addrPointer - compressionStarts; i++) { addr[ADDRLEN - i] = addr[addrPointer - i]; addr[addrPointer - i] = 0; } } return fromAddress(addr); }    }