From 40e63dbbe8a3076cbbf7808d2f633a368f28d8a6 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Wed, 5 Dec 2018 22:35:36 -0500 Subject: [PATCH 1/2] fix #306 Fix spurious bidi rule violations in casefolding channel names by stripping the # before starting the casefolding. --- irc/strings.go | 21 ++++++++++++++------- irc/strings_test.go | 11 +++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/irc/strings.go b/irc/strings.go index 2ccea99c..c5385853 100644 --- a/irc/strings.go +++ b/irc/strings.go @@ -39,18 +39,25 @@ func Casefold(str string) (string, error) { // CasefoldChannel returns a casefolded version of a channel name. func CasefoldChannel(name string) (string, error) { - lowered, err := Casefold(name) - - if err != nil { - return "", err - } else if len(lowered) == 0 { + if len(name) == 0 { return "", errStringIsEmpty } - if lowered[0] != '#' { + // don't casefold the preceding #'s + var start int + for start = 0; start < len(name) && name[start] == '#'; start += 1 { + } + + if start == 0 { + // no preceding #'s return "", errInvalidCharacter } + lowered, err := Casefold(name[start:]) + if err != nil { + return "", err + } + // space can't be used // , is used as a separator // * is used in mask matching @@ -59,7 +66,7 @@ func CasefoldChannel(name string) (string, error) { return "", errInvalidCharacter } - return lowered, err + return name[:start] + lowered, err } // CasefoldName returns a casefolded version of a nick/user name. diff --git a/irc/strings_test.go b/irc/strings_test.go index 7f23af83..513a4f8f 100644 --- a/irc/strings_test.go +++ b/irc/strings_test.go @@ -40,10 +40,21 @@ func TestCasefoldChannel(t *testing.T) { channel: "#", folded: "#", }, + { + channel: "#中文频道", + folded: "#中文频道", + }, + { + // Hebrew; it's up to the client to display this right-to-left, including the # + channel: "#שלום", + folded: "#שלום", + }, } for _, errCase := range []string{ "", "#*starpower", "# NASA", "#interro?", "OOF#", "foo", + // bidi violation mixing latin and hebrew characters: + "#shalomעליכם", } { testCases = append(testCases, channelTest{channel: errCase, err: true}) } From 92eca4d795ef60e6364278949738f6445088cfea Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Thu, 6 Dec 2018 06:46:11 -0500 Subject: [PATCH 2/2] add more test cases --- irc/strings_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/irc/strings_test.go b/irc/strings_test.go index 513a4f8f..48a7d538 100644 --- a/irc/strings_test.go +++ b/irc/strings_test.go @@ -40,6 +40,14 @@ func TestCasefoldChannel(t *testing.T) { channel: "#", folded: "#", }, + { + channel: "##", + folded: "##", + }, + { + channel: "##Ubuntu", + folded: "##ubuntu", + }, { channel: "#中文频道", folded: "#中文频道",