lzstring

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 6 Imported by: 2

README

go-lz-string

Go implementation of lz-string

This is an implementation of lz-string written in Go. You can also embed go-lz-string as a library to your Go products.

lz-string algorithm version

Currently it implements algorithm that is compatible with lz-string@^1.4.4

Usage as a CLI

Installation
From GitHub Releases

See GitHub Releases.
Available binaries are:

  • macOS
  • Linux
  • Windows
go install

Go v1.18 or later is required.

go install github.com/daku10/go-lz-string/cmd/go-lz-string@latest
Example
$ go-lz-string compress <filename> -o <output-filename>
# use standard input/output
$ echo -n '🍎🍇🍌' | go-lz-string compress -m base64
jwbjl96cX3kGX2g=
$ echo -n 'jwbjl96cX3kGX2g=' | go-lz-string decompress -m base64
🍎🍇🍌

Compression and decompression methods are not only base64 but also invalid utf-16, utf-16, encodedURIComponent and byte array are implemented as well as original javascript program. To use other methods, see go-lz-string help

Usage as a library

Installation
go get -u github.com/daku10/go-lz-string
Example
package main

import (
	"fmt"

	lzstring "github.com/daku10/go-lz-string"
)

func main() {
	var input string = "Hello, world"
	var compressed []uint16 = lzstring.Compress(input)
	// [1157 12342 24822 832 1038 59649 14720 9792]
	fmt.Println(compressed)
	var decompressed string = lzstring.Decompress(compressed)
	// Hello, world!
	fmt.Println(decompressed)
}

Motivation

  • Need CLI tool to use easily
  • Some the go implementations of lz-string already exists, but there are lack of some functionality.
    • pieroxy/lz-string-go support decompression encodedURIComponent only.
    • lazarus/lz-string-go support compression/decompresson, but base64 method only, and specific input like emoji can not be compressed correctly.

Author

daku10

License

This software is released under the MIT License, see LICENSE.

Third-party Libraries

This repository contains third-party libraries in the "third-party" directory. Each library is stored as a Git submodule and is subject to the license terms of its respective library.

Please note that these third-party libraries are not developed, maintained, or supported by this repository's maintainers, and any issues or questions related to these libraries should be directed to their respective maintainers.

For more information on the licensing terms of each library, please see the LICENSE file located in each library's directory.

Documentation

Overview

Package lzstring implements the LZ-String algorithm for string compression and decompression. The library features two main sets of functions, Compress and Decompress, which are used to compress and decompress strings, respectively.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInputInvalidString = errors.New("input is invalid string")
	ErrInputNotDecodable  = errors.New("input is not decodable")
	ErrInputNil           = errors.New("input should not be nil")
	ErrInputBlank         = errors.New("input should not be blank")
)

Functions

func Compress

func Compress(uncompressed string) ([]uint16, error)

Compress takes an uncompressed string and compresses it into a slice of uint16. It returns an error if the input string is not a valid UTF-8 string. Note: The resulting uint16 slice may contain invalid UTF-16 characters, which is consistent with the original algorithm's behavior.

func CompressToBase64

func CompressToBase64(uncompressed string) (string, error)

CompressToBase64 takes an uncompressed string and compresses it into a Base64 string. It returns an error if the input string is not a valid UTF-8 string.

func CompressToEncodedURIComponent

func CompressToEncodedURIComponent(uncompressed string) (string, error)

CompressToEncodedURIComponent takes an uncompressed string and compresses it into a URL-safe string, where special characters are replaced with safe alternatives. It returns an error if the input string is not a valid UTF-8 string.

func CompressToUTF16

func CompressToUTF16(uncompressed string) ([]uint16, error)

CompressToUTF16 takes an uncompressed string and compresses it into a slice of uint16, where each element represents a UTF-16 encoded character. It returns an error if the input string is not a valid UTF-8 string.

func CompressToUint8Array

func CompressToUint8Array(uncompressed string) ([]byte, error)

CompressToUint8Array takes an uncompressed string and compresses it into a slice of bytes. It returns an error if the input string is not a valid UTF-8 string.

func Decompress

func Decompress(compressed []uint16) (string, error)

Decompress takes a compressed slice of uint16 main contain invalid UTF-16 characters and decompresses it into a string. It returns an error if the input is not a valid compressed data.

func DecompressFromBase64

func DecompressFromBase64(compressed string) (string, error)

DecompressFromBase64 takes a compressed Base64 string and decompresses it into a string. It returns an error if the input is not a valid compressed data.

func DecompressFromEncodedURIComponent

func DecompressFromEncodedURIComponent(compressed string) (string, error)

DecompressFromEncodedURIComponent takes a compressed URL-encoded string and decompresses it into a string. It returns an error if the input is not a valid compressed data.

func DecompressFromUTF16

func DecompressFromUTF16(compressed []uint16) (string, error)

DecompressFromUTF16 takes a compressed slice of uint16 UTF-16 characters and decompresses it into a string. It returns an error if the input is not a valid compressed data.

func DecompressFromUint8Array

func DecompressFromUint8Array(compressed []byte) (string, error)

DecompressFromUint8Array takes a compressed slice of bytes and decompresses it into a string. It returns an error if the input is not a valid compressed data.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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