mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-12-18 16:34:18 +01:00
Add entries for URL encoding and decoding
This commit is contained in:
52
README.md
52
README.md
@@ -48,6 +48,8 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s
|
|||||||
* [Strip first occurrence of pattern from string](#strip-first-occurrence-of-pattern-from-string)
|
* [Strip first occurrence of pattern from string](#strip-first-occurrence-of-pattern-from-string)
|
||||||
* [Strip pattern from start of string](#strip-pattern-from-start-of-string)
|
* [Strip pattern from start of string](#strip-pattern-from-start-of-string)
|
||||||
* [Strip pattern from end of string](#strip-pattern-from-end-of-string)
|
* [Strip pattern from end of string](#strip-pattern-from-end-of-string)
|
||||||
|
* [Percent-encode a string](#percent-encode-a-string)
|
||||||
|
* [Decode a percent-encoded string](#decode-a-percent-encoded-string)
|
||||||
* [Check if string contains a sub-string](#check-if-string-contains-a-sub-string)
|
* [Check if string contains a sub-string](#check-if-string-contains-a-sub-string)
|
||||||
* [Check if string starts with sub-string](#check-if-string-starts-with-sub-string)
|
* [Check if string starts with sub-string](#check-if-string-starts-with-sub-string)
|
||||||
* [Check if string ends with sub-string](#check-if-string-ends-with-sub-string)
|
* [Check if string ends with sub-string](#check-if-string-ends-with-sub-string)
|
||||||
@@ -483,6 +485,56 @@ $ rstrip "The Quick Brown Fox" " Fox"
|
|||||||
The Quick Brown
|
The Quick Brown
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Percent-encode a string
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
urlencode() {
|
||||||
|
# Usage: urlencode "string"
|
||||||
|
local LC_ALL=C
|
||||||
|
for (( i = 0; i < ${#1}; i++ )); do
|
||||||
|
: "${1:i:1}"
|
||||||
|
case "$_" in
|
||||||
|
[a-zA-Z0-9.~_-])
|
||||||
|
printf '%s' "$_"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
printf '%%%02X' "'$_"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
printf '\n'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ urlencode "https://github.com/dylanaraps/pure-bash-bible"
|
||||||
|
https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible
|
||||||
|
```
|
||||||
|
|
||||||
|
## Decode a percent-encoded string
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
urldecode() {
|
||||||
|
# Usage: urldecode "string"
|
||||||
|
: "${1//+/ }"
|
||||||
|
printf '%b\n' "${_//%/\\x}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
|
||||||
|
https://github.com/dylanaraps/pure-bash-bible
|
||||||
|
```
|
||||||
|
|
||||||
## Check if string contains a sub-string
|
## Check if string contains a sub-string
|
||||||
|
|
||||||
**Using a test:**
|
**Using a test:**
|
||||||
|
|||||||
@@ -308,6 +308,55 @@ $ rstrip "The Quick Brown Fox" " Fox"
|
|||||||
The Quick Brown
|
The Quick Brown
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Percent-encode a string
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
urlencode() {
|
||||||
|
# Usage: urlencode "string"
|
||||||
|
for (( i = 0; i < ${#1}; i++ )); do
|
||||||
|
: "${1:i:1}"
|
||||||
|
case "$_" in
|
||||||
|
[a-zA-Z0-9.~_-])
|
||||||
|
printf '%s' "$_"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
printf '%%%02X' "'$_"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
printf '\n'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ urlencode "https://github.com/dylanaraps/pure-bash-bible"
|
||||||
|
https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible
|
||||||
|
```
|
||||||
|
|
||||||
|
## Decode a percent-encoded string
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
urldecode() {
|
||||||
|
# Usage: urldecode "string"
|
||||||
|
: "${1//+/ }"
|
||||||
|
printf '%b\n' "${_//%/\\x}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
|
||||||
|
https://github.com/dylanaraps/pure-bash-bible
|
||||||
|
```
|
||||||
|
|
||||||
## Check if string contains a sub-string
|
## Check if string contains a sub-string
|
||||||
|
|
||||||
**Using a test:**
|
**Using a test:**
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ $ hello_world="value"
|
|||||||
$ var="world"
|
$ var="world"
|
||||||
|
|
||||||
# Declare a nameref.
|
# Declare a nameref.
|
||||||
$ declare -n "ref=hello_$var"
|
$ declare -n ref=hello_$var
|
||||||
|
|
||||||
$ printf '%s\n' "$ref"
|
$ printf '%s\n' "$ref"
|
||||||
value
|
value
|
||||||
@@ -31,7 +31,7 @@ value
|
|||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ var="world"
|
$ var="world"
|
||||||
$ declare hello_$var=value
|
$ declare "hello_$var=value"
|
||||||
$ printf '%s\n' "$hello_world"
|
$ printf '%s\n' "$hello_world"
|
||||||
value
|
value
|
||||||
```
|
```
|
||||||
|
|||||||
10
test.sh
10
test.sh
@@ -53,6 +53,16 @@ test_rstrip() {
|
|||||||
assert_equals "$result" "Hello"
|
assert_equals "$result" "Hello"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_urlencode() {
|
||||||
|
result="$(urlencode "https://github.com/dylanaraps/pure-bash-bible")"
|
||||||
|
assert_equals "$result" "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
|
||||||
|
}
|
||||||
|
|
||||||
|
test_urldecode() {
|
||||||
|
result="$(urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible")"
|
||||||
|
assert_equals "$result" "https://github.com/dylanaraps/pure-bash-bible"
|
||||||
|
}
|
||||||
|
|
||||||
test_reverse_array() {
|
test_reverse_array() {
|
||||||
IFS=$'\n' read -d "" -ra result < <(reverse_array 1 2 3 4 5)
|
IFS=$'\n' read -d "" -ra result < <(reverse_array 1 2 3 4 5)
|
||||||
assert_equals "${result[*]}" "5 4 3 2 1"
|
assert_equals "${result[*]}" "5 4 3 2 1"
|
||||||
|
|||||||
Reference in New Issue
Block a user