string_encoding¶
shortfx.fxString.string_encoding
¶
String Encoding and Decoding Functions.
This module provides functions for encoding and decoding strings using common formats such as Base64, URL percent-encoding, HTML entities, and classical ciphers (Caesar, Vigenère).
Key Features: - Base64 encoding and decoding - URL percent-encoding and decoding - HTML entity escaping and unescaping - Caesar cipher (shift cipher) - Vigenère cipher (polyalphabetic substitution)
Functions¶
caesar_cipher(text: str, shift: int, decrypt: bool = False) -> str
¶
Applies the Caesar (shift) cipher to a string.
Shifts each alphabetical character by shift positions in the alphabet. Non-alphabetic characters are left unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to encrypt or decrypt. |
required |
shift
|
int
|
Number of positions to shift (1-25). |
required |
decrypt
|
bool
|
If True, reverse the shift to decrypt. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The encrypted or decrypted string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If text is not a string or shift is not an integer. |
Example
caesar_cipher("Hello World", 3) 'Khoor Zruog' caesar_cipher("Khoor Zruog", 3, decrypt=True) 'Hello World'
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
decode_base64(encoded_text: str, encoding: str = 'utf-8') -> str
¶
Decodes a Base64-encoded string back to plain text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded_text
|
str
|
The Base64-encoded string. |
required |
encoding
|
str
|
Character encoding to use after Base64 decoding. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
The decoded plain text string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If encoded_text is not a string. |
ValueError
|
If the input is not valid Base64. |
Example
decode_base64("SGVsbG8gV29ybGQ=") 'Hello World'
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
decode_html_entities(encoded_text: str) -> str
¶
Unescapes HTML entities back to their original characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded_text
|
str
|
The string with HTML entities. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The string with entities replaced by actual characters. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If encoded_text is not a string. |
Example
decode_html_entities('<b>bold</b>') 'bold'
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
decode_url(encoded_text: str) -> str
¶
Decodes a URL percent-encoded string back to plain text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded_text
|
str
|
The percent-encoded string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The decoded plain text string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If encoded_text is not a string. |
Example
decode_url("hello%20world%26foo%3Dbar") 'hello world&foo=bar'
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
encode_base64(text: str, encoding: str = 'utf-8') -> str
¶
Encodes a string to Base64 representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to encode. |
required |
encoding
|
str
|
Character encoding to use before Base64 conversion. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
The Base64-encoded string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If text is not a string. |
Example
encode_base64("Hello World") 'SGVsbG8gV29ybGQ='
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
encode_html_entities(text: str) -> str
¶
Escapes HTML special characters into their entity equivalents.
Converts characters like <, >, &, " and ' into
safe HTML entities (e.g. <, >, &).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string containing HTML characters. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The string with HTML characters escaped. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If text is not a string. |
Example
encode_html_entities('') '<script>alert("xss")</script>'
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
encode_url(text: str, safe: str = '') -> str
¶
Encodes a string using URL percent-encoding (RFC 3986).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to encode. |
required |
safe
|
str
|
Characters that should not be encoded. |
''
|
Returns:
| Type | Description |
|---|---|
str
|
The percent-encoded string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If text is not a string. |
Example
encode_url("hello world&foo=bar") 'hello%20world%26foo%3Dbar'
Complexity: O(n)
Source code in shortfx/fxString/string_encoding.py
vigenere_cipher(text: str, key: str, decrypt: bool = False) -> str
¶
Applies the Vigenère polyalphabetic cipher to a string.
Each alphabetical character is shifted by the corresponding character in key. Non-alphabetic characters pass through unchanged and do not consume a key character.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to encrypt or decrypt. |
required |
key
|
str
|
Alphabetic key string (e.g. |
required |
decrypt
|
bool
|
If True, reverse the operation to decrypt. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The encrypted or decrypted string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If text or key is not a string. |
ValueError
|
If key is empty or contains non-alphabetic characters. |
Example
vigenere_cipher("Hello World", "KEY") 'Rijvs Uyvjn' vigenere_cipher("Rijvs Uyvjn", "KEY", decrypt=True) 'Hello World'
Complexity: O(n)