Skip to content

Commit

Permalink
Merge pull request #511 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 3.7.0
  • Loading branch information
andyone authored Oct 14, 2024
2 parents d89e634 + bea1f12 commit 37713aa
Show file tree
Hide file tree
Showing 16 changed files with 588 additions and 208 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## Changelog

### [13.7.0](https://kaos.sh/ek/13.7.0)

- `[fmtutil]` Added support of `kk`, `kkk`, `kib`, `mib`, `gib`, and `tib` in `ParseSize`
- `[knf]` Added getter `GetSZ` for reading value as the size
- `[knf/united]` Added getter `GetSZ` for reading value as the size
- `[knf/validators]` Added validator `TypeSize`
- `[knf/validators]` Added validator `InRange`
- `[fmtutil]` Code refactoring
- `[knf/united]` Improved usage examples

### [13.6.1](https://kaos.sh/ek/13.6.1)

- `[req]` Guess the value of the `Content-Type` header based on the request body type
Expand Down
73 changes: 34 additions & 39 deletions fmtutil/fmtutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func PrettyBool(b bool, vals ...string) string {

// ParseSize parses size and return it in bytes (e.g 1.34 Mb -> 1478182)
func ParseSize(size string) uint64 {
ns := strings.ToLower(strings.Replace(size, " ", "", -1))
mlt, sfx := extractSizeInfo(ns)
v := strings.ToLower(strings.ReplaceAll(size, " ", ""))
mod, suf := extractSizeInfo(v)

if sfx == "" {
if suf == "" {
num, err := strconv.ParseUint(size, 10, 64)

if err != nil {
Expand All @@ -171,14 +171,14 @@ func ParseSize(size string) uint64 {
return num
}

ns = strings.TrimRight(ns, sfx)
numFlt, err := strconv.ParseFloat(ns, 64)
v = strings.TrimRight(v, suf)
numFlt, err := strconv.ParseFloat(v, 64)

if err != nil {
return 0
}

return uint64(numFlt * float64(mlt))
return uint64(numFlt * mod)
}

// Float formats float numbers more nicely
Expand Down Expand Up @@ -340,41 +340,36 @@ func appendPrettySymbol(str, sep string) string {
return b.String()
}

func extractSizeInfo(s string) (uint64, string) {
var mlt uint64
var sfx string

switch {
case strings.HasSuffix(s, "tb"):
mlt = 1024 * 1024 * 1024 * 1024
sfx = "tb"
case strings.HasSuffix(s, "t"):
mlt = 1000 * 1000 * 1000 * 1000
sfx = "t"
case strings.HasSuffix(s, "gb"):
mlt = 1024 * 1024 * 1024
sfx = "gb"
case strings.HasSuffix(s, "g"):
mlt = 1000 * 1000 * 1000
sfx = "g"
case strings.HasSuffix(s, "mb"):
mlt = 1024 * 1024
sfx = "mb"
case strings.HasSuffix(s, "m"):
mlt = 1000 * 1000
sfx = "m"
case strings.HasSuffix(s, "kb"):
mlt = 1024
sfx = "kb"
case strings.HasSuffix(s, "k"):
mlt = 1000
sfx = "k"
case strings.HasSuffix(s, "b"):
mlt = 1
sfx = "b"
// extractSizeInfo extracts size info
func extractSizeInfo(s string) (float64, string) {
var mod float64

suf := strings.TrimLeft(s, "0123456789. ")

switch suf {
case "tb", "tib":
mod = 1024 * 1024 * 1024 * 1024
case "t":
mod = 1000 * 1000 * 1000 * 1000
case "gb", "gib":
mod = 1024 * 1024 * 1024
case "g", "kkk":
mod = 1000 * 1000 * 1000
case "mb", "mib":
mod = 1024 * 1024
case "m", "kk":
mod = 1000 * 1000
case "kb":
mod = 1024
case "k":
mod = 1000
case "b":
mod = 1
default:
suf = ""
}

return mlt, sfx
return mod, suf
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 2 additions & 0 deletions fmtutil/fmtutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func (s *FmtUtilSuite) TestParseSize(c *C) {
c.Assert(ParseSize("5g"), Equals, uint64(5*1000*1000*1000))
c.Assert(ParseSize("13kb"), Equals, uint64(13*1024))
c.Assert(ParseSize("13k"), Equals, uint64(13*1000))
c.Assert(ParseSize("13kk"), Equals, uint64(13*1000*1000))
c.Assert(ParseSize("13kkk"), Equals, uint64(13*1000*1000*1000))
c.Assert(ParseSize("512"), Equals, uint64(512))
c.Assert(ParseSize("kb"), Equals, uint64(0))
c.Assert(ParseSize("123!"), Equals, uint64(0))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/essentialkaos/ek/v13
go 1.22.8

require (
github.com/essentialkaos/check v1.4.0
github.com/essentialkaos/check v1.4.1
github.com/essentialkaos/depsy v1.3.1
github.com/essentialkaos/go-linenoise/v3 v3.6.1
golang.org/x/crypto v0.28.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk=
github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0=
github.com/essentialkaos/check v1.4.1 h1:SuxXzrbokPGTPWxGRnzy0hXvtb44mtVrdNxgPa1s4c8=
github.com/essentialkaos/check v1.4.1/go.mod h1:xQOYwFvnxfVZyt5Qvjoa1SxcRqu5VyP77pgALr3iu+M=
github.com/essentialkaos/depsy v1.3.1 h1:00k9QcMsdPM4IzDaEFHsTHBD/zoM0oxtB5+dMUwbQa8=
github.com/essentialkaos/depsy v1.3.1/go.mod h1:B5+7Jhv2a2RacOAxIKU2OeJp9QfZjwIpEEPI5X7auWM=
github.com/essentialkaos/go-linenoise/v3 v3.6.1 h1:VzjakaWNAPfattl/HSIJveWYpfrxAYHzUl8u6DdOjtY=
Expand Down
Loading

0 comments on commit 37713aa

Please sign in to comment.