Last change
on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago |
Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.
Signed-off-by: Izuru Yakumo <yakumo.izuru@…>
|
File size:
997 bytes
|
Rev | Line | |
---|
[822] | 1 | package irc
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "bytes"
|
---|
| 5 | "regexp"
|
---|
| 6 | )
|
---|
| 7 |
|
---|
| 8 | var maskTranslations = map[byte]string{
|
---|
| 9 | '?': ".",
|
---|
| 10 | '*': ".*",
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | // MaskToRegex converts an irc mask to a go Regexp for more convenient
|
---|
| 14 | // use. This should never return an error, but we have this here just
|
---|
| 15 | // in case.
|
---|
| 16 | func MaskToRegex(rawMask string) (*regexp.Regexp, error) {
|
---|
| 17 | input := bytes.NewBufferString(rawMask)
|
---|
| 18 |
|
---|
| 19 | output := &bytes.Buffer{}
|
---|
| 20 | output.WriteByte('^')
|
---|
| 21 |
|
---|
| 22 | for {
|
---|
| 23 | c, err := input.ReadByte()
|
---|
| 24 | if err != nil {
|
---|
| 25 | break
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | if c == '\\' {
|
---|
| 29 | c, err = input.ReadByte()
|
---|
| 30 | if err != nil {
|
---|
| 31 | output.WriteString(regexp.QuoteMeta("\\"))
|
---|
| 32 | break
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | if c == '?' || c == '*' || c == '\\' {
|
---|
| 36 | output.WriteString(regexp.QuoteMeta(string(c)))
|
---|
| 37 | } else {
|
---|
| 38 | output.WriteString(regexp.QuoteMeta("\\" + string(c)))
|
---|
| 39 | }
|
---|
| 40 | } else if trans, ok := maskTranslations[c]; ok {
|
---|
| 41 | output.WriteString(trans)
|
---|
| 42 | } else {
|
---|
| 43 | output.WriteString(regexp.QuoteMeta(string(c)))
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | output.WriteByte('$')
|
---|
| 48 |
|
---|
| 49 | return regexp.Compile(output.String())
|
---|
| 50 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.