summaryrefslogtreecommitdiffstats
path: root/modules/base/tool.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/base/tool.go')
-rw-r--r--modules/base/tool.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 97fd87e85c..d99ed2bab4 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -5,6 +5,7 @@
package base
import (
+ "bytes"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
@@ -36,6 +37,9 @@ import (
"github.com/gogits/chardet"
)
+// UTF8BOM is the utf-8 byte-order marker
+var UTF8BOM = []byte{'\xef', '\xbb', '\xbf'}
+
// EncodeMD5 encodes string to md5 hex value.
func EncodeMD5(str string) string {
m := md5.New()
@@ -91,6 +95,14 @@ func DetectEncoding(content []byte) (string, error) {
return result.Charset, err
}
+// RemoveBOMIfPresent removes a UTF-8 BOM from a []byte
+func RemoveBOMIfPresent(content []byte) []byte {
+ if len(content) > 2 && bytes.Equal(content[0:3], UTF8BOM) {
+ return content[3:]
+ }
+ return content
+}
+
// BasicAuthDecode decode basic auth string
func BasicAuthDecode(encoded string) (string, string, error) {
s, err := base64.StdEncoding.DecodeString(encoded)