Changed the allow parameter to also take a string
This commit is contained in:
parent
f5201da64c
commit
c91770339d
4 changed files with 18 additions and 5 deletions
|
@ -108,7 +108,7 @@ tasks {
|
|||
archiveBaseName.set(rootProject.name)
|
||||
}
|
||||
|
||||
"sonarqube" {
|
||||
"sonar" {
|
||||
dependsOn(koverReport)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,6 @@
|
|||
<ID>MagicNumber:UrlEncoder.kt$UrlEncoder$16</ID>
|
||||
<ID>MagicNumber:UrlEncoder.kt$UrlEncoder$3</ID>
|
||||
<ID>MagicNumber:UrlEncoder.kt$UrlEncoder$4</ID>
|
||||
<ID>NestedBlockDepth:UrlEncoder.kt$UrlEncoder$@JvmStatic fun encode(source: String, vararg allow: Char): String</ID>
|
||||
<ID>NestedBlockDepth:UrlEncoder.kt$UrlEncoder$@JvmStatic fun encode(source: String, allow: String): String</ID>
|
||||
</CurrentIssues>
|
||||
</SmellBaseline>
|
||||
|
|
|
@ -83,7 +83,7 @@ object UrlEncoder {
|
|||
ch = source[i]
|
||||
if (ch == '%') {
|
||||
if (out == null) {
|
||||
out = StringBuilder(source.length)
|
||||
out = StringBuilder(length)
|
||||
out.append(source, 0, i)
|
||||
}
|
||||
if (bytesBuffer == null) {
|
||||
|
@ -125,7 +125,7 @@ object UrlEncoder {
|
|||
* encoding. Letters, numbers, unreserved (`_-!.~'()*`) and allowed characters are left intact.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun encode(source: String, vararg allow: Char): String {
|
||||
fun encode(source: String, allow: String): String {
|
||||
if (source.isBlank()) {
|
||||
return source
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ object UrlEncoder {
|
|||
var i = 0
|
||||
while (i < source.length) {
|
||||
ch = source[i]
|
||||
if (ch.isUnreserved() || allow.contains(ch)) {
|
||||
if (ch.isUnreserved() || allow.indexOf(ch) != -1) {
|
||||
out?.append(ch)
|
||||
i++
|
||||
} else {
|
||||
|
@ -165,4 +165,13 @@ object UrlEncoder {
|
|||
|
||||
return out?.toString() ?: source
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a provided [String] object into a new string, containing only valid URL characters in the UTF-8
|
||||
* encoding. Letters, numbers, unreserved (`_-!.~'()*`) and allowed characters are left intact.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun encode(source: String, vararg allow: Char): String {
|
||||
return encode(source, String(allow))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.junit.Test
|
|||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import kotlin.test.assertSame
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
|
||||
class UrlEncoderTest {
|
||||
|
@ -53,10 +54,13 @@ class UrlEncoderTest {
|
|||
fun testEncode() {
|
||||
assertEquals("", encode(""))
|
||||
assertSame(same, encode(same))
|
||||
assertSame(same, encode(same, ""))
|
||||
assertTrue(encode("").isEmpty())
|
||||
validMap.forEach {
|
||||
assertEquals(it.value, encode(it.key))
|
||||
}
|
||||
assertEquals("?test=a%20test", encode("?test=a test", '=', '?'))
|
||||
assertEquals("?test=a%20test", encode("?test=a test", "=?"))
|
||||
assertEquals("aaa", encode("aaa", 'a'))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue