encoding

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

html_utils.go

utf_utils.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChecksumAdler32

func ChecksumAdler32(input []byte) uint32

ChecksumAdler32 computes the Adler-32 checksum of the input data. It returns the checksum as a uint32 value. Adler-32 is a checksum algorithm that is faster than CRC32 but less reliable. It is suitable for applications where speed is more important than reliability. This function is useful for data integrity checks and error detection. Note: The Adler-32 checksum is not suitable for cryptographic purposes.

func ChecksumCRC32

func ChecksumCRC32(input []byte) uint32

ChecksumCRC32 computes the CRC32 checksum of the input data. It returns the checksum as a uint32 value. This function is useful for data integrity checks and error detection.

func CompressGzip

func CompressGzip(data []byte) ([]byte, error)

CompressGzip compresses data using gzip compression. It returns the compressed data as a byte slice.

func ContainsHTMLTags

func ContainsHTMLTags(s string) bool

ContainsHTMLTags checks whether a string likely contains HTML tags. This is a simple heuristic and may not be 100% accurate.

func ConvertISO8859ToUTF8

func ConvertISO8859ToUTF8(input []byte) (string, error)

ConvertISO8859ToUTF8 decodes an ISO-8859-1 (Latin-1) byte slice into a UTF-8 string.

func ConvertToValidUTF8String

func ConvertToValidUTF8String(data []byte) string

ConvertToValidUTF8String converts a potentially invalid UTF-8 byte slice into a valid UTF-8 string. It returns the original string if valid, or a fixed version with invalid bytes replaced.

func ConvertUTF8ToISO8859

func ConvertUTF8ToISO8859(input string) ([]byte, error)

ConvertUTF8ToISO8859 encodes a UTF-8 string into ISO-8859-1 (Latin-1) format bytes.

func ConvertUTF8ToUTF16

func ConvertUTF8ToUTF16(s string) []uint16

ConvertUTF8ToUTF16 converts a UTF-8 string to a slice of UTF-16 code units (LE format).

func ConvertUTF16BytesToUTF8

func ConvertUTF16BytesToUTF8(data []byte) (string, error)

ConvertUTF16BytesToUTF8 converts a byte slice encoded in UTF-16 (with BOM) into a UTF-8 string. Supports both little-endian and big-endian formats.

func ConvertUTF16ToUTF8

func ConvertUTF16ToUTF8(u16 []uint16) string

ConvertUTF16ToUTF8 converts a slice of UTF-16 code units to a UTF-8 encoded string.

func CountUnicodeCharacters

func CountUnicodeCharacters(s string) int

CountUnicodeCharacters returns the number of Unicode code points (runes) in the input string.

func DecodeGOB

func DecodeGOB(data []byte, out any) error

DecodeGOB deserializes binary gob data into the provided output object. The out parameter should be a pointer to the target struct.

func DecodeSpecialChars

func DecodeSpecialChars(s string) string

DecodeSpecialChars replaces basic HTML entities back to their original characters. EscapeHTMLForAttribute escapes special characters in a string for use in HTML attributes.

func DecompressGzip

func DecompressGzip(data []byte) ([]byte, error)

DecompressGzip decompresses gzip-compressed data. It returns the decompressed data as a byte slice.

func DecryptAES

func DecryptAES(ciphertext []byte, key []byte) ([]byte, error)

DecryptAES decrypts ciphertext generated by EncryptAES. The key must be either 16, 24, or 32 bytes long.

func DetectUTF16BOM

func DetectUTF16BOM(data []byte) string

DetectUTF16BOM checks for a UTF-16 byte order mark (BOM) at the start of the byte slice. Returns "LE" for little-endian, "BE" for big-endian, or an empty string if no BOM is found.

func EncodeGOB

func EncodeGOB(input any) ([]byte, error)

EncodeGOB serializes an object to binary format using encoding/gob.

func EncodeSpecialChars

func EncodeSpecialChars(s string) string

EncodeSpecialChars replaces certain ASCII characters with their corresponding HTML entities. This is useful for escaping characters that have special meaning in HTML attributes.

func EncryptAES

func EncryptAES(plain []byte, key []byte) ([]byte, error)

EncryptAES encrypts the given plaintext using AES-GCM authenticated encryption. The key must be either 16, 24, or 32 bytes long.

func FixUTF8

func FixUTF8(data []byte) string

FixUTF8 replaces invalid UTF-8 byte sequences in the input with the Unicode replacement character (\uFFFD).

func HTMLEscape

func HTMLEscape(s string) string

HTMLEscape escapes special characters in a string so it can be safely used in HTML. For example, '<' becomes '&lt;', '>' becomes '&gt;', etc.

func HTMLUnescape

func HTMLUnescape(s string) string

HTMLUnescape unescapes a string that contains HTML character entities back to its original form. For example, '&lt;' becomes '<', '&gt;' becomes '>', etc.

func HashFNV32

func HashFNV32(input []byte) string

HashFNV32 computes the FNV-1a hash of the input data. It returns the hash as a hexadecimal string.

func HashFNV64

func HashFNV64(input []byte) string

HashFNV64 computes the FNV-1a hash of the input data. It returns the hash as a hexadecimal string.

func HashSHA256

func HashSHA256(input []byte) string

HashSHA256 computes the SHA-256 hash of the input data. It returns the hash as a hexadecimal string.

func HashSHA512

func HashSHA512(input []byte) string

HashSHA512 computes the SHA-512 hash of the input data. It returns the hash as a hexadecimal string.

func IsHTMLEscaped

func IsHTMLEscaped(s string) bool

IsHTMLEscaped checks if the string contains any HTML-escaped sequences. This is a simple heuristic and may not be 100% accurate.

func IsValidUTF8

func IsValidUTF8(data []byte) bool

IsValidUTF8 returns true if the input byte slice is valid UTF-8.

func MurmurHash64

func MurmurHash64(data []byte, seed uint64) uint64

This implementation is adapted from publicly available sources of MurmurHash3. MurmurHash3 was created by Austin Appleby and is available under the MIT License. See: https://github.com/aappleby/smhasher

MurmurHash is a non-cryptographic hash function designed for general hash-based lookups.

func NormalizeNFC

func NormalizeNFC(s string) string

NormalizeNFC returns the NFC (Normalization Form C) version of the input string. Useful for canonical representation and string comparisons.

func NormalizeNFD

func NormalizeNFD(s string) string

NormalizeNFD returns the NFD (Normalization Form D) version of the input string. This form decomposes characters into base characters and combining marks.

func StripHTMLTags

func StripHTMLTags(s string) string

StripHTMLTags removes all HTML tags from the input string. It does not parse or sanitize malformed HTML, but removes everything between '<' and '>'. This is a simple heuristic and may not be 100% accurate.

func StripInvalidUTF8

func StripInvalidUTF8(data []byte) []byte

StripInvalidUTF8 removes any invalid UTF-8 sequences from the input byte slice. Valid sequences are preserved; invalid ones are skipped entirely.

func URLDecode

func URLDecode(input string) (string, error)

URLDecode decodes a percent-encoded string back to its original form. It reverses the effect of URLEncode. If the input is not valid percent-encoded data, it returns an error.

func URLEncode

func URLEncode(input string) string

URLEncode encodes a string for use in a URL query parameter. It replaces special characters with their percent-encoded equivalents.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL