diff options
author | 2024-02-27 20:40:43 +0100 | |
---|---|---|
committer | 2024-02-28 19:32:38 -0800 | |
commit | f274394f0e8406c85360b1722136d27a7b82e94a (patch) | |
tree | f1d91452ab29daac6d4972d1dda01a58990fb175 /internal/template/functions.go | |
parent | 9a4a942cc44223126c084952938aead01d95b68e (diff) | |
download | v2-f274394f0e8406c85360b1722136d27a7b82e94a.tar.gz v2-f274394f0e8406c85360b1722136d27a7b82e94a.tar.zst v2-f274394f0e8406c85360b1722136d27a7b82e94a.zip |
Simplify formatFileSize
No need to use a loop with divisions and multiplications when we have logarithms.
Diffstat (limited to 'internal/template/functions.go')
-rw-r--r-- | internal/template/functions.go | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/internal/template/functions.go b/internal/template/functions.go index 5b4018a1..f8aa2bbd 100644 --- a/internal/template/functions.go +++ b/internal/template/functions.go @@ -36,8 +36,8 @@ func (f *funcMap) Map() template.FuncMap { "hasKey": hasKey, "truncate": truncate, "isEmail": isEmail, - "baseURL": config.Opts.BaseURL, - "rootURL": config.Opts.RootURL, + "baseURL": config.Opts.BaseURL, + "rootURL": config.Opts.RootURL, "hasOAuth2Provider": func(provider string) bool { return config.Opts.OAuth2Provider() == provider }, @@ -71,7 +71,7 @@ func (f *funcMap) Map() template.FuncMap { "mustBeProxyfied": func(mediaType string) bool { return slices.Contains(config.Opts.ProxyMediaTypes(), mediaType) }, - "domain": urllib.Domain, + "domain": urllib.Domain, "hasPrefix": strings.HasPrefix, "contains": strings.Contains, "replace": func(str, old, new string) string { @@ -209,11 +209,7 @@ func formatFileSize(b int64) string { if b < unit { return fmt.Sprintf("%d B", b) } - div, exp := int64(unit), 0 - for n := b / unit; n >= unit; n /= unit { - div *= unit - exp++ - } - return fmt.Sprintf("%.1f %ciB", - float64(b)/float64(div), "KMGTPE"[exp]) + base := math.Log(float64(b)) / math.Log(unit) + number := math.Pow(unit, base-math.Floor(base)) + return fmt.Sprintf("%.1f %ciB", number, "KMGTPE"[int64(base)-1]) } |