go-htmlescape/saferune.go

181 lines
4.6 KiB
Go
Raw Normal View History

2024-02-13 05:32:35 +00:00
package htmlescape
var escaped00 []byte = []byte("�")
var escaped01 []byte = []byte("")
var escaped02 []byte = []byte("")
var escaped03 []byte = []byte("")
var escaped04 []byte = []byte("")
var escaped05 []byte = []byte("")
var escaped06 []byte = []byte("")
var escaped07 []byte = []byte("")
var escaped08 []byte = []byte("")
var escaped09 []byte = []byte("	")
var escaped0A []byte = []byte("
")
var escaped0B []byte = []byte("")
var escaped0C []byte = []byte("")
var escaped0D []byte = []byte("
")
var escaped0E []byte = []byte("")
var escaped0F []byte = []byte("")
var escaped10 []byte = []byte("")
var escaped11 []byte = []byte("")
var escaped12 []byte = []byte("")
var escaped13 []byte = []byte("")
var escaped14 []byte = []byte("")
var escaped15 []byte = []byte("")
var escaped16 []byte = []byte("")
var escaped17 []byte = []byte("")
var escaped18 []byte = []byte("")
var escaped19 []byte = []byte("")
var escaped1A []byte = []byte("")
var escaped1B []byte = []byte("")
var escaped1C []byte = []byte("")
var escaped1D []byte = []byte("")
var escaped1E []byte = []byte("")
var escaped1F []byte = []byte("")
const minCtrlBlock1 = 0x7F
var escaped7F []byte = []byte("")
var escaped80 []byte = []byte("€")
var escaped81 []byte = []byte("")
var escaped82 []byte = []byte("‚")
var escaped83 []byte = []byte("ƒ")
var escaped84 []byte = []byte("„")
var escaped85 []byte = []byte("…")
var escaped86 []byte = []byte("†")
var escaped87 []byte = []byte("‡")
var escaped88 []byte = []byte("ˆ")
var escaped89 []byte = []byte("‰")
var escaped8A []byte = []byte("Š")
var escaped8B []byte = []byte("‹")
var escaped8C []byte = []byte("Œ")
var escaped8D []byte = []byte("")
var escaped8E []byte = []byte("Ž")
var escaped8F []byte = []byte("")
var escaped90 []byte = []byte("")
var escaped91 []byte = []byte("‘")
var escaped92 []byte = []byte("’")
var escaped93 []byte = []byte("“")
var escaped94 []byte = []byte("”")
var escaped95 []byte = []byte("•")
var escaped96 []byte = []byte("–")
var escaped97 []byte = []byte("—")
var escaped98 []byte = []byte("˜")
var escaped99 []byte = []byte("™")
var escaped9A []byte = []byte("š")
var escaped9B []byte = []byte("›")
var escaped9C []byte = []byte("œ")
var escaped9D []byte = []byte("")
var escaped9E []byte = []byte("ž")
var escaped9F []byte = []byte("Ÿ")
var ctrlBlock0 [][]byte = [][]byte{
escaped00,
escaped01,
escaped02,
escaped03,
escaped04,
escaped05,
escaped06,
escaped07,
escaped08,
escaped09,
escaped0A,
escaped0B,
escaped0C,
escaped0D,
escaped0E,
escaped0F,
escaped10,
escaped11,
escaped12,
escaped13,
escaped14,
escaped15,
escaped16,
escaped17,
escaped18,
escaped19,
escaped1A,
escaped1B,
escaped1C,
escaped1D,
escaped1E,
escaped1F,
}
var ctrlBlock1 [][]byte = [][]byte{
escaped7F,
escaped80,
escaped81,
escaped82,
escaped83,
escaped84,
escaped85,
escaped86,
escaped87,
escaped88,
escaped89,
escaped8A,
escaped8B,
escaped8C,
escaped8D,
escaped8E,
escaped8F,
escaped90,
escaped91,
escaped92,
escaped93,
escaped94,
escaped95,
escaped96,
escaped97,
escaped98,
escaped99,
escaped9A,
escaped9B,
escaped9C,
escaped9D,
escaped9E,
escaped9F,
}
var escapedQuotationMark []byte = []byte(""") // """ """
var escapedApostrophe []byte = []byte("'") // "'" "'"
var escapedAmpersand []byte = []byte("&") // "&" "&"
var escapedLessThanSign []byte = []byte("<") // "<" "&#x3C"
var escapedGreaterThanSign []byte = []byte(">") // ">" "&#x3E"
// safeRune will only escape the rune 'r' if it is necessary or wise to do so.
// The returned []byte is in UTF-8 — although all the values returned are in the sub-set of Unicode that is also ASCII.
// So it is both valid UTF-8 and ASCII.
func safeRune(r rune) (escapedRune []byte, wasEscaped bool) {
switch r {
case '"':
return escapedQuotationMark, true
case '\'':
return escapedApostrophe, true
case '&':
return escapedAmpersand, true
case '<':
return escapedLessThanSign, true
case '>':
return escapedGreaterThanSign, true
default:
}
if int64(0) <= int64(r) && int64(r) < int64(len(ctrlBlock0)) {
return ctrlBlock0[int(r)], true
}
if minCtrlBlock1 <= int64(r) && int64(r) < (minCtrlBlock1 + int64(len(ctrlBlock1))) {
return ctrlBlock1[int(r) - minCtrlBlock1], true
}
return nil, false
}