3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-10-13 22:17:27 +02:00

refactor 005 token generation

This commit is contained in:
Shivaram Lingamneni 2025-02-05 00:47:23 -05:00
parent d1126b53eb
commit a850602bcc
2 changed files with 23 additions and 27 deletions

View File

@ -5,7 +5,7 @@ package isupport
import ( import (
"fmt" "fmt"
"sort" "slices"
"strings" "strings"
) )
@ -58,7 +58,7 @@ func getTokenString(name string, value string) string {
// GetDifference returns the difference between two token lists. // GetDifference returns the difference between two token lists.
func (il *List) GetDifference(newil *List) [][]string { func (il *List) GetDifference(newil *List) [][]string {
var outTokens sort.StringSlice var outTokens []string
// append removed tokens // append removed tokens
for name := range il.Tokens { for name := range il.Tokens {
@ -84,7 +84,7 @@ func (il *List) GetDifference(newil *List) [][]string {
outTokens = append(outTokens, token) outTokens = append(outTokens, token)
} }
sort.Sort(outTokens) slices.Sort(outTokens)
// create output list // create output list
replies := make([][]string, 0) replies := make([][]string, 0)
@ -117,38 +117,34 @@ func (il *List) GetDifference(newil *List) [][]string {
// RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply // RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
func (il *List) RegenerateCachedReply() (err error) { func (il *List) RegenerateCachedReply() (err error) {
il.CachedReply = make([][]string, 0) var tokens []string
var length int // Length of the current cache for name, value := range il.Tokens {
var cache []string // Token list cache token := getTokenString(name, value)
// make sure we get a sorted list of tokens, needed for tests and looks nice
var tokens sort.StringSlice
for name := range il.Tokens {
tokens = append(tokens, name)
}
sort.Sort(tokens)
for _, name := range tokens {
token := getTokenString(name, il.Tokens[name])
if token[0] == ':' || strings.Contains(token, " ") { if token[0] == ':' || strings.Contains(token, " ") {
err = fmt.Errorf("bad isupport token (cannot contain spaces or start with :): %s", token) err = fmt.Errorf("bad isupport token (cannot contain spaces or start with :): %s", token)
continue continue
} }
tokens = append(tokens, token)
}
// make sure we get a sorted list of tokens, needed for tests and looks nice
slices.Sort(tokens)
if len(token)+length <= maxLastArgLength { var cache []string // Tokens in current line
// account for the space separating tokens var length int // Length of the current line
if len(cache) > 0 {
length++
}
cache = append(cache, token)
length += len(token)
}
if len(cache) == maxParameters || len(token)+length >= maxLastArgLength { for _, token := range tokens {
// account for the space separating tokens
if len(cache) == maxParameters || (len(token)+1)+length > maxLastArgLength {
il.CachedReply = append(il.CachedReply, cache) il.CachedReply = append(il.CachedReply, cache)
cache = make([]string, 0) cache = nil
length = 0 length = 0
} }
if len(cache) > 0 {
length++
}
length += len(token)
cache = append(cache, token)
} }
if len(cache) > 0 { if len(cache) > 0 {

View File

@ -37,7 +37,7 @@ func TestISUPPORT(t *testing.T) {
} }
if !reflect.DeepEqual(tListLong.CachedReply, longReplies) { if !reflect.DeepEqual(tListLong.CachedReply, longReplies) {
t.Errorf("Multiple output replies did not match, got [%v]", longReplies) t.Errorf("Multiple output replies did not match, got [%v]", tListLong.CachedReply)
} }
// create first list // create first list