mc root source folder cleanup

This commit is contained in:
adman
2026-06-27 13:54:54 +02:00
parent b2784574bd
commit 666f250b3e
480 changed files with 325 additions and 325 deletions
+43
View File
@@ -0,0 +1,43 @@
package util
import (
"slices"
"unicode"
)
var boundaries = []rune{' ', '_', '-', '.', ',', ';', ':', '(', ')', '[', ']', '{', '}'}
func CamelCase(s string) string {
ret := ""
atWordBoundary := true
for _, c := range s {
if slices.Contains(boundaries, c) {
atWordBoundary = true
continue
}
if atWordBoundary && unicode.IsLower(c) {
c = unicode.ToUpper(c)
}
atWordBoundary = false
ret += string(c)
}
return ret
}
func SnakeCase(s string) string {
ret := ""
for i, c := range s {
if unicode.IsUpper(c) {
if i > 0 {
ret += "_"
}
c = unicode.ToLower(c)
}
ret += string(c)
}
return ret
}
func FirstLetterLower(s string) string {
return string(unicode.ToLower(rune(s[0]))) + s[1:]
}
+29
View File
@@ -0,0 +1,29 @@
package util_test
import (
"testing"
"github.com/admin-else/strom/mc/util"
)
func TestCamelCase(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello world", "HelloWorld"},
{"test_case", "TestCase"},
{"simple-test", "SimpleTest"},
{"already.Camel", "AlreadyCamel"},
{"UPPER_CASE", "UPPERCASE"},
{"mixed_Case_string", "MixedCaseString"},
{"", ""},
}
for _, test := range tests {
result := util.CamelCase(test.input)
if result != test.expected {
t.Errorf("CamelCase(%q) = %q; expected %q", test.input, result, test.expected)
}
}
}
+11
View File
@@ -0,0 +1,11 @@
package util
import "math"
func FloorDiv(a, b int32) int32 {
return (a - ((a%b + b) % b)) / b
}
func BpeByNum(x float64) uint8 {
return uint8(math.Ceil(math.Log2(x)))
}
+40
View File
@@ -0,0 +1,40 @@
package util
import "fmt"
func MustOk[T any](v T, ok bool) T {
if !ok {
panic("not ok")
}
return v
}
func MustT[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func Must(err error) {
if err != nil {
panic(err)
}
}
func AssertTypeError[T any](v any) (T, error) {
var zero T
r, ok := v.(T)
if !ok {
return zero, fmt.Errorf("type assertion failed: %T cannot be asserted to %T", v, zero)
}
return r, nil
}
func MapGetError[K comparable, V any, M map[K]V](m M, k K) (V, error) {
v, ok := m[k]
if !ok {
return v, fmt.Errorf("key %v not found", k)
}
return v, nil
}
+20
View File
@@ -0,0 +1,20 @@
package util
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64
}
func IsLastElement[T comparable](s []T, e T) bool {
return len(s) > 0 && s[len(s)-1] == e
}
func GetNextLargestNumberSlice[T Numeric](a T, s []T) (ret T, found bool) {
for _, v := range s {
if v > a {
return v, true
}
}
return
}
+42
View File
@@ -0,0 +1,42 @@
package util
import (
"cmp"
"maps"
"slices"
)
func OrderedKeys[Map map[K]V, K cmp.Ordered, V any](m Map) []K {
keysIter := maps.Keys(m)
keys := slices.Collect(keysIter)
slices.Sort(keys)
return keys
}
func AssertAndConvertMapValues[T any, M map[K]any, K cmp.Ordered](m M) map[K]T {
ret := map[K]T{}
for k, v := range m {
ret[k] = v.(T)
}
return ret
}
func ReverseMap[M map[K]V, K cmp.Ordered, V cmp.Ordered](m M) map[V]K {
ret := map[V]K{}
for k, v := range m {
ret[v] = k
}
return ret
}
func PopFront[T any](s []T) (T, []T, bool) {
if len(s) == 0 {
var zero T
return zero, s, false
}
return s[0], s[1:], true
}
func CombineNamAndData(name string, data any) []any {
return []any{name, data}
}