mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
utils: avoid importing encoding/xml package
This commit is contained in:
parent
f7ced299d9
commit
d185826bee
@ -1,11 +1,11 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,9 +69,63 @@ func S2B(s string) (b []byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// XmlEscape xml escape string
|
const (
|
||||||
func XmlEscape(c string) string {
|
escQuot = """ // shorter than """
|
||||||
buf := new(strings.Builder)
|
escApos = "'" // shorter than "'"
|
||||||
_ = xml.EscapeText(buf, []byte(c))
|
escAmp = "&"
|
||||||
return buf.String()
|
escLT = "<"
|
||||||
|
escGT = ">"
|
||||||
|
escTab = "	"
|
||||||
|
escNL = "
"
|
||||||
|
escCR = "
"
|
||||||
|
escFFFD = "\uFFFD" // Unicode replacement character
|
||||||
|
)
|
||||||
|
|
||||||
|
func isInCharacterRange(r rune) (inrange bool) {
|
||||||
|
return r == 0x09 ||
|
||||||
|
r == 0x0A ||
|
||||||
|
r == 0x0D ||
|
||||||
|
r >= 0x20 && r <= 0xD7FF ||
|
||||||
|
r >= 0xE000 && r <= 0xFFFD ||
|
||||||
|
r >= 0x10000 && r <= 0x10FFFF
|
||||||
|
}
|
||||||
|
|
||||||
|
// XmlEscape xml escape string
|
||||||
|
func XmlEscape(s string) string {
|
||||||
|
var esc string
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.Grow(len(s))
|
||||||
|
last := 0
|
||||||
|
for i, r := range s {
|
||||||
|
width := utf8.RuneLen(r)
|
||||||
|
switch r {
|
||||||
|
case '"':
|
||||||
|
esc = escQuot
|
||||||
|
case '\'':
|
||||||
|
esc = escApos
|
||||||
|
case '&':
|
||||||
|
esc = escAmp
|
||||||
|
case '<':
|
||||||
|
esc = escLT
|
||||||
|
case '>':
|
||||||
|
esc = escGT
|
||||||
|
case '\t':
|
||||||
|
esc = escTab
|
||||||
|
case '\n':
|
||||||
|
esc = escNL
|
||||||
|
case '\r':
|
||||||
|
esc = escCR
|
||||||
|
default:
|
||||||
|
if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
|
||||||
|
esc = escFFFD
|
||||||
|
break
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sb.WriteString(s[last:i])
|
||||||
|
sb.WriteString(esc)
|
||||||
|
last = i + width
|
||||||
|
}
|
||||||
|
sb.WriteString(s[last:])
|
||||||
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
14
utils/string_test.go
Normal file
14
utils/string_test.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestXmlEscape(t *testing.T) {
|
||||||
|
input := "A \x00 terminated string."
|
||||||
|
expected := "A \uFFFD terminated string."
|
||||||
|
text := XmlEscape(input)
|
||||||
|
if text != expected {
|
||||||
|
t.Errorf("have %v, want %v", text, expected)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user