@@ -4,7 +4,6 @@ package miniredis
44
55import (
66 "fmt"
7- "regexp"
87 "strings"
98
109 "github.com/alicebob/miniredis/v2/server"
@@ -35,11 +34,11 @@ func (m *Miniredis) cmdSubscribe(c *server.Peer, cmd string, args []string) {
3534 sub := m .subscribedState (c )
3635 for _ , channel := range args {
3736 n := sub .Subscribe (channel )
38- c .Block (func (c * server.Writer ) {
39- c .WriteLen (3 )
40- c .WriteBulk ("subscribe" )
41- c .WriteBulk (channel )
42- c .WriteInt (n )
37+ c .Block (func (w * server.Writer ) {
38+ w .WriteLen (3 )
39+ w .WriteBulk ("subscribe" )
40+ w .WriteBulk (channel )
41+ w .WriteInt (n )
4342 })
4443 }
4544 })
@@ -63,11 +62,11 @@ func (m *Miniredis) cmdUnsubscribe(c *server.Peer, cmd string, args []string) {
6362 // there is no de-duplication
6463 for _ , channel := range channels {
6564 n := sub .Unsubscribe (channel )
66- c .Block (func (c * server.Writer ) {
67- c .WriteLen (3 )
68- c .WriteBulk ("unsubscribe" )
69- c .WriteBulk (channel )
70- c .WriteInt (n )
65+ c .Block (func (w * server.Writer ) {
66+ w .WriteLen (3 )
67+ w .WriteBulk ("unsubscribe" )
68+ w .WriteBulk (channel )
69+ w .WriteInt (n )
7170 })
7271 }
7372
@@ -92,116 +91,16 @@ func (m *Miniredis) cmdPsubscribe(c *server.Peer, cmd string, args []string) {
9291 sub := m .subscribedState (c )
9392 for _ , pat := range args {
9493 n := sub .Psubscribe (pat )
95- c .Block (func (c * server.Writer ) {
96- c .WriteLen (3 )
97- c .WriteBulk ("psubscribe" )
98- c .WriteBulk (pat )
99- c .WriteInt (n )
94+ c .Block (func (w * server.Writer ) {
95+ w .WriteLen (3 )
96+ w .WriteBulk ("psubscribe" )
97+ w .WriteBulk (pat )
98+ w .WriteInt (n )
10099 })
101100 }
102101 })
103102}
104103
105- func compileChannelPattern (pattern string ) * regexp.Regexp {
106- const readingLiteral uint8 = 0
107- const afterEscape uint8 = 1
108- const inClass uint8 = 2
109-
110- rgx := []rune {'\\' , 'A' }
111- state := readingLiteral
112- literals := []rune {}
113- klass := map [rune ]struct {}{}
114-
115- for _ , c := range pattern {
116- switch state {
117- case readingLiteral :
118- switch c {
119- case '\\' :
120- state = afterEscape
121- case '?' :
122- rgx = append (rgx , append ([]rune (regexp .QuoteMeta (string (literals ))), '.' )... )
123- literals = []rune {}
124- case '*' :
125- rgx = append (rgx , append ([]rune (regexp .QuoteMeta (string (literals ))), '.' , '*' )... )
126- literals = []rune {}
127- case '[' :
128- rgx = append (rgx , []rune (regexp .QuoteMeta (string (literals )))... )
129- literals = []rune {}
130- state = inClass
131- default :
132- literals = append (literals , c )
133- }
134- case afterEscape :
135- literals = append (literals , c )
136- state = readingLiteral
137- case inClass :
138- if c == ']' {
139- expr := []rune {'[' }
140-
141- if _ , hasDash := klass ['-' ]; hasDash {
142- delete (klass , '-' )
143- expr = append (expr , '-' )
144- }
145-
146- flatClass := make ([]rune , len (klass ))
147- i := 0
148-
149- for c := range klass {
150- flatClass [i ] = c
151- i ++
152- }
153-
154- klass = map [rune ]struct {}{}
155- expr = append (append (expr , []rune (regexp .QuoteMeta (string (flatClass )))... ), ']' )
156-
157- if len (expr ) < 3 {
158- rgx = append (rgx , 'x' , '\\' , 'b' , 'y' )
159- } else {
160- rgx = append (rgx , expr ... )
161- }
162-
163- state = readingLiteral
164- } else {
165- klass [c ] = struct {}{}
166- }
167- }
168- }
169-
170- switch state {
171- case afterEscape :
172- rgx = append (rgx , '\\' , '\\' )
173- case inClass :
174- if len (klass ) < 0 {
175- rgx = append (rgx , '\\' , '[' )
176- } else {
177- expr := []rune {'[' }
178-
179- if _ , hasDash := klass ['-' ]; hasDash {
180- delete (klass , '-' )
181- expr = append (expr , '-' )
182- }
183-
184- flatClass := make ([]rune , len (klass ))
185- i := 0
186-
187- for c := range klass {
188- flatClass [i ] = c
189- i ++
190- }
191-
192- expr = append (append (expr , []rune (regexp .QuoteMeta (string (flatClass )))... ), ']' )
193-
194- if len (expr ) < 3 {
195- rgx = append (rgx , 'x' , '\\' , 'b' , 'y' )
196- } else {
197- rgx = append (rgx , expr ... )
198- }
199- }
200- }
201-
202- return regexp .MustCompile (string (append (rgx , '\\' , 'z' )))
203- }
204-
205104// PUNSUBSCRIBE
206105func (m * Miniredis ) cmdPunsubscribe (c * server.Peer , cmd string , args []string ) {
207106 if ! m .handleAuth (c ) {
@@ -220,11 +119,11 @@ func (m *Miniredis) cmdPunsubscribe(c *server.Peer, cmd string, args []string) {
220119 // there is no de-duplication
221120 for _ , pat := range patterns {
222121 n := sub .Punsubscribe (pat )
223- c .Block (func (c * server.Writer ) {
224- c .WriteLen (3 )
225- c .WriteBulk ("punsubscribe" )
226- c .WriteBulk (pat )
227- c .WriteInt (n )
122+ c .Block (func (w * server.Writer ) {
123+ w .WriteLen (3 )
124+ w .WriteBulk ("punsubscribe" )
125+ w .WriteBulk (pat )
126+ w .WriteInt (n )
228127 })
229128 }
230129
0 commit comments