unishox2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

unishox2

Go Reference

Unishox2 is a Go library for the Unishox2 Unicode compression algorithm. This package provides CGo bindings to the official C implementation from https://github.com/siara-cc/Unishox2.

Unishox2 is a lossless compression algorithm optimized for short strings and Unicode text. It achieves high compression ratios while maintaining fast compression and decompression speeds.

Features

  • Full Unishox2 algorithm implementation via CGo
  • Optimized for short strings and Unicode text
  • 17 presets for different data types (default, JSON, XML, HTML, URLs, etc.)
  • Simple and comprehensive API
  • Round-trip compression/decompression guaranteed

Installation

go get codeberg.org/OperatorFoundation/unishox2

Note: This package requires CGo and a C compiler.

Usage

Simple API
package main

import (
    "fmt"
    "log"

    "codeberg.org/OperatorFoundation/unishox2"
)

func main() {
    // Compress a string
    compressed, err := unishox2.CompressSimple([]byte("Hello, World!"))
    if err != nil {
        log.Fatal(err)
    }

    // Decompress
    decompressed, err := unishox2.DecompressSimple(compressed)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Original: %s\n", string(decompressed))
    fmt.Printf("Compressed size: %d bytes\n", len(compressed))
}
Using Presets
// Compress JSON data with JSON-optimized preset
jsonData := []byte(`{"name":"John","age":30,"city":"New York"}`)
compressed, err := unishox2.Compress(jsonData, unishox2.PresetJSON)
if err != nil {
    log.Fatal(err)
}

// Decompress with same preset
decompressed, err := unishox2.Decompress(compressed, unishox2.PresetJSON)
if err != nil {
    log.Fatal(err)
}
String Helpers
// Compress string directly
compressed, err := unishox2.CompressSimpleString("Hello, World!")
if err != nil {
    log.Fatal(err)
}

// Decompress to string
decompressed, err := unishox2.DecompressSimpleString(compressed)
if err != nil {
    log.Fatal(err)
}

Available Presets

Preset Description
PresetDefault General purpose (default)
PresetAlphaOnly Alphabetic text only
PresetAlphaNumOnly Alphanumeric text only
PresetAlphaNumSymOnly Alphanumeric and symbols
PresetFavorAlpha Favors alphabetic content
PresetFavorDict Favors dictionary coding
PresetFavorSym Favors symbols
PresetFavorUmlaut Favors umlaut letters
PresetNoDict No dictionary coding
PresetNoUni No Unicode support
PresetNoUniFavorText No Unicode, favors text
PresetURL Optimized for URLs
PresetJSON Optimized for JSON
PresetJSONNoUni JSON without Unicode
PresetXML Optimized for XML
PresetHTML Optimized for HTML

API Reference

Simple Functions
  • CompressSimple(data []byte) ([]byte, error) - Compress with default preset
  • CompressSimpleString(s string) ([]byte, error) - Compress string with default preset
  • DecompressSimple(data []byte) ([]byte, error) - Decompress with default preset
  • DecompressSimpleString(data []byte) (string, error) - Decompress to string
Preset Functions
  • Compress(data []byte, preset Preset) ([]byte, error) - Compress with specific preset
  • CompressString(s string, preset Preset) ([]byte, error) - Compress string with preset
  • Decompress(data []byte, preset Preset) ([]byte, error) - Decompress with preset
  • DecompressString(data []byte, preset Preset) (string, error) - Decompress to string

Requirements

  • Go 1.21 or later
  • CGo enabled
  • C compiler (gcc, clang, etc.)

License

Apache License 2.0 - See LICENSE file for details.

The original Unishox2 C library is Copyright (C) 2020 Siara Logics (cc) and licensed under Apache License 2.0.

Acknowledgments

This package wraps the Unishox2 C library by Arundale Ramanathan: https://github.com/siara-cc/Unishox2

Documentation

Overview

Package unishox2 provides Go bindings for the Unishox2 compression algorithm.

Unishox2 is a lossless compression algorithm optimized for short strings and Unicode text. It achieves high compression ratios while maintaining fast compression and decompression speeds.

Basic usage:

// Compress a string
compressed, err := unishox2.CompressSimple("Hello, World!")
if err != nil {
    log.Fatal(err)
}

// Decompress
decompressed, err := unishox2.DecompressSimple(compressed)
if err != nil {
    log.Fatal(err)
}

Presets:

The package provides several presets optimized for different types of data:

  • PresetDefault: General purpose (default)
  • PresetAlphaOnly: Alphabetic text only
  • PresetAlphaNumOnly: Alphanumeric text only
  • PresetURL: URLs
  • PresetJSON: JSON data
  • PresetXML: XML data
  • PresetHTML: HTML data

Example with preset:

compressed, err := unishox2.Compress("Hello", unishox2.PresetJSON)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCompressionFailed   = errors.New("unishox2: compression failed")
	ErrDecompressionFailed = errors.New("unishox2: decompression failed")
	ErrBufferTooSmall      = errors.New("unishox2: output buffer too small")
)

Error definitions.

Functions

func Compress

func Compress(data []byte, preset Preset) ([]byte, error)

Compress compresses data using the specified preset. Returns the compressed data or an error if compression fails.

func CompressSimple

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

CompressSimple compresses data using the default preset. Returns the compressed data or an error if compression fails.

func CompressSimpleString

func CompressSimpleString(s string) ([]byte, error)

CompressSimpleString compresses a string using the default preset. Returns the compressed data or an error if compression fails.

func CompressString

func CompressString(s string, preset Preset) ([]byte, error)

CompressString compresses a string using the specified preset. Returns the compressed data or an error if compression fails.

func Decompress

func Decompress(data []byte, preset Preset) ([]byte, error)

Decompress decompresses data using the specified preset. Returns the decompressed data or an error if decompression fails.

func DecompressSimple

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

DecompressSimple decompresses data using the default preset. Returns the decompressed data or an error if decompression fails.

func DecompressSimpleString

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

DecompressSimpleString decompresses data and returns a string. Returns the decompressed string or an error if decompression fails.

func DecompressString

func DecompressString(data []byte, preset Preset) (string, error)

DecompressString decompresses data and returns a string. Returns the decompressed string or an error if decompression fails.

Types

type Preset

type Preset int

Preset represents a compression preset configuration.

const (
	PresetDefault            Preset = 0  // Optimum - favors all including JSON, XML, URL and HTML
	PresetAlphaOnly          Preset = 1  // Alphabets [a-z], [A-Z] and space only
	PresetAlphaNumOnly       Preset = 2  // Alphanumeric [a-z], [A-Z], [0-9] and space only
	PresetAlphaNumSymOnly    Preset = 3  // Alphanumeric and symbols only
	PresetAlphaNumSymOnlyTxt Preset = 4  // Alphanumeric and symbols only (Favor English text)
	PresetFavorAlpha         Preset = 5  // Favor Alphabets
	PresetFavorDict          Preset = 6  // Favor Dictionary coding
	PresetFavorSym           Preset = 7  // Favor Symbols
	PresetFavorUmlaut        Preset = 8  // Favor Umlaut
	PresetNoDict             Preset = 9  // No dictionary
	PresetNoUni              Preset = 10 // No Unicode
	PresetNoUniFavorText     Preset = 11 // No Unicode, favour English text
	PresetURL                Preset = 12 // Favor URLs
	PresetJSON               Preset = 13 // Favor JSON
	PresetJSONNoUni          Preset = 14 // Favor JSON (No Unicode)
	PresetXML                Preset = 15 // Favor XML
	PresetHTML               Preset = 16 // Favor HTML
)

Preset constants for different data types.

Jump to

Keyboard shortcuts

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