Browse Source

fix #306

Fix spurious bidi rule violations in casefolding channel names
by stripping the # before starting the casefolding.
tags/v1.0.0-rc1
Shivaram Lingamneni 5 years ago
parent
commit
40e63dbbe8
2 changed files with 25 additions and 7 deletions
  1. 14
    7
      irc/strings.go
  2. 11
    0
      irc/strings_test.go

+ 14
- 7
irc/strings.go View File

@@ -39,18 +39,25 @@ func Casefold(str string) (string, error) {
39 39
 
40 40
 // CasefoldChannel returns a casefolded version of a channel name.
41 41
 func CasefoldChannel(name string) (string, error) {
42
-	lowered, err := Casefold(name)
43
-
44
-	if err != nil {
45
-		return "", err
46
-	} else if len(lowered) == 0 {
42
+	if len(name) == 0 {
47 43
 		return "", errStringIsEmpty
48 44
 	}
49 45
 
50
-	if lowered[0] != '#' {
46
+	// don't casefold the preceding #'s
47
+	var start int
48
+	for start = 0; start < len(name) && name[start] == '#'; start += 1 {
49
+	}
50
+
51
+	if start == 0 {
52
+		// no preceding #'s
51 53
 		return "", errInvalidCharacter
52 54
 	}
53 55
 
56
+	lowered, err := Casefold(name[start:])
57
+	if err != nil {
58
+		return "", err
59
+	}
60
+
54 61
 	// space can't be used
55 62
 	// , is used as a separator
56 63
 	// * is used in mask matching
@@ -59,7 +66,7 @@ func CasefoldChannel(name string) (string, error) {
59 66
 		return "", errInvalidCharacter
60 67
 	}
61 68
 
62
-	return lowered, err
69
+	return name[:start] + lowered, err
63 70
 }
64 71
 
65 72
 // CasefoldName returns a casefolded version of a nick/user name.

+ 11
- 0
irc/strings_test.go View File

@@ -40,10 +40,21 @@ func TestCasefoldChannel(t *testing.T) {
40 40
 			channel: "#",
41 41
 			folded:  "#",
42 42
 		},
43
+		{
44
+			channel: "#中文频道",
45
+			folded:  "#中文频道",
46
+		},
47
+		{
48
+			// Hebrew; it's up to the client to display this right-to-left, including the #
49
+			channel: "#שלום",
50
+			folded:  "#שלום",
51
+		},
43 52
 	}
44 53
 
45 54
 	for _, errCase := range []string{
46 55
 		"", "#*starpower", "# NASA", "#interro?", "OOF#", "foo",
56
+		// bidi violation mixing latin and hebrew characters:
57
+		"#shalomעליכם",
47 58
 	} {
48 59
 		testCases = append(testCases, channelTest{channel: errCase, err: true})
49 60
 	}

Loading…
Cancel
Save