Files
torrserver/server/utils/strings.go
T
nkozobrod 616c6b1c62
Release Docker multi arch / docker (push) Has been cancelled
Test Install Script / Test Script Syntax (push) Has been cancelled
Test Install Script / Test on almalinux-10 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-10 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-8 (root) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (default) (push) Has been cancelled
Test Install Script / Test on almalinux-9 (root) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (default) (push) Has been cancelled
Test Install Script / Test on amazonlinux-2 (root) (push) Has been cancelled
Test Install Script / Test on debian-11 (default) (push) Has been cancelled
Test Install Script / Test on debian-11 (root) (push) Has been cancelled
Test Install Script / Test on debian-12 (default) (push) Has been cancelled
Test Install Script / Test on debian-12 (root) (push) Has been cancelled
Test Install Script / Test on debian-13 (default) (push) Has been cancelled
Test Install Script / Test on debian-13 (root) (push) Has been cancelled
Test Install Script / Test on fedora-latest (default) (push) Has been cancelled
Test Install Script / Test on fedora-latest (root) (push) Has been cancelled
Test Install Script / Test on rocky-10 (default) (push) Has been cancelled
Test Install Script / Test on rocky-10 (root) (push) Has been cancelled
Test Install Script / Test on rocky-8 (default) (push) Has been cancelled
Test Install Script / Test on rocky-8 (root) (push) Has been cancelled
Test Install Script / Test on rocky-9 (default) (push) Has been cancelled
Test Install Script / Test on rocky-9 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-22.04 (root) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (default) (push) Has been cancelled
Test Install Script / Test on ubuntu-24.04 (root) (push) Has been cancelled
Initial commit: docker compose config
2026-05-30 12:07:11 +00:00

100 lines
1.6 KiB
Go

package utils
import (
"fmt"
"strconv"
"strings"
"unicode"
)
const (
_ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier
KB
MB
GB
TB
PB
EB
)
func Format(b float64) string {
multiple := ""
value := b
switch {
case b >= EB:
value /= EB
multiple = "EB"
case b >= PB:
value /= PB
multiple = "PB"
case b >= TB:
value /= TB
multiple = "TB"
case b >= GB:
value /= GB
multiple = "GB"
case b >= MB:
value /= MB
multiple = "MB"
case b >= KB:
value /= KB
multiple = "KB"
case b == 0:
return "0"
default:
return strconv.FormatInt(int64(b), 10) + "B"
}
return fmt.Sprintf("%.2f%s", value, multiple)
}
func CommonPrefix(first, second string) string {
var result strings.Builder
minLength := len(first)
if len(second) < minLength {
minLength = len(second)
}
for i := 0; i < minLength; i++ {
if first[i] != second[i] {
break
}
result.WriteByte(first[i])
}
return result.String()
}
func NumberPrefix(str string) (int, error) {
var result strings.Builder
for i := 0; i < len(str); i++ {
if !unicode.IsDigit(rune(str[i])) {
break
}
result.WriteByte(str[i])
}
return strconv.Atoi(result.String())
}
func CompareStrings(first, second string) bool {
commonPrefix := CommonPrefix(first, second)
resultStr1 := strings.TrimPrefix(first, commonPrefix)
resultStr2 := strings.TrimPrefix(second, commonPrefix)
num1, err1 := NumberPrefix(resultStr1)
num2, err2 := NumberPrefix(resultStr2)
if err1 == nil && err2 == nil {
return num1 < num2
}
if err1 == nil {
return true
} else if err2 == nil {
return false
}
return resultStr1 < resultStr2
}