Skip to content

Commit

Permalink
Add support for ForEachOperation.
Browse files Browse the repository at this point in the history
Upgrade x/text to its latest version.
Remove deprecated constants.
Refactor repairString to not use string ptr for performance reasons.

Signed-off-by: Aliwoto <aminnimaj@gmail.com>
  • Loading branch information
ALiwoto committed May 6, 2024
1 parent 5f9b3c9 commit db34aad
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 50 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ module github.com/ALiwoto/ssg

go 1.18

require golang.org/x/text v0.9.0
require golang.org/x/text v0.15.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
27 changes: 13 additions & 14 deletions ssg/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ const (
)

const (
JA_Str = "❞\u200d;" // start character (") for string in japanese.
JA_Equality = "=\u200d;" // equal character (=) in japanese.
JA_Ddot = ":\u200d;" // ddot character (:) in japanese.
JA_Coma = "、\u200d;" // coma character (,) in japanese.
JA_RealStr = "\uff4e" // the real str
JA_BrOpen = "「\u200d;" // the real str
JA_BrClose = "」\u200d;" // the real str
BACK_Str = `\"`
BACK_Flag = `\--`
BACK_Equality = `\=`
BACK_Ddot = `\:`
BACK_Coma = `\,`
BACK_BrOpen = `\[`
BACK_BrClose = `\]`
// ForEachOperationBreak will just continue the loop without doing anything.
ForEachOperationContinue = 0

// ForEachOperationBreak will just break the loop without doing anything.
ForEachOperationBreak = 1

// ForEachOperationBreak will just remove the current item from the list
// and continue the loop.
ForEachOperationRemove = 2

// ForEachOperationBreak will remove the current item from the list
// and break the loop.
ForEachOperationRemoveBreak = 3
)

// the base constant values.
Expand Down
10 changes: 5 additions & 5 deletions ssg/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,13 @@ func IsRuneNumber(r rune) bool {
return false
}

func repairString(value *string) *string {
func repairString(value string) string {
entered := false
ignoreNext := false
final := EMPTY
last := len(*value) - BaseIndex
last := len(value) - BaseIndex
next := BaseIndex
for i, current := range *value {
for i, current := range value {
if ignoreNext {
ignoreNext = false
continue
Expand Down Expand Up @@ -518,7 +518,7 @@ func repairString(value *string) *string {
if current == LineChar {
if i != last {
next = i + BaseOneIndex
if (*value)[next] == LineChar {
if value[next] == LineChar {
final += BackSlash +
string(current) + string(current)
ignoreNext = true
Expand All @@ -532,7 +532,7 @@ func repairString(value *string) *string {
final += string(current)
}

return &final
return final
}

func isSpecial(r rune) bool {
Expand Down
15 changes: 12 additions & 3 deletions ssg/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (s *StrongString) LockSpecial() {
// it will escape them.
// if it wasn't for this function, members had to
// escape all of these bullshits themselves...
final = *repairString(&final)
final = repairString(final)

final = strings.ReplaceAll(final, BACK_FLAG, JA_FLAG)
final = strings.ReplaceAll(final, BACK_EQUALITY, JA_EQUALITY)
Expand Down Expand Up @@ -584,15 +584,24 @@ func (s *AdvancedMap[TKey, TValue]) GetRandom() *TValue {
return value
}

func (s *AdvancedMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) bool) {
func (s *AdvancedMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) ForEachOperation) {
if fn == nil {
return
}
s.lock()

myFor:
for key, value := range s.values {
if fn(key, value) {
switch fn(key, value) {
case ForEachOperationContinue:
continue
case ForEachOperationBreak:
break myFor
case ForEachOperationRemove:
s.delete(key, false)
case ForEachOperationRemoveBreak:
s.delete(key, false)
break myFor
}
}

Expand Down
13 changes: 11 additions & 2 deletions ssg/safeEMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,32 @@ func (s *SafeEMap[TKey, TValue]) Delete(key TKey) {
s.delete(key, true)
}

func (s *SafeEMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) bool) {
func (s *SafeEMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) ForEachOperation) {
if fn == nil {
return
}
s.lock()

var tmpValue *TValue

myFor:
for key, value := range s.values {
if value == nil {
tmpValue = nil
} else {
tmpValue = value.GetValue()
}

if fn(key, tmpValue) {
switch fn(key, tmpValue) {
case ForEachOperationContinue:
continue
case ForEachOperationBreak:
break myFor
case ForEachOperationRemove:
s.delete(key, false)
case ForEachOperationRemoveBreak:
s.delete(key, false)
break myFor
}
}

Expand Down
49 changes: 26 additions & 23 deletions ssg/safeMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,67 +33,70 @@ func (s *SafeMap[TKey, TValue]) Add(key TKey, value *TValue) {
s.values[key] = value
}

func (s *SafeMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) bool) {
func (s *SafeMap[TKey, TValue]) ForEach(fn func(TKey, *TValue) ForEachOperation) {
if fn == nil {
return
}

s.lock()
myFor:
for key, value := range s.values {
if fn(key, value) {
switch fn(key, value) {
case ForEachOperationContinue:
continue
case ForEachOperationBreak:
break myFor
case ForEachOperationRemove:
s.delete(key, false)
case ForEachOperationRemoveBreak:
s.delete(key, false)
break myFor
}
}

s.unlock()
}

func (s *SafeMap[TKey, TValue]) ToArray() []TValue {
var array []TValue
s.rLock()

result := make([]TValue, len(s.values))
i := 0
for _, v := range s.values {
if v == nil {
array = append(array, s._default)
result[i] = s._default
i++
continue
}

array = append(array, *v)
result[i] = *v
i++
}
s.rUnlock()

return array
return result
}

func (s *SafeMap[TKey, TValue]) ToPointerArray() []*TValue {
var array []*TValue
s.rLock()

result := make([]*TValue, len(s.values))
i := 0
for _, v := range s.values {
if v == nil {
// most likely impossible, this checker is here just for more safety.
continue
}

array = append(array, v)
result[i] = v
i++
}
s.rUnlock()

return array
return result
}

func (s *SafeMap[TKey, TValue]) ToList() GenericList[*TValue] {
list := GetEmptyList[*TValue]()
s.rLock()
for _, v := range s.values {
if v == nil {
// most likely impossible, this checker is here just for more safety.
continue
}

list.Add(v)
}
s.rUnlock()

return list
return GetListFromArray(s.ToPointerArray())
}

func (s *SafeMap[TKey, TValue]) AddList(keyGetter func(*TValue) TKey, elements ...TValue) {
Expand Down
4 changes: 4 additions & 0 deletions ssg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ type UInt8Container = rangeValues.IntContainer[uint8]

type ExecuteCommandResult = shellUtils.ExecuteCommandResult

// ForEachOperation describes an operation that has to be returned
// from a ForEach method.
type ForEachOperation int

//type safeList[T any] #TODO: implement safe-list

type StringUniqueIdContainer = UniqueIdContainer[string]
Expand Down

0 comments on commit db34aad

Please sign in to comment.