mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-30 10:45:18 -06:00
Add initial String implementation
This commit is contained in:
@@ -4,7 +4,7 @@ import "io"
|
||||
|
||||
type Blob interface {
|
||||
Value
|
||||
Len() uint64
|
||||
ByteLen() uint64
|
||||
Read() io.Reader
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ func (fb flatBlob) Read() io.Reader {
|
||||
return bytes.NewBuffer(fb.data)
|
||||
}
|
||||
|
||||
func (fb flatBlob) Len() uint64 {
|
||||
func (fb flatBlob) ByteLen() uint64 {
|
||||
return uint64(len(fb.data))
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,12 @@ func TestNewBlobIsFlatBlob(t *testing.T) {
|
||||
assert.IsType(t, flatBlob{}, b)
|
||||
}
|
||||
|
||||
func TestFlatBlobLen(t *testing.T) {
|
||||
func TestFlatBlobByteLen(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
b := NewBlob([]byte{})
|
||||
assert.Equal(uint64(0), b.Len())
|
||||
assert.Equal(uint64(0), b.ByteLen())
|
||||
b = NewBlob([]byte{0x01})
|
||||
assert.Equal(uint64(1), b.Len())
|
||||
assert.Equal(uint64(1), b.ByteLen())
|
||||
}
|
||||
|
||||
func TestFlatBlobEquals(t *testing.T) {
|
||||
|
||||
9
types/flat_string.go
Normal file
9
types/flat_string.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package types
|
||||
|
||||
type flatString struct {
|
||||
flatBlob
|
||||
}
|
||||
|
||||
func (fs flatString) String() string {
|
||||
return string(fs.data)
|
||||
}
|
||||
43
types/flat_string_test.go
Normal file
43
types/flat_string_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewStringIsFlatString(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s := NewString("foo")
|
||||
assert.IsType(s, flatString{})
|
||||
}
|
||||
|
||||
func TestFlatStringLen(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s1 := NewString("foo")
|
||||
s2 := NewString("⌘")
|
||||
assert.Equal(uint64(3), uint64(s1.ByteLen()))
|
||||
assert.Equal(uint64(3), uint64(s2.ByteLen()))
|
||||
}
|
||||
|
||||
func TestFlatStringEquals(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s1 := NewString("foo")
|
||||
s2 := NewString("foo")
|
||||
s3 := s2
|
||||
s4 := NewString("bar")
|
||||
assert.True(s1.Equals(s2))
|
||||
assert.True(s2.Equals(s1))
|
||||
assert.True(s1.Equals(s3))
|
||||
assert.True(s3.Equals(s1))
|
||||
assert.False(s1.Equals(s4))
|
||||
assert.False(s4.Equals(s1))
|
||||
}
|
||||
|
||||
func TestFlatStringString(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
s1 := NewString("")
|
||||
s2 := NewString("foo")
|
||||
assert.Equal("", s1.String())
|
||||
assert.Equal("foo", s2.String())
|
||||
}
|
||||
16
types/string.go
Normal file
16
types/string.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package types
|
||||
|
||||
type String interface {
|
||||
Blob
|
||||
|
||||
// Slurps the entire string into memory. You obviously don't want to do this if the string might be large.
|
||||
String() string
|
||||
}
|
||||
|
||||
func NewString(s string) String {
|
||||
return flatString{flatBlob{[]byte(s)}}
|
||||
}
|
||||
|
||||
func StringFromBytes(b []byte) String {
|
||||
return flatString{flatBlob{b}}
|
||||
}
|
||||
Reference in New Issue
Block a user