codec

package
v0.0.0-...-dfb750a Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

README

Package cloudeng.io/algo/codec

import cloudeng.io/algo/codec

Package codec provides support for interpreting byte slices as slices of other basic types such as runes, int64's or strings.

Types

Type Decoder
type Decoder[T any] interface {
	Decode(input []byte) []T
}

Decoder represents the ability to decode a byte slice into a slice of some other data type.

Functions
func NewDecoder[T any](fn func([]byte) (T, int), opts ...Option) Decoder[T]

NewDecode returns an instance of Decoder appropriate for the supplied function.

Type Option
type Option func(*options)

Option represents an option accepted by NewDecoder.

Functions
func ResizePercent(percent int) Option

ResizePercent requests that the returned slice be reallocated if the ratio of unused to used capacity exceeds the specified percentage. That is, if cap(slice) - len(slice)) / len(slice) exceeds the percentage new underlying storage is allocated and contents copied. The default value for ResizePercent is 100.

func SizePercent(percent int) Option

SizePercent requests that the initially allocated slice be 'percent' as large as the original input slice's size in bytes. A percent of 25 will divide the original size by 4 for example.

Examples

ExampleDecoder

Documentation

Overview

Package codec provides support for interpreting byte slices as slices of other basic types such as runes, int64's or strings.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder[T any] interface {
	Decode(input []byte) []T
}

Decoder represents the ability to decode a byte slice into a slice of some other data type.

Example
package main

import (
	"fmt"
	"unicode/utf8"

	"cloudeng.io/algo/codec"
)

func main() {
	decoded := codec.NewDecoder(utf8.DecodeRune).Decode([]byte("日本語"))
	fmt.Println(len(decoded))
}
Output:

3

func NewDecoder

func NewDecoder[T any](fn func([]byte) (T, int), opts ...Option) Decoder[T]

NewDecode returns an instance of Decoder appropriate for the supplied function.

type Option

type Option func(*options)

Option represents an option accepted by NewDecoder.

func ResizePercent

func ResizePercent(percent int) Option

ResizePercent requests that the returned slice be reallocated if the ratio of unused to used capacity exceeds the specified percentage. That is, if cap(slice) - len(slice)) / len(slice) exceeds the percentage new underlying storage is allocated and contents copied. The default value for ResizePercent is 100.

func SizePercent

func SizePercent(percent int) Option

SizePercent requests that the initially allocated slice be 'percent' as large as the original input slice's size in bytes. A percent of 25 will divide the original size by 4 for example.

Jump to

Keyboard shortcuts

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