mirror of
https://github.com/gbevin/urlencoder.git
synced 2025-04-24 23:07:12 -07:00
Optimization to spaceToPlus support, tests cleanups.
This commit is contained in:
parent
5b293d9ca8
commit
b8322bfe9f
2 changed files with 30 additions and 31 deletions
|
@ -214,27 +214,26 @@ public final class UrlEncoder {
|
||||||
out = new StringBuilder(source.length());
|
out = new StringBuilder(source.length());
|
||||||
out.append(source, 0, i);
|
out.append(source, 0, i);
|
||||||
}
|
}
|
||||||
if (spaceToPlus && ch == ' ') {
|
var cp = source.codePointAt(i);
|
||||||
out.append('+');
|
if (cp < 0x80) {
|
||||||
i += 1;
|
if (spaceToPlus && ch == ' ') {
|
||||||
} else {
|
out.append('+');
|
||||||
var cp = source.codePointAt(i);
|
} else {
|
||||||
if (cp < 0x80) {
|
|
||||||
appendUrlEncodedByte(out, cp);
|
appendUrlEncodedByte(out, cp);
|
||||||
i += 1;
|
|
||||||
} else if (Character.isBmpCodePoint(cp)) {
|
|
||||||
for (var b : Character.toString(ch).getBytes(StandardCharsets.UTF_8)) {
|
|
||||||
appendUrlEncodedByte(out, b);
|
|
||||||
}
|
|
||||||
i += 1;
|
|
||||||
} else if (Character.isSupplementaryCodePoint(cp)) {
|
|
||||||
var high = Character.highSurrogate(cp);
|
|
||||||
var low = Character.lowSurrogate(cp);
|
|
||||||
for (var b : new String(new char[]{high, low}).getBytes(StandardCharsets.UTF_8)) {
|
|
||||||
appendUrlEncodedByte(out, b);
|
|
||||||
}
|
|
||||||
i += 2;
|
|
||||||
}
|
}
|
||||||
|
i += 1;
|
||||||
|
} else if (Character.isBmpCodePoint(cp)) {
|
||||||
|
for (var b : Character.toString(ch).getBytes(StandardCharsets.UTF_8)) {
|
||||||
|
appendUrlEncodedByte(out, b);
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
} else if (Character.isSupplementaryCodePoint(cp)) {
|
||||||
|
var high = Character.highSurrogate(cp);
|
||||||
|
var low = Character.lowSurrogate(cp);
|
||||||
|
for (var b : new String(new char[]{high, low}).getBytes(StandardCharsets.UTF_8)) {
|
||||||
|
appendUrlEncodedByte(out, b);
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ class UrlEncoderTest {
|
||||||
@ParameterizedTest(name = "processMain(-d {1}) should be {0}")
|
@ParameterizedTest(name = "processMain(-d {1}) should be {0}")
|
||||||
@MethodSource("validMap")
|
@MethodSource("validMap")
|
||||||
void testMainDecode(String expected, String source) {
|
void testMainDecode(String expected, String source) {
|
||||||
var result = UrlEncoder.processMain(new String[]{"-d", source});
|
var result = UrlEncoder.processMain("-d", source);
|
||||||
assertEquals(expected, result.output);
|
assertEquals(expected, result.output);
|
||||||
assertEquals(0, result.status, "processMain(-d " + source + ").status");
|
assertEquals(0, result.status, "processMain(-d " + source + ").status");
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ class UrlEncoderTest {
|
||||||
@ParameterizedTest(name = "processMain(-e {0})")
|
@ParameterizedTest(name = "processMain(-e {0})")
|
||||||
@MethodSource("validMap")
|
@MethodSource("validMap")
|
||||||
void testMainEncode(String source, String expected) {
|
void testMainEncode(String source, String expected) {
|
||||||
var result = UrlEncoder.processMain(new String[]{source});
|
var result = UrlEncoder.processMain(source);
|
||||||
assertEquals(expected, result.output);
|
assertEquals(expected, result.output);
|
||||||
assertEquals(0, result.status, "processMain(-e " + source + ").status");
|
assertEquals(0, result.status, "processMain(-e " + source + ").status");
|
||||||
}
|
}
|
||||||
|
@ -120,20 +120,20 @@ class UrlEncoderTest {
|
||||||
@ParameterizedTest(name = "processMain(-d {0})")
|
@ParameterizedTest(name = "processMain(-d {0})")
|
||||||
@MethodSource("invalid")
|
@MethodSource("invalid")
|
||||||
void testMainEncodeWithExceptions(String source) {
|
void testMainEncodeWithExceptions(String source) {
|
||||||
assertThrows(IllegalArgumentException.class, () -> UrlEncoder.processMain(new String[]{"-d", source}), source);
|
assertThrows(IllegalArgumentException.class, () -> UrlEncoder.processMain("-d", source), source);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMainTooManyArgs() {
|
void testMainTooManyArgs() {
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{"foo", "bar", "test"}).output.contains("Usage :"), "too many args");
|
assertTrue(UrlEncoder.processMain("foo", "bar", "test").output.contains("Usage :"), "too many args");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMainWithEmptyArgs() {
|
void testMainWithEmptyArgs() {
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{" ", " "}).output.contains("Usage :"), "processMain(' ', ' ')");
|
assertTrue(UrlEncoder.processMain(" ", " ").output.contains("Usage :"), "processMain(' ', ' ')");
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{"foo", " "}).output.contains("Usage :"), "processMain('foo', ' ')");
|
assertTrue(UrlEncoder.processMain("foo", " ").output.contains("Usage :"), "processMain('foo', ' ')");
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{" ", "foo"}).output.contains("Usage :"), "processMain(' ', 'foo')");
|
assertTrue(UrlEncoder.processMain(" ", "foo").output.contains("Usage :"), "processMain(' ', 'foo')");
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{"-d ", ""}).output.contains("Usage :"), "processMain('-d', '')");
|
assertTrue(UrlEncoder.processMain("-d ", "").output.contains("Usage :"), "processMain('-d', '')");
|
||||||
assertEquals("%20", UrlEncoder.processMain(new String[]{"-e", " "}).output, "processMain('-e', ' ')");
|
assertEquals("%20", UrlEncoder.processMain(new String[]{"-e", " "}).output, "processMain('-e', ' ')");
|
||||||
assertEquals(" ", UrlEncoder.processMain(new String[]{"-d", " "}).output, "processMain('-d', ' ')");
|
assertEquals(" ", UrlEncoder.processMain(new String[]{"-d", " "}).output, "processMain('-d', ' ')");
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,7 @@ class UrlEncoderTest {
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = {"", "-d", "-e"})
|
@ValueSource(strings = {"", "-d", "-e"})
|
||||||
void testMainWithInvalidArgs(String arg) {
|
void testMainWithInvalidArgs(String arg) {
|
||||||
var result = UrlEncoder.processMain(new String[]{arg});
|
var result = UrlEncoder.processMain(arg);
|
||||||
assertTrue(result.output.contains("Usage :"), "processMain('" + arg + "')");
|
assertTrue(result.output.contains("Usage :"), "processMain('" + arg + "')");
|
||||||
assertEquals(1, result.status, "processMain('" + arg + "').status");
|
assertEquals(1, result.status, "processMain('" + arg + "').status");
|
||||||
}
|
}
|
||||||
|
@ -149,14 +149,14 @@ class UrlEncoderTest {
|
||||||
@ParameterizedTest(name = "processMain(-e {0})")
|
@ParameterizedTest(name = "processMain(-e {0})")
|
||||||
@MethodSource("validMap")
|
@MethodSource("validMap")
|
||||||
void testMainWithOption(String source, String expected) {
|
void testMainWithOption(String source, String expected) {
|
||||||
var result = UrlEncoder.processMain(new String[]{"-e", source});
|
var result = UrlEncoder.processMain("-e", source);
|
||||||
assertEquals(expected, result.output);
|
assertEquals(expected, result.output);
|
||||||
assertEquals(0, result.status, "processMain(-e " + source + ").status");
|
assertEquals(0, result.status, "processMain(-e " + source + ").status");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testMainWithUnknownOptions() {
|
void testMainWithUnknownOptions() {
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{"-p"}).output.contains("Usage :"), "processMain(-p)");
|
assertTrue(UrlEncoder.processMain("-p").output.contains("Usage :"), "processMain(-p)");
|
||||||
assertTrue(UrlEncoder.processMain(new String[]{"-"}).output.contains("Usage :"), "processMain(-)");
|
assertTrue(UrlEncoder.processMain("-").output.contains("Usage :"), "processMain(-)");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue