From 01c46bbdc9e4fe6b1c1b2f40c668115688dc747e Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 4 Jun 2023 11:39:41 -0700 Subject: [PATCH] Fixed sonar code smells --- .../net/thauvin/erik/urlencoder/UrlEncoder.kt | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/src/main/kotlin/net/thauvin/erik/urlencoder/UrlEncoder.kt b/lib/src/main/kotlin/net/thauvin/erik/urlencoder/UrlEncoder.kt index 8636b28..d03145e 100644 --- a/lib/src/main/kotlin/net/thauvin/erik/urlencoder/UrlEncoder.kt +++ b/lib/src/main/kotlin/net/thauvin/erik/urlencoder/UrlEncoder.kt @@ -65,7 +65,7 @@ object UrlEncoder { // see https://www.rfc-editor.org/rfc/rfc3986#page-13 // and https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set private fun Char.isUnreserved(): Boolean { - return this <= 'z' && unreservedChars.get(code) + return this <= 'z' && unreservedChars[code] } private fun StringBuilder.appendEncodedDigit(digit: Int) { @@ -171,25 +171,29 @@ object UrlEncoder { out.append(source, 0, i) } val cp = source.codePointAt(i) - if (cp < 0x80) { - if (spaceToPlus && ch == ' ') { - out.append('+') - } else { - out.appendEncodedByte(cp) + when { + cp < 0x80 -> { + if (spaceToPlus && ch == ' ') { + out.append('+') + } else { + out.appendEncodedByte(cp) + } + i++ } - i++ - } else if (Character.isBmpCodePoint(cp)) { - for (b in ch.toString().toByteArray(StandardCharsets.UTF_8)) { - out.appendEncodedByte(b.toInt()) + Character.isBmpCodePoint(cp) -> { + for (b in ch.toString().toByteArray(StandardCharsets.UTF_8)) { + out.appendEncodedByte(b.toInt()) + } + i++ } - i++ - } else if (Character.isSupplementaryCodePoint(cp)) { - val high = Character.highSurrogate(cp) - val low = Character.lowSurrogate(cp) - for (b in charArrayOf(high, low).concatToString().toByteArray(StandardCharsets.UTF_8)) { - out.appendEncodedByte(b.toInt()) + Character.isSupplementaryCodePoint(cp) -> { + val high = Character.highSurrogate(cp) + val low = Character.lowSurrogate(cp) + for (b in charArrayOf(high, low).concatToString().toByteArray(StandardCharsets.UTF_8)) { + out.appendEncodedByte(b.toInt()) + } + i += 2 } - i += 2 } } }