Use - instead of HTML escape code

This commit is contained in:
Elara 2021-01-12 19:57:49 -08:00
parent 7c117bc735
commit b41dbba7ec

View File

@ -29,7 +29,6 @@ import (
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
) )
@ -38,30 +37,16 @@ import (
func EncodeToSafeString(data []byte) string { func EncodeToSafeString(data []byte) string {
// Encode data to base91 string // Encode data to base91 string
base91Data := base91.EncodeToString(data) base91Data := base91.EncodeToString(data)
// Create regex to match |, ;, and % // Replace all semicolons with "-" as semicolon is used as a separator
escapeRegex := regexp.MustCompile(`[|;%]`) escapedBase91Data := strings.ReplaceAll(base91Data, ";", "-")
// Map each character to its escape code
escapeMap := map[string]string{"|": "%7C", ";": "%3B", "%": "%25"}
// Replace all matched characters in accordance with the function
escapedBase91Data := escapeRegex.ReplaceAllStringFunc(base91Data, func(in string) string {
// Return character mapping
return escapeMap[in]
})
// Return escaped base91 string // Return escaped base91 string
return escapedBase91Data return escapedBase91Data
} }
// Decode base91 encoded, escaped string to byte slice // Decode base91 encoded, escaped string to byte slice
func DecodeSafeString(base91Str string) ([]byte, error) { func DecodeSafeString(base91Str string) ([]byte, error) {
// Create regex to match %7C, %3B, %25 // Replace "-" with semicolon to reverse escape
unescapeRegex := regexp.MustCompile(`%7C|%3B|%25`) base91Data := strings.ReplaceAll(base91Str, "-", ";")
// Map each escape code to its character
escapeMap := map[string]string{"%7C": "|", "%3B": ";", "%25": "%"}
// Replace all matched characters in accordance with the function
base91Data := unescapeRegex.ReplaceAllStringFunc(base91Str, func(in string) string {
// Return escape code mapping
return escapeMap[in]
})
// Decode unescaped base91 string // Decode unescaped base91 string
data := base91.DecodeString(base91Data) data := base91.DecodeString(base91Data)
// Return byte slice // Return byte slice