mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-08-02 13:10:31 -04:00
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
99 lines
1.8 KiB
Go
99 lines
1.8 KiB
Go
package units
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
var errInvalidSize = errors.New("invalid size")
|
|
var errInvalidUnit = errors.New("invalid or unsupported unit")
|
|
|
|
// ByteSize is the size of bytes
|
|
type ByteSize uint64
|
|
|
|
const (
|
|
_ = iota
|
|
// KB = 1KB
|
|
KB ByteSize = 1 << (10 * iota)
|
|
// MB = 1MB
|
|
MB
|
|
// GB = 1GB
|
|
GB
|
|
// TB = 1TB
|
|
TB
|
|
// PB = 1PB
|
|
PB
|
|
// EB = 1EB
|
|
EB
|
|
)
|
|
|
|
func (b ByteSize) String() string {
|
|
unit := ""
|
|
value := float64(0)
|
|
switch {
|
|
case b == 0:
|
|
return "0"
|
|
case b < KB:
|
|
unit = "B"
|
|
value = float64(b)
|
|
case b < MB:
|
|
unit = "KB"
|
|
value = float64(b) / float64(KB)
|
|
case b < GB:
|
|
unit = "MB"
|
|
value = float64(b) / float64(MB)
|
|
case b < TB:
|
|
unit = "GB"
|
|
value = float64(b) / float64(GB)
|
|
case b < PB:
|
|
unit = "TB"
|
|
value = float64(b) / float64(TB)
|
|
case b < EB:
|
|
unit = "PB"
|
|
value = float64(b) / float64(PB)
|
|
default:
|
|
unit = "EB"
|
|
value = float64(b) / float64(EB)
|
|
}
|
|
result := strconv.FormatFloat(value, 'f', 2, 64)
|
|
result = strings.TrimSuffix(result, ".0")
|
|
return result + unit
|
|
}
|
|
|
|
// Parse parses ByteSize from string
|
|
func (b *ByteSize) Parse(s string) error {
|
|
s = strings.TrimSpace(s)
|
|
s = strings.ToUpper(s)
|
|
i := strings.IndexFunc(s, unicode.IsLetter)
|
|
if i == -1 {
|
|
return errInvalidUnit
|
|
}
|
|
|
|
bytesString, multiple := s[:i], s[i:]
|
|
bytes, err := strconv.ParseFloat(bytesString, 64)
|
|
if err != nil || bytes <= 0 {
|
|
return errInvalidSize
|
|
}
|
|
switch multiple {
|
|
case "B":
|
|
*b = ByteSize(bytes)
|
|
case "K", "KB", "KIB":
|
|
*b = ByteSize(bytes * float64(KB))
|
|
case "M", "MB", "MIB":
|
|
*b = ByteSize(bytes * float64(MB))
|
|
case "G", "GB", "GIB":
|
|
*b = ByteSize(bytes * float64(GB))
|
|
case "T", "TB", "TIB":
|
|
*b = ByteSize(bytes * float64(TB))
|
|
case "P", "PB", "PIB":
|
|
*b = ByteSize(bytes * float64(PB))
|
|
case "E", "EB", "EIB":
|
|
*b = ByteSize(bytes * float64(EB))
|
|
default:
|
|
return errInvalidUnit
|
|
}
|
|
return nil
|
|
}
|