common

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package common provides shared types and helpers for file range, lifecycle, and algorithms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatPercent added in v0.1.5

func FormatPercent(p float64) string

FormatPercent formats a percent value as a string without unnecessary trailing zeros (e.g. 95, 95.5)

func LessFileAndRange added in v0.1.5

func LessFileAndRange(a, b FileAndRangeSpec) bool

LessFileAndRange returns true if a comes before b by FilePath, then Start, then End.

func ParseInt64 added in v0.1.5

func ParseInt64(s string) (int64, error)

ParseInt64 parses a string as int64 and returns an error if invalid or negative.

func ParsePercent added in v0.1.5

func ParsePercent(s string) (float64, error)

ParsePercent parses a percent string (e.g., "50%") and returns its value as a float64.

Types

type Algorithm

type Algorithm int

Algorithm represents a hash algorithm.

const (
	CRC32 Algorithm = iota
	BSDCKSUM
	MD4
	MD5
	SHA1
	SHA256
	SHA512
	SHA3_256
	SHAKE128
	SHAKE256
	BLAKE2B
	BLAKE3
	HMACSHA1
	HMACSHA256
	HMACSHA512
	CHACHA20POLY1305
	XXHASH
	SIPHASH
	CITYHASH
	KANGAROOTWELVE
	STREEBOG256
	STREEBOG512
	SHA224
	SHA384
	SHA512_224
	SHA512_256
	SHA3_224
	SHA3_384
	SHA3_512
	RIPEMD160
	HMACMD5
	HMACRIPEMD160
	HMACBLAKE2B
	BLAKE2S
	ADLER32
	BCRYPT_SHA512
	ARGON2_SHA512
	SM3
	TTH
	KECCAK256
	PBKDF2_SHA512
	SCRYPT_SHA512
	SSDEEP
	WHIRLPOOL
)

Constants for hash algorithms.

type FileAndRangeSpec added in v0.1.5

type FileAndRangeSpec struct {
	FilePath string
	// Start and End are stored as basis points (0-10000) when IsPercent is true.
	Start     int64
	End       int64
	IsPercent bool
}

FileAndRangeSpec represents a file path with an optional byte or percent range. When IsPercent is true, Start and End are stored as basis points (0-10000), where 10000 = 100%.

func IncrementalRanges added in v0.1.5

func IncrementalRanges(filePath string, fileSize int64, percent float64) []FileAndRangeSpec

IncrementalRanges generates a slice of FileAndRangeSpec representing incremental percent-based ranges for a file.

func (*FileAndRangeSpec) GetRangeSize added in v0.1.5

func (rs *FileAndRangeSpec) GetRangeSize(fileSize int64) int64

GetRangeSize returns the size of the range for this FileAndRangeSpec. If End == -1, it uses fileSize as the end.

func (*FileAndRangeSpec) Parse added in v0.1.5

func (rs *FileAndRangeSpec) Parse(s string) error

Parse populates the FileAndRangeSpec fields from a string of the form "file#start-end" or "file#start%-end%".

func (*FileAndRangeSpec) String added in v0.1.5

func (rs *FileAndRangeSpec) String() string

String returns a string representation of the FileAndRangeSpec, including any range or percent info.

func (*FileAndRangeSpec) ToAbsoluteRange added in v0.1.5

func (rs *FileAndRangeSpec) ToAbsoluteRange(fileSize int64) FileAndRangeSpec

ToAbsoluteRange converts a percent-based FileAndRangeSpec to a byte-based one, given the file size. If already absolute, returns a copy.

func (*FileAndRangeSpec) ToBytes added in v0.1.5

func (rs *FileAndRangeSpec) ToBytes() (start, end int64, err error)

ToBytes converts the FileAndRangeSpec's range (including percent) to absolute byte offsets for a file of the given size.

func (*FileAndRangeSpec) ToPercentRange added in v0.1.5

func (rs *FileAndRangeSpec) ToPercentRange(fileSize int64) FileAndRangeSpec

ToPercentRange converts a byte-based FileAndRangeSpec to a percent-based one (basis points), given the file size. If already percent, returns a copy.

type FileLifecycle

type FileLifecycle struct {
	OnStart func(offset1, offset2 int64)
	OnChunk func(bytes int64)
	OnEnd   func()
}

FileLifecycle represents a lifecycle of a file being processed.

func MakeDefaultLifecycle added in v0.1.9

func MakeDefaultLifecycle(rs FileAndRangeSpec, size int64) FileLifecycle

MakeDefaultLifecycle returns a no-op lifecycle matching the progressFunc signature.

func MakeProgressBars added in v0.1.9

func MakeProgressBars(rs FileAndRangeSpec, size int64) FileLifecycle

MakeProgressBars returns a lifecycle with progress bar functionality.

type LifecycleReader

type LifecycleReader struct {
	Reader    io.Reader
	Lifecycle FileLifecycle
}

LifecycleReader is a reader that tracks the lifecycle of a file being processed.

func (*LifecycleReader) Read

func (lr *LifecycleReader) Read(parts []byte) (n int, err error)

Read implements io.Reader.

type ProgressFunc added in v0.1.9

type ProgressFunc func(rs FileAndRangeSpec, size int64) FileLifecycle

ProgressFunc creates a FileLifecycle for a file range and size.

Jump to

Keyboard shortcuts

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