Minor cleanup

This commit is contained in:
Erik C. Thauvin 2023-01-06 18:32:47 -08:00
parent 4b746ba75c
commit 9f6b5a0913
2 changed files with 12 additions and 10 deletions

View file

@ -84,6 +84,7 @@ object UrlEncoder {
* encoding. * encoding.
*/ */
@JvmStatic @JvmStatic
@JvmOverloads
fun decode(source: String, plusToSpace: Boolean = false): String { fun decode(source: String, plusToSpace: Boolean = false): String {
if (source.isEmpty()) { if (source.isEmpty()) {
return source return source
@ -95,13 +96,13 @@ object UrlEncoder {
var bytesBuffer: ByteArray? = null var bytesBuffer: ByteArray? = null
var bytesPos = 0 var bytesPos = 0
var i = 0 var i = 0
var start = false var started = false
while (i < length) { while (i < length) {
ch = source[i] ch = source[i]
if (ch == '%') { if (ch == '%') {
if (!start) { if (!started) {
out.append(source, 0, i) out.append(source, 0, i)
start = true started = true
} }
if (bytesBuffer == null) { if (bytesBuffer == null) {
// the remaining characters divided by the length of the encoding format %xx, is the maximum number // the remaining characters divided by the length of the encoding format %xx, is the maximum number
@ -111,7 +112,7 @@ object UrlEncoder {
i++ i++
require(length >= i + 2) { "Illegal escape sequence" } require(length >= i + 2) { "Illegal escape sequence" }
try { try {
val v: Int = source.substring(i, i + 2).toInt(16) val v = source.substring(i, i + 2).toInt(16)
require(v in 0..0xFF) { "Illegal escape value" } require(v in 0..0xFF) { "Illegal escape value" }
bytesBuffer[bytesPos++] = v.toByte() bytesBuffer[bytesPos++] = v.toByte()
i += 2 i += 2
@ -121,19 +122,19 @@ object UrlEncoder {
} else { } else {
if (bytesBuffer != null) { if (bytesBuffer != null) {
out.append(String(bytesBuffer, 0, bytesPos, StandardCharsets.UTF_8)) out.append(String(bytesBuffer, 0, bytesPos, StandardCharsets.UTF_8))
start = true started = true
bytesBuffer = null bytesBuffer = null
bytesPos = 0 bytesPos = 0
} }
if (plusToSpace && ch == '+') { if (plusToSpace && ch == '+') {
if (!start) { if (!started) {
out.append(source, 0, i) out.append(source, 0, i)
} }
out.append(" ") out.append(" ")
start = true started = true
} else if (start) { } else if (started) {
out.append(ch) out.append(ch)
start = true started = true
} }
i++ i++
} }
@ -143,7 +144,7 @@ object UrlEncoder {
out.append(String(bytesBuffer, 0, bytesPos, StandardCharsets.UTF_8)) out.append(String(bytesBuffer, 0, bytesPos, StandardCharsets.UTF_8))
} }
return if (!start) source else out.toString() return if (!started) source else out.toString()
} }
/** /**

View file

@ -79,6 +79,7 @@ class UrlEncoderTest {
assertEquals("foo bar foo", decode("foo+bar++foo", true)) assertEquals("foo bar foo", decode("foo+bar++foo", true))
assertEquals("foo bar foo", decode("foo+%20bar%20+foo", true)) assertEquals("foo bar foo", decode("foo+%20bar%20+foo", true))
assertEquals("foo + bar", decode("foo+%2B+bar", plusToSpace = true)) assertEquals("foo + bar", decode("foo+%2B+bar", plusToSpace = true))
assertEquals("foo+bar", decode("foo%2Bbar", plusToSpace = true))
} }
@ParameterizedTest(name = "encode({0}) should be {1}") @ParameterizedTest(name = "encode({0}) should be {1}")