-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_mention.go
103 lines (96 loc) · 2.2 KB
/
get_mention.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gommand
import "io"
// Get the mention if it exists.
func getMention(r io.ReadSeeker, char uint8, role bool) *string {
// Defines the parsing stage.
stage := uint8(0)
// Defines the ID we will compare to.
CmpID := ""
// Defines if this was a mention.
mention := false
// Loop through chars until we are sure it's a mention.
start := true
for {
ob := make([]byte, 1)
_, err := r.Read(ob)
if err != nil {
// Check the stage. If it can't be a ID, return nil.
if stage == 3 {
return &CmpID
}
return nil
}
if ob[0] == ' ' {
if !start {
// This isn't the start. Is it a mention?
if mention {
// Isn't a prefix.
return nil
}
// Return the ID.
return &CmpID
}
} else {
// Set start to false.
start = false
// Check the stage.
if stage == 0 {
// We expect a '<' char here.
if ob[0] == '<' {
// This is ok! move to stage 1 (type symbol).
stage = 1
mention = true
} else if ob[0] > 46 && 58 > ob[0] {
// We should move to stage 3.
CmpID += string(ob[0])
stage = 3
} else {
// This is invalid for this stage.
return nil
}
} else if stage == 1 {
if ob[0] == char {
// This is increasingly looking like a mention. Move to stage 2 (possible number/explanation mark).
stage = 2
} else {
// This isn't a mention.
return nil
}
} else if stage == 2 {
x := uint8('!')
if role {
x = '&'
}
if ob[0] == x {
// Ok, we should be ok to move to stage 3 without any ID logging.
stage = 3
} else {
// Is this within 0-9?
if ob[0] > 46 && 58 > ob[0] {
// It is. Add to the ID and make it stage 3.
stage = 3
CmpID += string(ob[0])
} else {
// Not a mention.
return nil
}
}
} else if stage == 3 {
if ob[0] == '>' {
// This is the end. We should return here.
return &CmpID
} else if ob[0] > 46 && 58 > ob[0] {
// Append to the ID.
CmpID += string(ob[0])
} else {
// Return nil if this is a mention. If it is, rewind one and return the ID.
if mention {
return nil
}
_, _ = r.Seek(-1, io.SeekCurrent)
return &CmpID
}
}
}
}
}