From 3cc7fe967591be890b43f3119cc0a39febdf8a0f Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 22 Nov 2015 13:45:42 +0200 Subject: [PATCH] Moved byte slice manipulation functions to bytesconv.go --- args.go | 55 ---------------------------------------------------- bytesconv.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/args.go b/args.go index 124d5c9..878f7d8 100644 --- a/args.go +++ b/args.go @@ -324,30 +324,6 @@ func peekArgStr(h []argsKV, k string) []byte { return nil } -// EqualBytesStr returns true if string(b) == s. -// -// It doesn't allocate memory unlike string(b) do. -func EqualBytesStr(b []byte, s string) bool { - if len(s) != len(b) { - return false - } - for i, n := 0, len(s); i < n; i++ { - if s[i] != b[i] { - return false - } - } - return true -} - -// AppendBytesStr appends src to dst and returns dst -// (which may be newly allocated). -func AppendBytesStr(dst []byte, src string) []byte { - for i, n := 0, len(src); i < n; i++ { - dst = append(dst, src[i]) - } - return dst -} - type argsScanner struct { b []byte } @@ -420,34 +396,3 @@ func decodeArgAppend(dst, src []byte, decodePlus bool) []byte { } return dst } - -func unhex(c byte) int { - if c >= '0' && c <= '9' { - return int(c - '0') - } - if c >= 'a' && c <= 'f' { - return 10 + int(c-'a') - } - if c >= 'A' && c <= 'F' { - return 10 + int(c-'A') - } - return -1 -} - -func appendQuotedArg(dst, v []byte) []byte { - for _, c := range v { - if c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '/' || c == '.' { - dst = append(dst, c) - } else { - dst = append(dst, '%', hexChar(c>>4), hexChar(c&15)) - } - } - return dst -} - -func hexChar(c byte) byte { - if c < 10 { - return '0' + c - } - return c - 10 + 'A' -} diff --git a/bytesconv.go b/bytesconv.go index 98db95c..b0bc54c 100644 --- a/bytesconv.go +++ b/bytesconv.go @@ -263,3 +263,58 @@ func lowercaseBytes(b []byte) { func unsafeBytesToStr(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } + +func unhex(c byte) int { + if c >= '0' && c <= '9' { + return int(c - '0') + } + if c >= 'a' && c <= 'f' { + return 10 + int(c-'a') + } + if c >= 'A' && c <= 'F' { + return 10 + int(c-'A') + } + return -1 +} + +func appendQuotedArg(dst, v []byte) []byte { + for _, c := range v { + if c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '/' || c == '.' { + dst = append(dst, c) + } else { + dst = append(dst, '%', hexChar(c>>4), hexChar(c&15)) + } + } + return dst +} + +func hexChar(c byte) byte { + if c < 10 { + return '0' + c + } + return c - 10 + 'A' +} + +// EqualBytesStr returns true if string(b) == s. +// +// It doesn't allocate memory unlike string(b) do. +func EqualBytesStr(b []byte, s string) bool { + if len(s) != len(b) { + return false + } + for i, n := 0, len(s); i < n; i++ { + if s[i] != b[i] { + return false + } + } + return true +} + +// AppendBytesStr appends src to dst and returns dst +// (which may be newly allocated). +func AppendBytesStr(dst []byte, src string) []byte { + for i, n := 0, len(src); i < n; i++ { + dst = append(dst, src[i]) + } + return dst +}