mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-24 09:03:14 +01:00
bb40325977
This PR showcases the implementation of additional linter rules. I've updated the golangci-lint GitHub Actions to the latest available version. This update makes sure that the tool works the same way locally - assuming being updated regularly - and with the GitHub Actions. I've also taken care of keeping all the GitHub Actions up to date, which helps our code stay current. But there's one part, goreleaser that's a bit tricky to test on our computers. So, it's important to take a close look at that. To make it easier to understand what I've done, I've made separate changes for each thing that the new linters found. This should help the people reviewing the changes see what's going on more clearly. Some of the changes might not be obvious at first glance. Things to consider for the future CI runs on Ubuntu so the static analysis only happens for Linux. Consider running it for the rest: Darwin, Windows
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package base62
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
base = uint32(len(alphabet))
|
|
)
|
|
|
|
// Encode encodes a uint32 value to a base62 string.
|
|
func Encode(num uint32) string {
|
|
if num == 0 {
|
|
return string(alphabet[0])
|
|
}
|
|
|
|
var encoded strings.Builder
|
|
|
|
for num > 0 {
|
|
remainder := num % base
|
|
encoded.WriteByte(alphabet[remainder])
|
|
num /= base
|
|
}
|
|
|
|
// Reverse the encoded string
|
|
encodedString := encoded.String()
|
|
reversed := reverse(encodedString)
|
|
return reversed
|
|
}
|
|
|
|
// Decode decodes a base62 string to a uint32 value.
|
|
func Decode(encoded string) (uint32, error) {
|
|
var decoded uint32
|
|
strLen := len(encoded)
|
|
|
|
for i, char := range encoded {
|
|
index := strings.IndexRune(alphabet, char)
|
|
if index < 0 {
|
|
return 0, fmt.Errorf("invalid character: %c", char)
|
|
}
|
|
|
|
decoded += uint32(index) * uint32(math.Pow(float64(base), float64(strLen-i-1)))
|
|
}
|
|
|
|
return decoded, nil
|
|
}
|
|
|
|
// Reverse a string.
|
|
func reverse(s string) string {
|
|
runes := []rune(s)
|
|
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
|
runes[i], runes[j] = runes[j], runes[i]
|
|
}
|
|
return string(runes)
|
|
}
|