github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/escape.go (about) 1 package main 2 3 import "strings" 4 5 // The special strings should be as unusual as possible, but short. 6 // It's important that the various characters will not be syntax highlighted separately. 7 const ( 8 escapedLessThan = "æ010_" + "lt" + "_101æ" 9 escapedGreaterThan = "æ010_" + "gt" + "_101æ" 10 escapedCommentStart = "____æ____" 11 escapedCommentEnd = "____ø____" 12 ) 13 14 var ( 15 escapeReplacer = strings.NewReplacer("<", escapedLessThan, ">", escapedGreaterThan) 16 unEscapeReplacer = strings.NewReplacer(escapedLessThan, "<", escapedGreaterThan, ">") 17 shEscapeReplacer = strings.NewReplacer("<", escapedLessThan, ">", escapedGreaterThan, "/*", escapedCommentStart, "*/", escapedCommentEnd) 18 shUnEscapeReplacer = strings.NewReplacer(escapedLessThan, "<", escapedGreaterThan, ">", escapedCommentStart, "/*", escapedCommentEnd, "*/") 19 ) 20 21 // Escape escapes < and > by replacing them with specialString1 and specialString2 22 func Escape(s string) string { 23 return escapeReplacer.Replace(s) 24 } 25 26 // UnEscape escapes specialString1 and specialString2 by replacing them with < and > 27 func UnEscape(s string) string { 28 return unEscapeReplacer.Replace(s) 29 } 30 31 // ShEscape escapes < and > by replacing them with specialString1 and specialString2 32 // Also escapes /* and */ 33 func ShEscape(s string) string { 34 return shEscapeReplacer.Replace(s) 35 } 36 37 // ShUnEscape escapes specialString1 and specialString2 by replacing them with < and > 38 // Also escapes /* and */ 39 func ShUnEscape(s string) string { 40 return shUnEscapeReplacer.Replace(s) 41 }