mirror of
https://github.com/gbevin/urlencoder.git
synced 2025-04-29 08:58:11 -07:00
Compare commits
2 commits
262e9d6d29
...
10e793fc39
Author | SHA1 | Date | |
---|---|---|---|
10e793fc39 | |||
3e397a65d2 |
2 changed files with 22 additions and 4 deletions
10
README.md
10
README.md
|
@ -10,12 +10,16 @@ A simple library to encode/decode URL parameters.
|
||||||
This library was extracted from the [RIFE2 Web Application Framework](https://rife2.com).
|
This library was extracted from the [RIFE2 Web Application Framework](https://rife2.com).
|
||||||
A Kotlin version can also be found at [https://github.com/ethauvin/urlencoder](https://github.com/ethauvin/urlencoder).
|
A Kotlin version can also be found at [https://github.com/ethauvin/urlencoder](https://github.com/ethauvin/urlencoder).
|
||||||
|
|
||||||
For decades, we've been using [java.net.URLEncoder](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URLEncoder.html)
|
For decades, we've been using `[java.net.URLEncoder](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URLEncoder.html)
|
||||||
because of its improper naming. It is actually intended to encode HTML form
|
because of its improper naming. It is actually intended to encode HTML form
|
||||||
parameters, not URLs.
|
parameters, not URLs, causing the wrong escape sequences to be used.
|
||||||
|
|
||||||
|
Additionally, java.net.URLEncoder allocates memory even when no encoding is
|
||||||
|
necessary, significantly impacting performance. This library has a negligible
|
||||||
|
performance impact when the string that is passed in doesn't need to be encoded.
|
||||||
|
|
||||||
Android's [Uri.encode](https://developer.android.com/reference/android/net/Uri#encode(java.lang.String,%20java.lang.String))
|
Android's [Uri.encode](https://developer.android.com/reference/android/net/Uri#encode(java.lang.String,%20java.lang.String))
|
||||||
also addresses this issue.
|
also addresses these issues.
|
||||||
|
|
||||||
## Examples (TL;DR)
|
## Examples (TL;DR)
|
||||||
|
|
||||||
|
|
|
@ -126,6 +126,20 @@ public final class UrlEncoder {
|
||||||
return out.toString();
|
return out.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a provided <code>String</code> object into a new string,
|
||||||
|
* containing only valid URL characters in the UTF-8 encoding.
|
||||||
|
*
|
||||||
|
* @param source The string that has to be transformed into a valid URL
|
||||||
|
* string.
|
||||||
|
* @return The encoded <code>String</code> object.
|
||||||
|
* @see #decode(String)
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public static String encode(String source) {
|
||||||
|
return encode(source, (String)null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms a provided <code>String</code> object into a new string,
|
* Transforms a provided <code>String</code> object into a new string,
|
||||||
* containing only valid URL characters in the UTF-8 encoding.
|
* containing only valid URL characters in the UTF-8 encoding.
|
||||||
|
@ -162,7 +176,7 @@ public final class UrlEncoder {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
while(i < source.length()) {
|
while(i < source.length()) {
|
||||||
ch = source.charAt(i);
|
ch = source.charAt(i);
|
||||||
if (isUnreservedUriChar(ch) || allow.indexOf(ch) != -1) {
|
if (isUnreservedUriChar(ch) || (allow != null && allow.indexOf(ch) != -1)) {
|
||||||
if (out != null) {
|
if (out != null) {
|
||||||
out.append(ch);
|
out.append(ch);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue