package g import ( "fmt" "html" "net/url" "strconv" "unicode/utf8" json "encoding/json/v2" ) type ( // A struct that wraps an String for encoding. encode struct{ str String } // A struct that wraps an String for decoding. decode struct{ str String } ) // Encode returns an encode struct wrapping the given String. func (s String) Encode() encode { return encode{s} } // Decode returns a decode struct wrapping the given String. func (s String) Decode() decode { return decode{s} } // Base64 encodes the wrapped String using standard Base64 (with padding). func (e encode) Base64() String { return e.str.BytesUnsafe().Encode().Base64().StringUnsafe() } // Base64Raw encodes the wrapped String using standard Base64 without padding. func (e encode) Base64Raw() String { return e.str.BytesUnsafe().Encode().Base64Raw().StringUnsafe() } // Base64URL encodes the wrapped String using URL-safe Base64 (with padding). func (e encode) Base64URL() String { return e.str.BytesUnsafe().Encode().Base64URL().StringUnsafe() } // Base64RawURL encodes the wrapped String using URL-safe Base64 without padding. func (e encode) Base64RawURL() String { return e.str.BytesUnsafe().Encode().Base64RawURL().StringUnsafe() } // Base64 decodes the wrapped String using standard Base64 (with padding). func (d decode) Base64() Result[String] { return d.str.BytesUnsafe().Decode().Base64().Map(Bytes.String) } // Base64Raw decodes the wrapped String using standard Base64 without padding. func (d decode) Base64Raw() Result[String] { return d.str.BytesUnsafe().Decode().Base64Raw().Map(Bytes.String) } // Base64URL decodes the wrapped String using URL-safe Base64 (with padding). func (d decode) Base64URL() Result[String] { return d.str.BytesUnsafe().Decode().Base64URL().Map(Bytes.String) } // Base64RawURL decodes the wrapped String using URL-safe Base64 without padding. func (d decode) Base64RawURL() Result[String] { return d.str.BytesUnsafe().Decode().Base64RawURL().Map(Bytes.String) } // Hex hex-encodes the wrapped String and returns the encoded result as an String. func (e encode) Hex() String { return e.str.BytesUnsafe().Encode().Hex().StringUnsafe() } // Hex hex-decodes the wrapped String and returns the decoded result as Result[String]. func (d decode) Hex() Result[String] { return d.str.BytesUnsafe().Decode().Hex().Map(Bytes.String) } // XOR encodes the wrapped String using a repeating-key XOR cipher with the given key. // // WARNING: XOR is NOT a security primitive. A repeating-key XOR cipher provides no // confidentiality against any serious analysis and offers no integrity or // authentication. Use it only for lightweight obfuscation, never to protect // sensitive data. func (e encode) XOR(key String) String { return String(e.str.BytesUnsafe().Encode().XOR(key.BytesUnsafe())) } // XOR decodes the wrapped String using a repeating-key XOR cipher with the given key. // // WARNING: XOR is NOT a security primitive. See encode.XOR for details. func (d decode) XOR(key String) String { return d.str.Encode().XOR(key) } // Binary converts the wrapped String to its binary representation. func (e encode) Binary() String { return e.str.BytesUnsafe().Encode().Binary().StringUnsafe() } // Binary converts the wrapped binary String back to its original String. func (d decode) Binary() Result[String] { return d.str.BytesUnsafe().Decode().Binary().Map(Bytes.String) } // JSON encodes the provided string as a JSON string using encoding/json/v2 and // returns the result as Result[String]. // // Breaking change (v2 semantics): a String containing invalid UTF-8 now yields // Err. Previously (encoding/json v1) invalid sequences were silently replaced // with the Unicode replacement character (U+FFFD), making the encoding lossy. // Unlike encoding/json v1, the output does not HTML-escape '<', '>', '&' or the // line separators U+2028/U+2029 — they are emitted raw. Escape the output // yourself before embedding it in HTML or