Skip to content

Commit a2b725d

Browse files
📝 Add docstrings to mltheuser/ktor-6711
Docstrings generation was requested by @mltheuser. * #5238 (comment) The following files were modified: * `ktor-http/common/src/io/ktor/http/HttpUrlEncoded.kt`
1 parent e081285 commit a2b725d

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

ktor-http/common/src/io/ktor/http/HttpUrlEncoded.kt

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,75 @@ public fun String.parseUrlEncodedParameters(defaultEncoding: Charset = Charsets.
2929
}
3030

3131
/**
32-
* Encode form parameters from a list of pairs
33-
*
34-
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.http.formUrlEncode)
35-
*/
36-
public fun List<Pair<String, String?>>.formUrlEncode(): String = buildString { formUrlEncodeTo(this) }
32+
* Form-url-encodes a list of key/value pairs into an application/x-www-form-urlencoded string.
33+
*
34+
* @param spaceToPlus If `true`, spaces are encoded as `+`; if `false`, spaces are encoded as `%20`. Defaults to `true`.
35+
* @return The encoded form string containing joined `key=value` pairs separated by `&`. Values that are `null` are serialized as keys without `=`.
36+
*/
37+
public fun List<Pair<String, String?>>.formUrlEncode(spaceToPlus: Boolean = true): String =
38+
buildString { formUrlEncodeTo(this, spaceToPlus) }
3739

3840
/**
39-
* Encode form parameters from a list of pairs to the specified [out] appendable
41+
* Write this list of key/value pairs to the given Appendable using application/x-www-form-urlencoded encoding.
4042
*
41-
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.http.formUrlEncodeTo)
43+
* Each pair is serialized as `key` or `key=value` and pairs are joined with `&`. A `null` value produces the key without an `=`. Keys and values are percent-encoded; when [spaceToPlus] is `true` space characters are encoded as `+`, otherwise as `%20`.
44+
*
45+
* @param out Destination appendable that receives the encoded form string.
46+
* @param spaceToPlus If `true`, encode space as `+`; if `false`, encode space as `%20`. Defaults to `true`.
4247
*/
43-
public fun List<Pair<String, String?>>.formUrlEncodeTo(out: Appendable) {
48+
public fun List<Pair<String, String?>>.formUrlEncodeTo(out: Appendable, spaceToPlus: Boolean = true) {
4449
joinTo(out, "&") {
45-
val key = it.first.encodeURLParameter(spaceToPlus = true)
50+
val key = it.first.encodeURLParameter(spaceToPlus = spaceToPlus)
4651
if (it.second == null) {
4752
key
4853
} else {
49-
val value = it.second.toString().encodeURLParameterValue()
54+
val value = it.second.toString().encodeURLParameter(spaceToPlus = spaceToPlus)
5055
"$key=$value"
5156
}
5257
}
5358
}
5459

5560
/**
56-
* Encode form parameters
57-
*
58-
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.http.formUrlEncode)
59-
*/
60-
public fun Parameters.formUrlEncode(): String = entries()
61+
* Encodes parameters into an application/x-www-form-urlencoded string.
62+
*
63+
* @param spaceToPlus If `true`, space characters are encoded as `+`. If `false`, spaces are encoded as `%20`.
64+
* @return The resulting form-encoded string.
65+
*/
66+
public fun Parameters.formUrlEncode(spaceToPlus: Boolean = true): String = entries()
6167
.flatMap { e -> e.value.map { e.key to it } }
62-
.formUrlEncode()
68+
.formUrlEncode(spaceToPlus)
6369

6470
/**
65-
* Encode form parameters to the specified [out] appendable
71+
* Write these parameters as application/x-www-form-urlencoded into the given appendable.
6672
*
67-
* [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.http.formUrlEncodeTo)
73+
* @param out Destination to which the encoded form string is written.
74+
* @param spaceToPlus If `true`, encode space as `+`; if `false`, encode space as `%20`. Defaults to `true`.
6875
*/
69-
public fun Parameters.formUrlEncodeTo(out: Appendable) {
70-
entries().formUrlEncodeTo(out)
76+
public fun Parameters.formUrlEncodeTo(out: Appendable, spaceToPlus: Boolean = true) {
77+
entries().formUrlEncodeTo(out, spaceToPlus)
7178
}
7279

73-
internal fun ParametersBuilder.formUrlEncodeTo(out: Appendable) {
74-
entries().formUrlEncodeTo(out)
80+
/**
81+
* Write this builder's parameters to the given Appendable using application/x-www-form-urlencoded encoding.
82+
*
83+
* @param out Destination to which the encoded parameter string is written.
84+
* @param spaceToPlus If `true`, space characters are encoded as `'+'`; if `false`, spaces are percent-encoded (`"%20"`).
85+
*/
86+
internal fun ParametersBuilder.formUrlEncodeTo(out: Appendable, spaceToPlus: Boolean = true) {
87+
entries().formUrlEncodeTo(out, spaceToPlus)
7588
}
7689

77-
internal fun Set<Map.Entry<String, List<String>>>.formUrlEncodeTo(out: Appendable) {
90+
/**
91+
* Writes the set of parameter entries to the provided Appendable as application/x-www-form-urlencoded data.
92+
*
93+
* Each map entry is expanded into one or more form fields: if the value list is empty a lone key is written;
94+
* otherwise one key=value pair is written for each value in the list. Pairs are encoded and joined with `&`.
95+
*
96+
* @param out Destination Appendable to write the encoded form string to.
97+
* @param spaceToPlus When `true`, spaces are encoded as `+`; when `false`, spaces are percent-encoded.
98+
*/
99+
internal fun Set<Map.Entry<String, List<String>>>.formUrlEncodeTo(out: Appendable, spaceToPlus: Boolean = true) {
78100
flatMap { (key, value) ->
79101
if (value.isEmpty()) listOf(key to null) else value.map { key to it }
80-
}.formUrlEncodeTo(out)
81-
}
102+
}.formUrlEncodeTo(out, spaceToPlus)
103+
}

0 commit comments

Comments
 (0)