From 369e2432c4907d28e5052599162c20bca3c0aacf Mon Sep 17 00:00:00 2001 From: storezhang Date: Thu, 7 Nov 2024 14:52:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=AD=97=E8=8A=82):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=88=90=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/size/bytes.go | 14 +++++------- internal/size/bytes_formatter.go | 37 +++++++++++++++++++------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/internal/size/bytes.go b/internal/size/bytes.go index 41d72db..0697786 100644 --- a/internal/size/bytes.go +++ b/internal/size/bytes.go @@ -54,20 +54,16 @@ func ParseBytes(from string) (bytes Bytes, err error) { return } -func (b *Bytes) String() string { +func (b Bytes) String() string { return b.Formatter().Format() } -func (b *Bytes) Bit() int64 { - return int64(*b) * 8 +func (b Bytes) Bit() int64 { + return int64(b) * 8 } -func (b *Bytes) Byte() int64 { - return int64(*b) -} - -func (b *Bytes) Bit32() int32 { - return int32(*b) * 8 +func (b Bytes) Byte() int64 { + return int64(b) } func (b *Bytes) Formatter() *BytesFormatter { diff --git a/internal/size/bytes_formatter.go b/internal/size/bytes_formatter.go index 88ae069..312b623 100644 --- a/internal/size/bytes_formatter.go +++ b/internal/size/bytes_formatter.go @@ -56,47 +56,54 @@ func (b *BytesFormatter) Separator(separator rune) *BytesFormatter { } func (b *BytesFormatter) Format() string { - sb := new(strings.Builder) + builder := new(strings.Builder) + writed := false for { if *b.size < b.unit { break } + writed = true switch { case *b.size >= BytesEB: unit := *b.size / BytesEB *b.size -= unit * BytesEB - sb.WriteString(strconv.Itoa(int(unit))) - sb.WriteRune('e') + builder.WriteString(strconv.FormatInt(int64(unit), 10)) + builder.WriteRune('e') case *b.size >= BytesPB: unit := *b.size / BytesPB *b.size -= unit * BytesPB - sb.WriteString(strconv.Itoa(int(unit))) - sb.WriteRune('p') + builder.WriteString(strconv.FormatInt(int64(unit), 10)) + builder.WriteRune('p') case *b.size >= BytesTB: unit := *b.size / BytesTB *b.size -= unit * BytesTB - sb.WriteString(strconv.Itoa(int(unit))) - sb.WriteRune('t') + builder.WriteString(strconv.FormatInt(int64(unit), 10)) + builder.WriteRune('t') case *b.size >= BytesGB: unit := *b.size / BytesGB *b.size -= unit * BytesGB - sb.WriteString(strconv.Itoa(int(unit))) - sb.WriteRune('g') + builder.WriteString(strconv.FormatInt(int64(unit), 10)) + builder.WriteRune('g') case *b.size >= BytesMB: unit := *b.size / BytesMB *b.size -= unit * BytesMB - sb.WriteString(strconv.Itoa(int(unit))) - sb.WriteRune('m') + builder.WriteString(strconv.FormatInt(int64(unit), 10)) + builder.WriteRune('m') case *b.size >= BytesKB: unit := *b.size / BytesKB *b.size -= unit * BytesKB - sb.WriteString(strconv.Itoa(int(unit))) - sb.WriteRune('k') + builder.WriteString(strconv.FormatInt(int64(unit), 10)) + builder.WriteRune('k') } - sb.WriteRune(b.separator) + builder.WriteRune(b.separator) + } + if !writed { + builder.WriteString(strconv.FormatInt(int64(*b.size), 10)) + builder.WriteRune('b') + builder.WriteRune(b.separator) } - return sb.String()[:sb.Len()-1] // 去掉最后一个分隔符 + return builder.String()[:builder.Len()-1] // 去掉最后一个分隔符 }