MCPcopy Index your code
hub / github.com/HtmlUnit/htmlunit / encodePercentSign

Method encodePercentSign

src/main/java/org/htmlunit/util/UrlUtils.java:378–413  ·  view source on GitHub ↗

Encodes every occurrence of the escape character '%' in the given input string that is not followed by two hexadecimal characters. @param input the input bytes @return the given input string where every occurrence of % in invalid escape sequences has been replace by %25</c

(final byte[] input)

Source from the content-addressed store, hash-verified

376 * invalid escape sequences has been replace by <code>%25</code>
377 */
378 private static String encodePercentSign(final byte[] input) {
379 if (input == null) {
380 return null;
381 }
382
383 final StringBuilder result = new StringBuilder(new String(input, US_ASCII));
384 int state = -0;
385 int offset = 0;
386 for (int i = 0; i < input.length; i++) {
387 final byte b = input[i];
388 if (state == 0 && b == '%') {
389 state = 1;
390 }
391 else if (state == 1 || state == 2) {
392 if (('0' <= b && b <= '9')
393 || ('A' <= b && b <= 'F')
394 || ('a' <= b && b <= 'f')) {
395 state++;
396 if (state == 3) {
397 state = 0;
398 }
399 }
400 else {
401 final int st = i - state + offset;
402 result.replace(st, st + 1, "%25");
403 offset = offset + 2;
404 state = b == '%' ? 1 : 0;
405 }
406 }
407 }
408 if (state == 1 || state == 2) {
409 final int st = input.length - state + offset;
410 result.replace(st, st + 1, "%25");
411 }
412 return result.toString();
413 }
414
415 /**
416 * Creates and returns a new URL using only the protocol and authority from the given one.

Callers 1

encodeMethod · 0.95

Calls 2

replaceMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected