@encoding
The @encoding module provides functions for encoding and decoding data in various formats including Base64, hexadecimal, and URL encoding. These functions work with strings. For byte-level encoding, see @bytes.
Import
import @encoding
Base64
base64_encode()
(data string) -> string
Encodes a string to Base64.
import @encoding
do main() {
mut encoded string = encoding.base64_encode("Hello, World!")
println(encoded) // "SGVsbG8sIFdvcmxkIQ=="
}
Parameters: data - The string to encode.
Returns: string - The Base64-encoded string.
Errors: E7001 for wrong argument count, E7003 if argument is not a string.
base64_decode()
(data string) -> (string, Error)
Decodes a Base64 string.
import @encoding
do main() {
mut decoded string, err Error = encoding.base64_decode("SGVsbG8sIFdvcmxkIQ==")
if err == nil {
println(decoded) // "Hello, World!"
} otherwise {
println("Error:", err.message)
}
}
Parameters: data - The Base64-encoded string to decode.
Returns:
string- The decoded string (empty on error).Error-nilon success, or an Error struct withmessageandcodefields.
Errors: E7001 for wrong argument count, E7003 if argument is not a string, E16001 for invalid Base64 input.
Hexadecimal
hex_encode()
(data string) -> string
Encodes a string to lowercase hexadecimal.
import @encoding
do main() {
mut encoded string = encoding.hex_encode("Hello")
println(encoded) // "48656c6c6f"
}
Parameters: data - The string to encode.
Returns: string - The hex-encoded string (lowercase).
Errors: E7001 for wrong argument count, E7003 if argument is not a string.
hex_decode()
(data string) -> (string, Error)
Decodes a hexadecimal string.
import @encoding
do main() {
mut decoded string, err Error = encoding.hex_decode("48656c6c6f")
if err == nil {
println(decoded) // "Hello"
} otherwise {
println("Error:", err.message)
}
}
Parameters: data - The hex-encoded string to decode (case-insensitive).
Returns:
string- The decoded string (empty on error).Error-nilon success, or an Error struct withmessageandcodefields.
Errors: E7001 for wrong argument count, E7003 if argument is not a string, E16002 for invalid hex input.
URL Encoding
url_encode()
(data string) -> string
URL percent-encodes a string for use in query parameters.
import @encoding
do main() {
mut encoded string = encoding.url_encode("hello world?foo=bar")
println(encoded) // "hello+world%3Ffoo%3Dbar"
}
Parameters: data - The string to encode.
Returns: string - The URL-encoded string.
Errors: E7001 for wrong argument count, E7003 if argument is not a string.
url_decode()
(data string) -> (string, Error)
Decodes a URL percent-encoded string.
import @encoding
do main() {
mut decoded string, err Error = encoding.url_decode("hello+world%3Ffoo%3Dbar")
if err == nil {
println(decoded) // "hello world?foo=bar"
} otherwise {
println("Error:", err.message)
}
}
Parameters: data - The URL-encoded string to decode.
Returns:
string- The decoded string (empty on error).Error-nilon success, or an Error struct withmessageandcodefields.
Errors: E7001 for wrong argument count, E7003 if argument is not a string, E16003 for invalid URL encoding.