-
Notifications
You must be signed in to change notification settings - Fork 16
/
escape.go
41 lines (34 loc) · 1.48 KB
/
escape.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
package main
import "strings"
// The special strings should be as unusual as possible, but short.
// It's important that the various characters will not be syntax highlighted separately.
const (
escapedLessThan = "æ010_" + "lt" + "_101æ"
escapedGreaterThan = "æ010_" + "gt" + "_101æ"
escapedCommentStart = "____æ____"
escapedCommentEnd = "____ø____"
)
var (
escapeReplacer = strings.NewReplacer("<", escapedLessThan, ">", escapedGreaterThan)
unEscapeReplacer = strings.NewReplacer(escapedLessThan, "<", escapedGreaterThan, ">")
shEscapeReplacer = strings.NewReplacer("<", escapedLessThan, ">", escapedGreaterThan, "/*", escapedCommentStart, "*/", escapedCommentEnd)
shUnEscapeReplacer = strings.NewReplacer(escapedLessThan, "<", escapedGreaterThan, ">", escapedCommentStart, "/*", escapedCommentEnd, "*/")
)
// Escape escapes < and > by replacing them with specialString1 and specialString2
func Escape(s string) string {
return escapeReplacer.Replace(s)
}
// UnEscape escapes specialString1 and specialString2 by replacing them with < and >
func UnEscape(s string) string {
return unEscapeReplacer.Replace(s)
}
// ShEscape escapes < and > by replacing them with specialString1 and specialString2
// Also escapes /* and */
func ShEscape(s string) string {
return shEscapeReplacer.Replace(s)
}
// ShUnEscape escapes specialString1 and specialString2 by replacing them with < and >
// Also escapes /* and */
func ShUnEscape(s string) string {
return shUnEscapeReplacer.Replace(s)
}