1
0
mirror of https://github.com/Oxalide/vsphere-influxdb-go.git synced 2023-10-10 11:36:51 +00:00

add vendoring with go dep

This commit is contained in:
Adrian Todorov
2017-10-25 20:52:40 +00:00
parent 704f4d20d1
commit a59409f16b
1627 changed files with 489673 additions and 0 deletions

View File

@@ -0,0 +1,111 @@
// Package escape contains utilities for escaping parts of InfluxQL
// and InfluxDB line protocol.
package escape // import "github.com/influxdata/influxdb/pkg/escape"
import (
"bytes"
"strings"
)
// Codes is a map of bytes to be escaped.
var Codes = map[byte][]byte{
',': []byte(`\,`),
'"': []byte(`\"`),
' ': []byte(`\ `),
'=': []byte(`\=`),
}
// Bytes escapes characters on the input slice, as defined by Codes.
func Bytes(in []byte) []byte {
for b, esc := range Codes {
in = bytes.Replace(in, []byte{b}, esc, -1)
}
return in
}
const escapeChars = `," =`
// IsEscaped returns whether b has any escaped characters,
// i.e. whether b seems to have been processed by Bytes.
func IsEscaped(b []byte) bool {
for len(b) > 0 {
i := bytes.IndexByte(b, '\\')
if i < 0 {
return false
}
if i+1 < len(b) && strings.IndexByte(escapeChars, b[i+1]) >= 0 {
return true
}
b = b[i+1:]
}
return false
}
// AppendUnescaped appends the unescaped version of src to dst
// and returns the resulting slice.
func AppendUnescaped(dst, src []byte) []byte {
var pos int
for len(src) > 0 {
next := bytes.IndexByte(src[pos:], '\\')
if next < 0 || pos+next+1 >= len(src) {
return append(dst, src...)
}
if pos+next+1 < len(src) && strings.IndexByte(escapeChars, src[pos+next+1]) >= 0 {
if pos+next > 0 {
dst = append(dst, src[:pos+next]...)
}
src = src[pos+next+1:]
pos = 0
} else {
pos += next + 1
}
}
return dst
}
// Unescape returns a new slice containing the unescaped version of in.
func Unescape(in []byte) []byte {
if len(in) == 0 {
return nil
}
if bytes.IndexByte(in, '\\') == -1 {
return in
}
i := 0
inLen := len(in)
var out []byte
for {
if i >= inLen {
break
}
if in[i] == '\\' && i+1 < inLen {
switch in[i+1] {
case ',':
out = append(out, ',')
i += 2
continue
case '"':
out = append(out, '"')
i += 2
continue
case ' ':
out = append(out, ' ')
i += 2
continue
case '=':
out = append(out, '=')
i += 2
continue
}
}
out = append(out, in[i])
i += 1
}
return out
}

View File

@@ -0,0 +1,68 @@
package escape
import (
"bytes"
"reflect"
"strings"
"testing"
)
func TestUnescape(t *testing.T) {
tests := []struct {
in []byte
out []byte
}{
{
[]byte(nil),
[]byte(nil),
},
{
[]byte(""),
[]byte(nil),
},
{
[]byte("\\,\\\"\\ \\="),
[]byte(",\" ="),
},
{
[]byte("\\\\"),
[]byte("\\\\"),
},
{
[]byte("plain and simple"),
[]byte("plain and simple"),
},
}
for ii, tt := range tests {
got := Unescape(tt.in)
if !reflect.DeepEqual(got, tt.out) {
t.Errorf("[%d] Unescape(%#v) = %#v, expected %#v", ii, string(tt.in), string(got), string(tt.out))
}
}
}
func TestAppendUnescaped(t *testing.T) {
cases := strings.Split(strings.TrimSpace(`
normal
inv\alid
goo\"d
sp\ ace
\,\"\ \=
f\\\ x
`), "\n")
for _, c := range cases {
exp := Unescape([]byte(c))
got := AppendUnescaped(nil, []byte(c))
if !bytes.Equal(got, exp) {
t.Errorf("AppendUnescaped failed for %#q: got %#q, exp %#q", c, got, exp)
}
}
}

View File

@@ -0,0 +1,21 @@
package escape
import "strings"
var (
escaper = strings.NewReplacer(`,`, `\,`, `"`, `\"`, ` `, `\ `, `=`, `\=`)
unescaper = strings.NewReplacer(`\,`, `,`, `\"`, `"`, `\ `, ` `, `\=`, `=`)
)
// UnescapeString returns unescaped version of in.
func UnescapeString(in string) string {
if strings.IndexByte(in, '\\') == -1 {
return in
}
return unescaper.Replace(in)
}
// String returns the escaped version of in.
func String(in string) string {
return escaper.Replace(in)
}

View File

@@ -0,0 +1,115 @@
package escape
import (
"testing"
)
var s string
func BenchmarkStringEscapeNoEscapes(b *testing.B) {
for n := 0; n < b.N; n++ {
s = String("no_escapes")
}
}
func BenchmarkStringUnescapeNoEscapes(b *testing.B) {
for n := 0; n < b.N; n++ {
s = UnescapeString("no_escapes")
}
}
func BenchmarkManyStringEscape(b *testing.B) {
tests := []string{
"this is my special string",
"a field w=i th == tons of escapes",
"some,commas,here",
}
for n := 0; n < b.N; n++ {
for _, test := range tests {
s = String(test)
}
}
}
func BenchmarkManyStringUnescape(b *testing.B) {
tests := []string{
`this\ is\ my\ special\ string`,
`a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`,
`some\,commas\,here`,
}
for n := 0; n < b.N; n++ {
for _, test := range tests {
s = UnescapeString(test)
}
}
}
func TestStringEscape(t *testing.T) {
tests := []struct {
in string
expected string
}{
{
in: "",
expected: "",
},
{
in: "this is my special string",
expected: `this\ is\ my\ special\ string`,
},
{
in: "a field w=i th == tons of escapes",
expected: `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`,
},
{
in: "no_escapes",
expected: "no_escapes",
},
{
in: "some,commas,here",
expected: `some\,commas\,here`,
},
}
for _, test := range tests {
if test.expected != String(test.in) {
t.Errorf("Got %s, expected %s", String(test.in), test.expected)
}
}
}
func TestStringUnescape(t *testing.T) {
tests := []struct {
in string
expected string
}{
{
in: "",
expected: "",
},
{
in: `this\ is\ my\ special\ string`,
expected: "this is my special string",
},
{
in: `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`,
expected: "a field w=i th == tons of escapes",
},
{
in: "no_escapes",
expected: "no_escapes",
},
{
in: `some\,commas\,here`,
expected: "some,commas,here",
},
}
for _, test := range tests {
if test.expected != UnescapeString(test.in) {
t.Errorf("Got %s, expected %s", UnescapeString(test.in), test.expected)
}
}
}