diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2016-02-15 16:53:44 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2016-02-15 16:53:44 +0200 |
commit | 394c20fdc5cdf3ae65415218705b6e59b8a7c344 (patch) | |
tree | 817a0b4d3c352c289a643f05c9c209bfbf229f90 /bytesconv.go | |
parent | Issue #52: updated client benchmark results in readme (diff) | |
download | fasthttp-394c20fdc5cdf3ae65415218705b6e59b8a7c344.tar.gz fasthttp-394c20fdc5cdf3ae65415218705b6e59b8a7c344.tar.bz2 fasthttp-394c20fdc5cdf3ae65415218705b6e59b8a7c344.zip |
Added AppendHTMLEscape helper function
Diffstat (limited to 'bytesconv.go')
-rw-r--r-- | bytesconv.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/bytesconv.go b/bytesconv.go index c430a80..6f8931b 100644 --- a/bytesconv.go +++ b/bytesconv.go @@ -12,6 +12,37 @@ import ( "unsafe" ) +// AppendHTMLEscape appends html-escaped s to dst and returns the extended dst. +func AppendHTMLEscape(dst []byte, s string) []byte { + var prev int + var sub string + for i, n := 0, len(s); i < n; i++ { + sub = "" + switch s[i] { + case '<': + sub = "<" + case '>': + sub = ">" + case '"': + sub = """ + case '\'': + sub = "'" + } + if len(sub) > 0 { + dst = append(dst, s[prev:i]...) + dst = append(dst, sub...) + prev = i + 1 + } + } + return append(dst, s[prev:]...) +} + +// AppendHTMLEscapeBytes appends html-escaped s to dst and returns +// the extended dst. +func AppendHTMLEscapeBytes(dst, s []byte) []byte { + return AppendHTMLEscape(dst, unsafeBytesToStr(s)) +} + // AppendIPv4 appends string representation of the given ip v4 to dst // and returns the extended dst. func AppendIPv4(dst []byte, ip net.IP) []byte { |