-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bytes and IBytes functions in the unit package (#14)
- Loading branch information
Showing
3 changed files
with
126 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v0.0.2 | ||
v0.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
package unit | ||
|
||
import "github.com/dustin/go-humanize" | ||
import ( | ||
"strings" | ||
|
||
"github.com/dustin/go-humanize" | ||
) | ||
|
||
// ParseBytes parse the string to bytes | ||
func ParseBytes(s string) (int, error) { | ||
b, err := humanize.ParseBytes(s) | ||
func ParseBytes(s string) (bytes uint64, iec bool, err error) { | ||
bytes, err = humanize.ParseBytes(s) | ||
if err != nil { | ||
return 0, err | ||
return bytes, iec, err | ||
} | ||
return int(b), nil | ||
iec = isIEC(s) | ||
return bytes, iec, nil | ||
} | ||
|
||
// Bytes produces a human readable representation of an SI size | ||
func Bytes(b uint64) string { | ||
return humanize.Bytes(b) | ||
} | ||
|
||
// IBytes produces a human readable representation of an IEC size | ||
func IBytes(b uint64) string { | ||
return humanize.IBytes(b) | ||
} | ||
|
||
func isIEC(s string) bool { | ||
return strings.ContainsRune(strings.ToLower(s), 'i') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters