Skip to content

Commit

Permalink
Reduce warnings in WhatWgUrlParser
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Oct 9, 2024
1 parent f4967f2 commit 8520fa5
Showing 1 changed file with 16 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* @author Arjen Poutsma
* @since 6.2
*/
@SuppressWarnings({"SameParameterValue", "BooleanMethodIsAlwaysInverted"})
final class WhatWgUrlParser {

public static final UrlRecord EMPTY_RECORD = new UrlRecord();
Expand Down Expand Up @@ -140,7 +141,7 @@ public static UrlRecord parse(String input, @Nullable UrlRecord base,
/**
* The basic URL parser takes a scalar value string input, with an optional
* null or base URL base (default null), an optional encoding (default UTF-8),
* an optional UrlRecord, and an optional state override.
* and optionally, a UrlRecord and/or State overrides to start from.
*/
private UrlRecord basicUrlParser(@Nullable UrlRecord url, @Nullable State stateOverride) {
// If url is not given:
Expand Down Expand Up @@ -471,6 +472,7 @@ private static boolean isNonCharacter(int ch) {
ch == 0xEFFFE || ch == 0xEFFFF || ch == 0xFFFFE || ch == 0xFFFFF || ch == 0x10FFFE || ch == 0x10FFFF);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private static boolean isUrlCodePoint(int ch) {
return (isAsciiAlphaNumeric(ch) ||
ch == '!' || ch == '$' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' ||
Expand All @@ -490,10 +492,8 @@ private static int defaultPort(@Nullable String scheme) {
if (scheme != null) {
return switch (scheme) {
case "ftp" -> 21;
case "http" -> 80;
case "https" -> 443;
case "ws" -> 80;
case "wss" -> 443;
case "http", "ws" -> 80;
case "https", "wss" -> 443;
default -> -1;
};
}
Expand Down Expand Up @@ -686,10 +686,9 @@ private static boolean isDoubleDotPathSegment(StringBuilder b) {

/**
* A Windows drive letter is two code points, of which the first is an ASCII alpha
* and the second is either U+003A (:) or U+007C (|).
*
* and the second is either U+003A {@code (:)} or U+007C {@code (|)}.
* A normalized Windows drive letter is a Windows drive letter of which
* the second code point is U+003A (:).
* the second code point is U+003A {@code (:)}.
*/
private static boolean isWindowsDriveLetter(CharSequence input, boolean normalized) {
if (input.length() != 2) {
Expand All @@ -699,8 +698,7 @@ private static boolean isWindowsDriveLetter(CharSequence input, boolean normaliz
}

/**
* A string starts with a Windows drive letter if all of the following are true:
*
* A string starts with a Windows drive letter if all the following are true:
* its length is greater than or equal to 2
* its first two code points are a Windows drive letter
* its length is 2 or its third code point is U+002F (/), U+005C (\), U+003F (?), or U+0023 (#).
Expand Down Expand Up @@ -1204,7 +1202,6 @@ else if (p.stateOverride != null && p.buffer.isEmpty() &&
// If state override is given, then return.
if (p.stateOverride != null) {
p.stopMainLoop = true;
return;
}
}
// Otherwise:
Expand Down Expand Up @@ -1656,7 +1653,7 @@ public void handle(int c, UrlRecord url, WhatWgUrlParser p) {
}
}
// Otherwise, if c is not the EOF code point:
else if (c != EOF) {
else {
if (p.validate()) {
// If c is not a URL code point and not U+0025 (%), invalid-URL-unit validation error.
if (!isUrlCodePoint(c) && c != '%') {
Expand Down Expand Up @@ -1769,39 +1766,6 @@ public boolean hasOpaquePath() {
}


/**
* The serialization of an origin is the string obtained by applying
* the following algorithm to the given origin:
* <ol>
* <li>If origin is an opaque origin, then return "null".
* <li>Otherwise, let result be origin's scheme.
* <li>Append "://" to result.
* Append origin's host, serialized, to result.
* <li>If origin's port is non-null, append a U+003A COLON character (:),
* and origin's port, serialized, to result.
* <li>Return result.
* </ol>
*/
public String origin() {
String scheme = scheme();
if (scheme.equals("ftp") ||
scheme.equals("http") || scheme.equals("https") ||
scheme.equals("ws") || scheme.equals("wss")) {
StringBuilder builder = new StringBuilder(scheme);
builder.append("://");
builder.append(host());
Port port = port();
if (port != null) {
builder.append(':');
builder.append(port);
}
return builder.toString();
}
else {
return "null";
}
}

/**
* A URL’s scheme is an ASCII string that identifies the type of URL and
* can be used to dispatch a URL for further processing after parsing.
Expand All @@ -1814,6 +1778,7 @@ public String scheme() {
/**
* The protocol getter steps are to return this’s URL’s scheme, followed by U+003A (:).
*/
@SuppressWarnings("unused")
public String protocol() {
return scheme() + ":";
}
Expand Down Expand Up @@ -1899,6 +1864,7 @@ public Host host() {
* <li>Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized.
* </ol>
*/
@SuppressWarnings("unused")
public String hostString() {
if (host() == null) {
return "";
Expand Down Expand Up @@ -2001,6 +1967,7 @@ public String fragment() {
* <li>Return U+0023 (#), followed by this’s URL’s fragment.
* </ol>
*/
@SuppressWarnings("unused")
public String hash() {
String fragment = fragment();
return (fragment != null && !fragment.isEmpty() ? "#" + fragment : "");
Expand Down Expand Up @@ -2248,6 +2215,7 @@ static final class IpAddressHost implements Host {
}
}

@SuppressWarnings("unused")
public IpAddress address() {
return this.address;
}
Expand Down Expand Up @@ -2776,7 +2744,7 @@ else if (c != EOF) {
}
// Otherwise, if compress is null and pieceIndex is not 8,
// IPv6-too-few-pieces validation error, return failure.
else if (compress == null && pieceIndex != 8) {
else if (pieceIndex != 8) {
throw new InvalidUrlException("An uncompressed IPv6 address contains fewer than 8 pieces.");
}
// Return address.
Expand Down Expand Up @@ -3019,6 +2987,7 @@ public boolean isOpaque() {
return true;
}

@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public Path clone() {
return new PathSegment(segment());
Expand Down Expand Up @@ -3103,6 +3072,7 @@ public boolean isOpaque() {
return false;
}

@SuppressWarnings("MethodDoesntCallSuperMethod")
@Override
public Path clone() {
return new PathSegments(this.segments);
Expand Down

0 comments on commit 8520fa5

Please sign in to comment.