Skip to content

Commit

Permalink
fix(字节): 修复转换成字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
storezhang committed Nov 7, 2024
1 parent 89cb2c0 commit 369e243
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
14 changes: 5 additions & 9 deletions internal/size/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 22 additions & 15 deletions internal/size/bytes_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] // 去掉最后一个分隔符
}

0 comments on commit 369e243

Please sign in to comment.