bytesize

package
v1.0.1000 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package bytesize provides a way to show readable values of byte size by reediting the code from http://golang.org/doc/effective_go.html. It could also parsing byte size text to ByteSize object.

Index

Constants

This section is empty.

Variables

View Source
var BytesizeRegexp = regexp.MustCompile(`(?i)^\s*(\-?[\d\.]+)\s*([KMGTPEZY]?B|[BKMGTPEZY]|)\s*$`)

BytesizeRegexp is the regexp object for ByteSize Text. The REGEXP is:

(?i)^\s*([\-?[\d\.]+)\s*([KMGTPEZY]?B|[BKMGTPEZY]|)\s?$

Example:

data["1234.2 kb"] = 1263820.80    // lower case
data["-1234.2 kb"] = -1263820.80  // lower case
data[" 1234.2  kb "] = 1263820.80 // space
data["1234.2 k"] = 1263820.80     // simple unit
data["1234.2 "] = 1234.2          // no unit
data[" kb "] = -1                 // illegal value
data["- kb"] = -1                 // illegal value
data["1234.2 aB"] = -1            // illegal unit
data["1234.2 Packages"] = -1      // illegal unit
View Source
var ErrText = "illegal bytesize text"

ErrText is error information for Illegal byte size text

Functions

func ParseByteSize

func ParseByteSize(val string) (int, error)

ParseByteSize parses byte size from string.

Types

type ByteSize

type ByteSize float64

ByteSize stands for byte size. Division operation is needed, so it uses float64 instead of uint64

const (
	B ByteSize = 1 << (10 * iota)
	KB
	MB
	GB
	TB
	PB
	EB
	ZB
	YB
)

const for bytesize. B is also specified.

func Parse

func Parse(sizeText []byte) (ByteSize, error)

Parse parses ByteSize Text to ByteSize object

Example

size, err := bytesize.Parse([]byte("1.5 KB"))
if err != nil {
    fmt.Println(err)
}
fmt.Printf("%.0f bytes\n", size)

func (ByteSize) String

func (b ByteSize) String() string

Print readable values of byte size

Jump to

Keyboard shortcuts

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