Skip to content

Commit

Permalink
utils: avoid importing encoding/xml package
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Mar 18, 2022
1 parent f7ced29 commit d185826
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
64 changes: 59 additions & 5 deletions utils/string.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package utils

import (
"encoding/xml"
"math/rand"
"reflect"
"strconv"
"strings"
"unicode/utf8"
"unsafe"
)

Expand Down Expand Up @@ -69,9 +69,63 @@ func S2B(s string) (b []byte) {
return
}

const (
escQuot = """ // shorter than """
escApos = "'" // shorter than "'"
escAmp = "&"
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(c string) string {
buf := new(strings.Builder)
_ = xml.EscapeText(buf, []byte(c))
return buf.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 changes: 14 additions & 0 deletions utils/string_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d185826

Please sign in to comment.