humansize

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Humansize

This library parses a human-readable format for writing data measurements in byte counts, or turns a byte count into a data measurement format string.

Installation

go get github.com/MyBlackJay/humansize

Example

Usage

package main

import (
	"fmt"
	"github.com/MyBlackJay/humansize"
)

func formatAndPrintKB() {
	size := "100KB"

	if parsing, err := humansize.Compile(size); err == nil {
		fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64())
	}
}

func formatAndPrintMiB() {
	size := "1MiB"

	if parsing, err := humansize.Compile(size); err == nil {
		fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64())
	}
}

func MustCompileMiWithError() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()

	size := "1MR"

	parsing := humansize.MustCompile(size)
	fmt.Println(parsing.GetInput(), parsing.GetMeasure(), parsing.GetCompiledUInt64())
}

func validateMeasureAndPrint() {
	measure := "EiR"
	fmt.Println(humansize.ValidateMeasure(measure))
}

func TurnBytesIntoSizeAndPrint() {
	size := 2.596 * float64(1<<60)
	if res, err := humansize.BytesToSize(size, 10); err == nil {
		fmt.Println(res)
	}
}

func TurnBytesIntoSizeInMeasureAndPrint() {
	if res, err := humansize.Compile("2048MB"); err == nil {
		fmt.Println(res.GetCompiledInMeasure("gib"))
	}
}

func main() {
	formatAndPrintKB()                   // 100KB 1024 102400
	formatAndPrintMiB()                  // 1MiB 1048576 1048576
	MustCompileMiWithError()             // unsupported data size format
	validateMeasureAndPrint()            // false
	TurnBytesIntoSizeAndPrint()          // 2.5960000000EB
	TurnBytesIntoSizeInMeasureAndPrint() // 2 <nil>

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	UnsupportedFormat = errors.New("unsupported data size format")
	UnexpectedError   = errors.New("unable convert")
)

available errors

Functions

func BytesToSize

func BytesToSize(size float64, precision uint) (string, error)

BytesToSize parses a number and returns a string of data size format. For example: 100MB.

func ValidateMeasure

func ValidateMeasure(format string) bool

ValidateMeasure parses a data size measure and returns true or false.

Types

type ReadableSize

type ReadableSize struct {
	// contains filtered or unexported fields
}

ReadableSize is the representation of a compiled data size expression.

func Compile

func Compile(input string) (*ReadableSize, error)

Compile parses a data size expression and returns, if successful, a ReadableSize object. For example: 100MB.

func MustCompile

func MustCompile(input string) *ReadableSize

MustCompile parses a data size expression and returns, if successful, a ReadableSize object or returns panic, if an error is found. For example: 100MB.

func (*ReadableSize) Get

func (rs *ReadableSize) Get() big.Int

Get returns the compiled data size in big.Int.

func (*ReadableSize) GetCompiledInMeasure

func (rs *ReadableSize) GetCompiledInMeasure(measure string) (float64, error)

GetCompiledInMeasure returns the compiled data size in a specific dimension. See the constant for the allowed options. WARNING: Due to the nature of rounding of floating point numbers, values may have slight deviations.

func (*ReadableSize) GetCompiledUInt64

func (rs *ReadableSize) GetCompiledUInt64() uint64

GetCompiledUInt64 returns the compiled data size in uint64. Warning: Possible rounding overflow, use with relatively small numbers.

func (*ReadableSize) GetInput

func (rs *ReadableSize) GetInput() string

GetInput returns original data size expression.

func (*ReadableSize) GetMeasure

func (rs *ReadableSize) GetMeasure() int64

GetMeasure returns the compiled data units in uint64.

func (*ReadableSize) GetRaw

func (rs *ReadableSize) GetRaw() big.Float

GetRaw returns the compiled data size.

func (ReadableSize) MarshalJSON added in v1.2.0

func (rs ReadableSize) MarshalJSON() ([]byte, error)

MarshalJSON designed to deserialize *ReadableSize to original data size expression defined in user code via the json library

func (ReadableSize) MarshalYAML added in v1.2.0

func (rs ReadableSize) MarshalYAML() (interface{}, error)

MarshalYAML designed to deserialize *ReadableSize in original data size expression defined in user code via the gopkg.in/yaml.v3 library

func (*ReadableSize) UnmarshalJSON added in v1.2.0

func (rs *ReadableSize) UnmarshalJSON(source []byte) error

UnmarshalJSON designed to serialize a string in data size expression to *ReadableSize, defined in user code via the json library

func (*ReadableSize) UnmarshalYAML added in v1.2.0

func (rs *ReadableSize) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML designed to serialize a string in data size expression to *ReadableSize, defined in user code via the gopkg.in/yaml.v3 library

Jump to

Keyboard shortcuts

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