File: README.md

package info (click to toggle)
golang-github-shenwei356-util 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
bytesize
========

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

Usage
-------

	fmt.Printf("1024 bytes\t%v\n", bytesize.ByteSize(1024))
	fmt.Printf("13146111 bytes\t%v\n", bytesize.ByteSize(13146111))

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


Result:

    1024 bytes         1.00 KB
    13146111 bytes    12.54 MB

    1536 bytes

REGEXP for ByteSize Text
----------------------------

    (?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