3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00

consolidate ArgsToStrings

This commit is contained in:
Shivaram Lingamneni 2020-12-30 00:41:34 -05:00
parent 0c9ecbade7
commit ec375f5bdc
5 changed files with 22 additions and 42 deletions

View File

@ -1864,7 +1864,7 @@ func monitorListHandler(server *Server, client *Client, msg ircmsg.IrcMessage, r
nickList = append(nickList, replynick) nickList = append(nickList, replynick)
} }
for _, line := range utils.ArgsToStrings(maxLastArgLength, nickList, ",") { for _, line := range utils.BuildTokenLines(maxLastArgLength, nickList, ",") {
rb.Add(nil, server.name, RPL_MONLIST, nick, line) rb.Add(nil, server.name, RPL_MONLIST, nick, line)
} }
@ -1890,12 +1890,12 @@ func monitorStatusHandler(server *Server, client *Client, msg ircmsg.IrcMessage,
} }
if len(online) > 0 { if len(online) > 0 {
for _, line := range utils.ArgsToStrings(maxLastArgLength, online, ",") { for _, line := range utils.BuildTokenLines(maxLastArgLength, online, ",") {
rb.Add(nil, server.name, RPL_MONONLINE, client.Nick(), line) rb.Add(nil, server.name, RPL_MONONLINE, client.Nick(), line)
} }
} }
if len(offline) > 0 { if len(offline) > 0 {
for _, line := range utils.ArgsToStrings(maxLastArgLength, offline, ",") { for _, line := range utils.BuildTokenLines(maxLastArgLength, offline, ",") {
rb.Add(nil, server.name, RPL_MONOFFLINE, client.Nick(), line) rb.Add(nil, server.name, RPL_MONOFFLINE, client.Nick(), line)
} }
} }

View File

@ -18,37 +18,6 @@ var (
ErrInvalidParams = errors.New("Invalid parameters") ErrInvalidParams = errors.New("Invalid parameters")
) )
// ArgsToStrings takes the arguments and splits them into a series of strings,
// each argument separated by delim and each string bounded by maxLength.
func ArgsToStrings(maxLength int, arguments []string, delim string) []string {
var messages []string
var buffer string
for {
if len(arguments) < 1 {
break
}
if len(buffer) > 0 && maxLength < len(buffer)+len(delim)+len(arguments[0]) {
messages = append(messages, buffer)
buffer = ""
continue
}
if len(buffer) > 0 {
buffer += delim
}
buffer += arguments[0]
arguments = arguments[1:]
}
if len(buffer) > 0 {
messages = append(messages, buffer)
}
return messages
}
func StringToBool(str string) (result bool, err error) { func StringToBool(str string) (result bool, err error) {
switch strings.ToLower(str) { switch strings.ToLower(str) {
case "on", "true", "t", "yes", "y", "enabled": case "on", "true", "t", "yes", "y", "enabled":

View File

@ -5,14 +5,6 @@ package utils
import "testing" import "testing"
func TestArgsToStrings(t *testing.T) {
val := ArgsToStrings(512, []string{"a", "b", "c"}, ",")
assertEqual(val, []string{"a,b,c"}, t)
val = ArgsToStrings(10, []string{"abcd", "efgh", "ijkl"}, ",")
assertEqual(val, []string{"abcd,efgh", "ijkl"}, t)
}
func TestStringToBool(t *testing.T) { func TestStringToBool(t *testing.T) {
val, err := StringToBool("on") val, err := StringToBool("on")
assertEqual(val, true, t) assertEqual(val, true, t)

View File

@ -132,3 +132,14 @@ func (t *TokenLineBuilder) Lines() (result []string) {
} }
return return
} }
// BuildTokenLines is a convenience to apply TokenLineBuilder to a predetermined
// slice of tokens.
func BuildTokenLines(lineLen int, tokens []string, delim string) []string {
var tl TokenLineBuilder
tl.Initialize(lineLen, delim)
for _, arg := range tokens {
tl.Add(arg)
}
return tl.Lines()
}

View File

@ -35,3 +35,11 @@ func TestTokenLineBuilder(t *testing.T) {
t.Errorf("text incorrectly split into lines: %s instead of %s", joined, monteCristo) t.Errorf("text incorrectly split into lines: %s instead of %s", joined, monteCristo)
} }
} }
func TestBuildTokenLines(t *testing.T) {
val := BuildTokenLines(512, []string{"a", "b", "c"}, ",")
assertEqual(val, []string{"a,b,c"}, t)
val = BuildTokenLines(10, []string{"abcd", "efgh", "ijkl"}, ",")
assertEqual(val, []string{"abcd,efgh", "ijkl"}, t)
}