encoding

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package encoding converts text files between UTF-8 and the encodings ZPad edits. The editor itself works on UTF-8.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownEncoding reports that no supported encoding could decode a
	// file's bytes.
	ErrUnknownEncoding = errors.New("unknown encoding")
	// ErrInvalidEncoding reports that text could not be encoded in the
	// target encoding.
	ErrInvalidEncoding = errors.New("invalid encoding")
)

All lists the encodings the status bar offers.

Functions

func Decode

func Decode(data []byte, kind Kind) (string, error)

Decode returns data as UTF-8 text.

func DecodeOwned added in v0.7.1

func DecodeOwned(data []byte, kind Kind) (string, error)

DecodeOwned decodes data in kind, reusing data without copying when it is already UTF-8. Callers must own data and never mutate it afterward.

func Encode

func Encode(text string, kind Kind) ([]byte, error)

Encode returns text in kind.

func EncodeStream added in v0.7.1

func EncodeStream(dst io.Writer, kind Kind, writeUTF8 func(io.Writer) error) (int64, error)

EncodeStream encodes the UTF-8 text written by writeUTF8 into dst in kind, returning the number of encoded bytes written to dst. Any invalid UTF-8 or unencodable sequence returns ErrInvalidEncoding.

func IsUTF16 added in v0.7.1

func IsUTF16(head []byte) bool

IsUTF16 reports whether head starts with a UTF-16 BOM or matches the UTF-16 NUL-byte pattern. Only the first 4096 bytes are inspected, so callers can distinguish UTF-16 from binary files using a small header read.

func Label

func Label(k Kind) string

Label is the name shown in the UI.

Types

type Kind

type Kind string

Kind is a text encoding.

const (
	UTF8    Kind = "utf-8"
	UTF8BOM Kind = "utf-8-bom"
	UTF16LE Kind = "utf-16le"
	UTF16BE Kind = "utf-16be"
	GB18030 Kind = "gb18030"
)

func Detect

func Detect(data []byte) (Kind, error)

Detect reads the encoding of data from its BOM, its UTF-16 byte pattern or its UTF-8 validity, falling back to GB18030.

func DetectAndDecodeOwned added in v0.7.1

func DetectAndDecodeOwned(data []byte) (string, Kind, error)

DetectAndDecodeOwned detects the encoding of data and decodes it in one pass. Callers must own data and never mutate it afterward, allowing UTF-8 text to be handed to the rope builder without a second validation pass or a full-file string copy, and GB18030 to be decoded once instead of twice.

func Parse

func Parse(s string) Kind

Parse turns a saved setting into a Kind, defaulting to UTF-8.

Jump to

Keyboard shortcuts

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