Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
412277b3e0
|
|||
e46f8019ec
|
|||
ae952b2166
|
|||
b24dba9a45
|
|||
cfbc20367d
|
|||
e25912758e
|
@@ -74,7 +74,7 @@ func processEnvOverrides(rval reflect.Value, delim string, prefix string) error
|
|||||||
|
|
||||||
rvfield.Set(reflect.ValueOf(envval))
|
rvfield.Set(reflect.ValueOf(envval))
|
||||||
|
|
||||||
fmt.Printf("[CONF] Overwrite config '%s' () with '%s'\n", fullEnvKey, envval)
|
fmt.Printf("[CONF] Overwrite config '%s' with '%s'\n", fullEnvKey, envval)
|
||||||
|
|
||||||
} else if rvfield.Type() == reflect.TypeOf(int(0)) {
|
} else if rvfield.Type() == reflect.TypeOf(int(0)) {
|
||||||
|
|
||||||
|
116
dataext/stack.go
Normal file
116
dataext/stack.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package dataext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrEmptyStack = errors.New("stack is empty")
|
||||||
|
|
||||||
|
type Stack[T any] struct {
|
||||||
|
lock *sync.Mutex
|
||||||
|
data []T
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStack[T any](threadsafe bool, initialCapacity int) *Stack[T] {
|
||||||
|
var lck *sync.Mutex = nil
|
||||||
|
if threadsafe {
|
||||||
|
lck = &sync.Mutex{}
|
||||||
|
}
|
||||||
|
return &Stack[T]{
|
||||||
|
lock: lck,
|
||||||
|
data: make([]T, 0, initialCapacity),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) Push(v T) {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
s.data = append(s.data, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) Pop() (T, error) {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(s.data)
|
||||||
|
if l == 0 {
|
||||||
|
return *new(T), ErrEmptyStack
|
||||||
|
}
|
||||||
|
|
||||||
|
result := s.data[l-1]
|
||||||
|
s.data = s.data[:l-1]
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) OptPop() *T {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(s.data)
|
||||||
|
if l == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result := s.data[l-1]
|
||||||
|
s.data = s.data[:l-1]
|
||||||
|
|
||||||
|
return langext.Ptr(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) Peek() (T, error) {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(s.data)
|
||||||
|
|
||||||
|
if l == 0 {
|
||||||
|
return *new(T), ErrEmptyStack
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.data[l-1], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) OptPeek() *T {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(s.data)
|
||||||
|
|
||||||
|
if l == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return langext.Ptr(s.data[l-1])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) Length() int {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(s.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stack[T]) Empty() bool {
|
||||||
|
if s.lock != nil {
|
||||||
|
s.lock.Lock()
|
||||||
|
defer s.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(s.data) == 0
|
||||||
|
}
|
@@ -34,3 +34,7 @@ func IsNil(i interface{}) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PtrEquals[T comparable](v1 *T, v2 *T) bool {
|
||||||
|
return (v1 == nil && v2 == nil) || (v1 != nil && v2 != nil && *v1 == *v2)
|
||||||
|
}
|
||||||
|
@@ -8,6 +8,7 @@ type Regex interface {
|
|||||||
MatchAll(haystack string) []RegexMatch
|
MatchAll(haystack string) []RegexMatch
|
||||||
ReplaceAll(haystack string, repl string, literal bool) string
|
ReplaceAll(haystack string, repl string, literal bool) string
|
||||||
ReplaceAllFunc(haystack string, repl func(string) string) string
|
ReplaceAllFunc(haystack string, repl func(string) string) string
|
||||||
|
RemoveAll(haystack string) string
|
||||||
GroupCount() int
|
GroupCount() int
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,6 +72,10 @@ func (w *regexWrapper) ReplaceAllFunc(haystack string, repl func(string) string)
|
|||||||
return w.rex.ReplaceAllStringFunc(haystack, repl)
|
return w.rex.ReplaceAllStringFunc(haystack, repl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *regexWrapper) RemoveAll(haystack string) string {
|
||||||
|
return w.rex.ReplaceAllLiteralString(haystack, "")
|
||||||
|
}
|
||||||
|
|
||||||
// GroupCount returns the amount of groups in this match, does not count group-0 (whole match)
|
// GroupCount returns the amount of groups in this match, does not count group-0 (whole match)
|
||||||
func (w *regexWrapper) GroupCount() int {
|
func (w *regexWrapper) GroupCount() int {
|
||||||
return len(w.subnames) - 1
|
return len(w.subnames) - 1
|
||||||
|
Reference in New Issue
Block a user