MCPcopy Create free account
hub / github.com/antlr/codebuff / fromString

Method fromString

corpus/java/training/guava/net/HostAndPort.java:166–203  ·  view source on GitHub ↗

Split a freeform string into a host and port, without strict validation. Note that the host-only formats will leave the port field undefined. You can use #withDefaultPort(int) to patch in a default value. @param hostPortString the input string to parse. @return if parsing was successful, a

(String hostPortString)

Source from the content-addressed store, hash-verified

164 * @throws IllegalArgumentException if nothing meaningful could be parsed.
165 */
166 public static HostAndPort fromString(String hostPortString) {
167 checkNotNull(hostPortString);
168 String host;
169 String portString = null;
170 boolean hasBracketlessColons = false;
171
172 if (hostPortString.startsWith("[")) {
173 String[] hostAndPort = getHostAndPortFromBracketedHost(hostPortString);
174 host = hostAndPort[0];
175 portString = hostAndPort[1];
176 } else {
177 int colonPos = hostPortString.indexOf(':');
178 if (colonPos >= 0 && hostPortString.indexOf(':', colonPos + 1) == -1) {
179 // Exactly 1 colon. Split into host:port.
180 host = hostPortString.substring(0, colonPos);
181 portString = hostPortString.substring(colonPos + 1);
182 } else {
183 // 0 or 2+ colons. Bare hostname or IPv6 literal.
184 host = hostPortString;
185 hasBracketlessColons = (colonPos >= 0);
186 }
187 }
188
189 int port = NO_PORT;
190 if (!Strings.isNullOrEmpty(portString)) {
191 // Try to parse the whole port string as a number.
192 // JDK7 accepts leading plus signs. We don't want to.
193 checkArgument(!portString.startsWith("+"), "Unparseable port number: %s", hostPortString);
194 try {
195 port = Integer.parseInt(portString);
196 } catch (NumberFormatException e) {
197 throw new IllegalArgumentException("Unparseable port number: " + hostPortString);
198 }
199 checkArgument(isValidPort(port), "Port number out of range: %s", hostPortString);
200 }
201
202 return new HostAndPort(host, port, hasBracketlessColons);
203 }
204
205 /**
206 * Parses a bracketed host-port string, throwing IllegalArgumentException if parsing fails.

Callers 3

fromPartsMethod · 0.95
fromHostMethod · 0.95
fromValidMethod · 0.95

Calls 6

isNullOrEmptyMethod · 0.95
isValidPortMethod · 0.95
checkNotNullMethod · 0.45
indexOfMethod · 0.45
checkArgumentMethod · 0.45

Tested by

no test coverage detected