Skip to content

Commit 5732b83

Browse files
authored
chore: add convenience function for decoding URL components (#607)
1 parent b3704ff commit 5732b83

File tree

2 files changed

+17
-0
lines changed
  • runtime/utils/common
    • src/aws/smithy/kotlin/runtime/util/text
    • test/aws/smithy/kotlin/runtime/util/text

2 files changed

+17
-0
lines changed

runtime/utils/common/src/aws/smithy/kotlin/runtime/util/text/Text.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,13 @@ public fun String.splitAsQueryString(): Map<String, List<String>> {
129129
}
130130
return entries
131131
}
132+
133+
private val percentEncodedParam = """%([A-Fa-f0-9]{2})""".toRegex()
134+
135+
/**
136+
* Decode a URL's query string, resolving percent-encoding (e.g., "%3B" → ";").
137+
*/
138+
@InternalApi
139+
public fun String.urlDecodeComponent(): String = this
140+
.replace('+', ' ')
141+
.replace(percentEncodedParam) { it.groupValues[1].toInt(radix = 16).toChar().toString() }

runtime/utils/common/test/aws/smithy/kotlin/runtime/util/text/TextTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,11 @@ class TextTest {
112112
actualValueWithEquals.shouldContain(entry.key, entry.value)
113113
}
114114
}
115+
116+
@Test
117+
fun decodeUrlComponent() {
118+
val component = "a%3Bb+c%7Ed%20e%2Bf"
119+
val expected = "a;b c~d e+f"
120+
assertEquals(expected, component.urlDecodeComponent())
121+
}
115122
}

0 commit comments

Comments
 (0)