revising aliases t

This commit is contained in:
2025-02-08 17:06:30 -01:06
parent 0814e0c67f
commit 509bb7dd71
19 changed files with 270 additions and 327 deletions

View File

@@ -1,19 +1,20 @@
package atag
import (
"realy.lol/kind"
"bytes"
"realy.lol/ints"
"realy.lol/hex"
"realy.lol/ints"
"realy.lol/kind"
)
type T struct {
Kind *kind.T
PubKey by
DTag by
PubKey []byte
DTag []byte
}
func (t T) Marshal(dst by) (b by) {
func (t T) Marshal(dst []byte) (b []byte) {
b = t.Kind.Marshal(dst)
b = append(b, ':')
b = hex.EncAppend(b, t.PubKey)
@@ -22,8 +23,8 @@ func (t T) Marshal(dst by) (b by) {
return
}
func (t *T) Unmarshal(b by) (r by, err er) {
split := bytes.Split(b, by{':'})
func (t *T) Unmarshal(b []byte) (r []byte, err error) {
split := bytes.Split(b, []byte{':'})
if len(split) != 3 {
return
}

View File

@@ -1,32 +1,35 @@
package atag
import (
"testing"
"realy.lol/kind"
"lukechampine.com/frand"
"bytes"
"math"
"testing"
"lukechampine.com/frand"
"realy.lol/ec/schnorr"
"realy.lol/hex"
"realy.lol/kind"
)
func TestT_Marshal_Unmarshal(t *testing.T) {
k := kind.New(frand.Intn(math.MaxUint16))
pk := make(by, schnorr.PubKeyBytesLen)
pk := make([]byte, schnorr.PubKeyBytesLen)
frand.Read(pk)
d := make(by, frand.Intn(10)+3)
d := make([]byte, frand.Intn(10)+3)
frand.Read(d)
var dtag st
var dtag string
dtag = hex.Enc(d)
t1 := &T{
Kind: k,
PubKey: pk,
DTag: by(dtag),
DTag: []byte(dtag),
}
b1 := t1.Marshal(nil)
log.I.F("%s", b1)
t2 := &T{}
var r by
var err er
var r []byte
var err error
if r, err = t2.Unmarshal(b1); chk.E(err) {
t.Fatal(err)
}
@@ -35,7 +38,7 @@ func TestT_Marshal_Unmarshal(t *testing.T) {
t.Fatalf("remainder")
}
b2 := t2.Marshal(nil)
if !equals(b1, b2) {
if !bytes.Equal(b1, b2) {
t.Fatalf("failed to re-marshal back original")
}
}

View File

@@ -1,22 +1,9 @@
package atag
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -21,66 +21,66 @@ const (
MarkerMention = "mention"
)
type BS[Z by | st] by
type BS[Z []byte | string] []byte
// T is a list of strings with a literal ordering.
//
// Not a set, there can be repeating elements.
type T struct {
field []BS[by]
field []BS[[]byte]
}
func (t *T) S(i no) (s st) {
func (t *T) S(i int) (s string) {
if t == nil {
return
}
if t.Len() <= i {
return
}
return st(t.field[i])
return string(t.field[i])
}
func (t *T) B(i no) (b by) {
func (t *T) B(i int) (b []byte) {
if t == nil {
return
}
if t.Len() <= i {
return
}
return by(t.field[i])
return []byte(t.field[i])
}
func (t *T) BS() (bs []by) {
func (t *T) BS() (bs [][]byte) {
if t == nil {
return
}
bs = make([]by, 0, t.Len())
bs = make([][]byte, 0, t.Len())
for _, b := range t.field {
bs = append(bs, b)
}
return
}
func (t *T) F() (b []by) {
func (t *T) F() (b [][]byte) {
if t == nil {
return []by{}
return [][]byte{}
}
b = make([]by, t.Len())
b = make([][]byte, t.Len())
for i := range t.field {
b[i] = t.B(i)
}
return
}
func (t *T) Len() no {
func (t *T) Len() int {
if t == nil {
return 0
}
return len(t.field)
}
func (t *T) Less(i, j no) bo {
var cursor no
func (t *T) Less(i, j int) bool {
var cursor int
for len(t.field[i]) < cursor-1 && len(t.field[j]) < cursor-1 {
if bytes.Compare(t.field[i], t.field[j]) < 0 {
return true
@@ -90,20 +90,20 @@ func (t *T) Less(i, j no) bo {
return false
}
func (t *T) Swap(i, j no) { t.field[i], t.field[j] = t.field[j], t.field[i] }
func (t *T) Swap(i, j int) { t.field[i], t.field[j] = t.field[j], t.field[i] }
func NewWithCap(c no) *T { return &T{make([]BS[by], 0, c)} }
func NewWithCap(c int) *T { return &T{make([]BS[[]byte], 0, c)} }
func New[V st | by](fields ...V) (t *T) {
t = &T{field: make([]BS[by], len(fields))}
func New[V string | []byte](fields ...V) (t *T) {
t = &T{field: make([]BS[[]byte], len(fields))}
for i, field := range fields {
t.field[i] = by(field)
t.field[i] = []byte(field)
}
return
}
func FromBytesSlice(fields ...by) (t *T) {
t = &T{field: make([]BS[by], len(fields))}
func FromBytesSlice(fields ...[]byte) (t *T) {
t = &T{field: make([]BS[[]byte], len(fields))}
for i, field := range fields {
t.field[i] = field
}
@@ -112,17 +112,17 @@ func FromBytesSlice(fields ...by) (t *T) {
// Clone makes a new tag.T with the same members.
func (t *T) Clone() (c *T) {
c = &T{field: make([]BS[by], 0, len(t.field))}
c = &T{field: make([]BS[[]byte], 0, len(t.field))}
for _, f := range t.field {
l := len(f)
b := make(by, l)
b := make([]byte, l)
copy(b, f)
c.field = append(c.field, b)
}
return
}
func (t *T) Append(b ...by) (tt *T) {
func (t *T) Append(b ...[]byte) (tt *T) {
if t == nil {
// we are propagating back this to tt if t was nil, else it appends
// t = &T{make([]BS[B], 0, len(t.field))}
@@ -134,21 +134,21 @@ func (t *T) Append(b ...by) (tt *T) {
return t
}
func (t *T) Cap() no { return cap(t.field) }
func (t *T) Clear() { t.field = t.field[:0] }
func (t *T) Slice(start, end no) *T { return &T{t.field[start:end]} }
func (t *T) Cap() int { return cap(t.field) }
func (t *T) Clear() { t.field = t.field[:0] }
func (t *T) Slice(start, end int) *T { return &T{t.field[start:end]} }
func (t *T) ToByteSlice() (b []by) {
func (t *T) ToByteSlice() (b [][]byte) {
for i := range t.field {
b = append(b, t.field[i])
}
return
}
func (t *T) ToStringSlice() (b []st) {
b = make([]st, 0, len(t.field))
func (t *T) ToStringSlice() (b []string) {
b = make([]string, 0, len(t.field))
for i := range t.field {
b = append(b, st(t.field[i]))
b = append(b, string(t.field[i]))
}
return
}
@@ -157,7 +157,7 @@ func (t *T) ToStringSlice() (b []st) {
//
// The last element is treated specially in that it is considered to match if
// the candidate has the same initial substring as its corresponding element.
func (t *T) StartsWith(prefix *T) bo {
func (t *T) StartsWith(prefix *T) bool {
// log.I.S("StartsWith", prefix)
prefixLen := len(prefix.field)
@@ -166,7 +166,7 @@ func (t *T) StartsWith(prefix *T) bo {
}
// check initial elements for equality
for i := 0; i < prefixLen-1; i++ {
if !equals(prefix.field[i], t.field[i]) {
if !bytes.Equal(prefix.field[i], t.field[i]) {
return false
}
}
@@ -175,7 +175,7 @@ func (t *T) StartsWith(prefix *T) bo {
}
// Key returns the first element of the tags.
func (t *T) Key() by {
func (t *T) Key() []byte {
if t == nil {
return nil
}
@@ -186,7 +186,7 @@ func (t *T) Key() by {
}
// FilterKey returns the first element of a filter tag (the key) with the # removed
func (t *T) FilterKey() by {
func (t *T) FilterKey() []byte {
if t == nil {
return nil
}
@@ -197,7 +197,7 @@ func (t *T) FilterKey() by {
}
// Value returns the second element of the tag.
func (t *T) Value() by {
func (t *T) Value() []byte {
if t == nil {
return nil
}
@@ -207,23 +207,23 @@ func (t *T) Value() by {
return nil
}
var etag, ptag = by("e"), by("p")
var etag, ptag = []byte("e"), []byte("p")
// Relay returns the third element of the tag.
func (t *T) Relay() (s by) {
func (t *T) Relay() (s []byte) {
if t == nil {
return nil
}
if (equals(t.Key(), etag) ||
equals(t.Key(), ptag)) &&
if (bytes.Equal(t.Key(), etag) ||
bytes.Equal(t.Key(), ptag)) &&
len(t.field) >= Relay {
return normalize.URL(by(t.field[Relay]))
return normalize.URL([]byte(t.field[Relay]))
}
return
}
func (t *T) Marshal(dst by) (b by) {
func (t *T) Marshal(dst []byte) (b []byte) {
dst = append(dst, '[')
for i, s := range t.field {
if i > 0 {
@@ -235,9 +235,9 @@ func (t *T) Marshal(dst by) (b by) {
return dst
}
func (t *T) Unmarshal(b by) (r by, err er) {
var inQuotes, openedBracket bo
var quoteStart no
func (t *T) Unmarshal(b []byte) (r []byte, err error) {
var inQuotes, openedBracket bool
var quoteStart int
// t.Field = []BS[B]{}
for i := 0; i < len(b); i++ {
if !openedBracket && b[i] == '[' {
@@ -264,9 +264,9 @@ func (t *T) Unmarshal(b by) (r by, err er) {
}
// Contains returns true if the provided element is found in the tag slice.
func (t *T) Contains(s by) (b bo) {
func (t *T) Contains(s []byte) (b bool) {
for i := range t.field {
if equals(t.field[i], s) {
if bytes.Equal(t.field[i], s) {
return true
}
}
@@ -274,12 +274,12 @@ func (t *T) Contains(s by) (b bo) {
}
// Equal checks that the provided tag list matches.
func (t *T) Equal(ta *T) bo {
func (t *T) Equal(ta *T) bool {
if len(t.field) != len(ta.field) {
return false
}
for i := range t.field {
if !equals(t.field[i], ta.field[i]) {
if !bytes.Equal(t.field[i], ta.field[i]) {
return false
}
}

View File

@@ -1,25 +1,26 @@
package tag
import (
"bytes"
"testing"
"lukechampine.com/frand"
)
func TestMarshalUnmarshal(t *testing.T) {
var b, bo, bc by
var b, bool, bc []byte
for _ = range 100 {
n := frand.Intn(8)
tg := NewWithCap(n)
for _ = range n {
b1 := make(by, frand.Intn(8))
b1 := make([]byte, frand.Intn(8))
_, _ = frand.Read(b1)
tg.field = append(tg.field, b1)
}
// log.I.S(tg)
b = tg.Marshal(b)
bo = make(by, len(b))
copy(bo, b)
bool = make([]byte, len(b))
copy(bool, b)
tg2 := NewWithCap(n)
rem, err := tg2.Unmarshal(b)
// log.I.S(tg2)
@@ -28,8 +29,8 @@ func TestMarshalUnmarshal(t *testing.T) {
}
bc = tg2.Marshal(bc)
// log.I.F("\n\norig\n%s\n\ncopy\n%s\n", bo, bc)
if !equals(bo, bc) {
t.Fatalf("got\n%s\nwant\n%s", bo, bc)
if !bytes.Equal(bool, bc) {
t.Fatalf("got\n%s\nwant\n%s", bool, bc)
}
if len(rem) != 0 {
t.Fatalf("len(rem)!=0:\n%s", rem)
@@ -37,34 +38,34 @@ func TestMarshalUnmarshal(t *testing.T) {
if !tg.Equal(tg2) {
t.Fatalf("got\n%s\nwant\n%s", tg2, tg)
}
b, bo, bc = b[:0], bo[:0], bc[:0]
b, bool, bc = b[:0], bool[:0], bc[:0]
}
}
func TestMarshalUnmarshalZeroLengthTag(t *testing.T) {
empty := by("[\"a\"]")
var b by
empty := []byte("[\"a\"]")
var b []byte
tg := &T{}
b, _ = tg.Unmarshal(empty)
b = tg.Marshal(b)
if !equals(empty, b) {
if !bytes.Equal(empty, b) {
t.Fatalf("got\n%s\nwant\n%s", b, empty)
}
empty = by("[]")
empty = []byte("[]")
tg = &T{}
b, _ = tg.Unmarshal(empty)
b = tg.Marshal(b)
if !equals(empty, b) {
if !bytes.Equal(empty, b) {
t.Fatalf("got\n%s\nwant\n%s", b, empty)
}
}
func BenchmarkMarshalUnmarshal(bb *testing.B) {
b := make(by, 0, 40000000)
b := make([]byte, 0, 40000000)
n := 4096
tg := NewWithCap(n)
for _ = range n {
b1 := make(by, 128)
b1 := make([]byte, 128)
_, _ = frand.Read(b1)
tg.field = append(tg.field, b1)
}
@@ -93,7 +94,7 @@ func TestT_Clone_Equal(t *testing.T) {
n := frand.Intn(64) + 2
t1 := NewWithCap(n)
for _ = range n {
f := make(by, frand.Intn(128)+2)
f := make([]byte, frand.Intn(128)+2)
_, _ = frand.Read(f)
t1.field = append(t1.field, f)
}

View File

@@ -1,22 +1,9 @@
package tag
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -27,28 +27,28 @@ func New(fields ...*tag.T) (t *T) {
return
}
func NewWithCap(c no) (t *T) {
func NewWithCap(c int) (t *T) {
return &T{t: make([]*tag.T, 0, c)}
}
func (t *T) F() (tt []*tag.T) {
if t == nil {
return []*tag.T{tag.New(by{})}
return []*tag.T{tag.New([]byte{})}
}
return t.t
}
func (t *T) N(i no) (tt *tag.T) {
func (t *T) N(i int) (tt *tag.T) {
if t == nil {
return tag.NewWithCap(0)
}
if len(t.t) <= i {
return tag.New(by{})
return tag.New([]byte{})
}
return t.t[i]
}
func (t *T) AppendTo(n no, b ...by) (tt *T) {
func (t *T) AppendTo(n int, b ...[]byte) (tt *T) {
if t == nil {
log.E.S(t, b)
return
@@ -69,12 +69,12 @@ func (t *T) AppendTo(n no, b ...by) (tt *T) {
// AppendSlice just appends a slice of slices of bytes into the tags. Like AppendTo
// but without the position specifier. todo: this is a terribly constructed API innit.
func (t *T) AppendSlice(b ...by) (tt *T) {
func (t *T) AppendSlice(b ...[]byte) (tt *T) {
t.t = append(t.t, tag.New(b...))
return
}
func (t *T) AddCap(i, c no) (tt *T) {
func (t *T) AddCap(i, c int) (tt *T) {
if t == nil {
log.E.F("cannot add capacity to index %d of nil tags", i)
fmt.Fprint(os.Stderr, lol.GetNLoc(7))
@@ -95,8 +95,8 @@ func (t *T) Value() (tt []*tag.T) {
return t.t
}
func (t *T) ToStringSlice() (b [][]st) {
b = make([][]st, 0, len(t.t))
func (t *T) ToStringSlice() (b [][]string) {
b = make([][]string, 0, len(t.t))
for i := range t.t {
b = append(b, t.t[i].ToStringSlice())
}
@@ -111,7 +111,7 @@ func (t *T) Clone() (c *T) {
return
}
func (t *T) Equal(ta *T) bo {
func (t *T) Equal(ta *T) bool {
// sort them the same so if they are the same in content they compare the same.
t1 := t.Clone()
sort.Sort(t1)
@@ -126,7 +126,7 @@ func (t *T) Equal(ta *T) bo {
}
// Less returns which tag's first element is first lexicographically
func (t *T) Less(i, j no) (less bo) {
func (t *T) Less(i, j int) (less bool) {
a, b := t.t[i], t.t[j]
if a.Len() < 1 && b.Len() < 1 {
return false // they are equal
@@ -140,11 +140,11 @@ func (t *T) Less(i, j no) (less bo) {
return
}
func (t *T) Swap(i, j no) {
func (t *T) Swap(i, j int) {
t.t[i], t.t[j] = t.t[j], t.t[i]
}
func (t *T) Len() (l no) {
func (t *T) Len() (l int) {
if t == nil {
return
}
@@ -188,7 +188,7 @@ func (t *T) GetAll(tagPrefix *tag.T) *T {
}
// FilterOut removes all tags that match the prefix, see [T.StartsWith]
func (t *T) FilterOut(tagPrefix []by) *T {
func (t *T) FilterOut(tagPrefix [][]byte) *T {
filtered := &T{t: make([]*tag.T, 0, len(t.t))}
for _, v := range t.t {
if !v.StartsWith(tag.New(tagPrefix...)) {
@@ -236,13 +236,13 @@ func (t *T) AppendTags(ttt ...*tag.T) (tt *T) {
// this method is invoked.
//
// todo: wut is this?
func (t *T) Scan(src any) (err er) {
var jtags by
func (t *T) Scan(src any) (err error) {
var jtags []byte
switch v := src.(type) {
case by:
case []byte:
jtags = v
case st:
jtags = by(v)
case string:
jtags = []byte(v)
default:
return errors.New("couldn't scan tag, it's not a json string")
}
@@ -254,7 +254,7 @@ func (t *T) Scan(src any) (err er) {
// Intersects returns true if a filter tags.T has a match. This means the second character of
// the filter tag key matches, (ignoring the stupid # prefix in the filter) and one of the
// following values in the tag matches the first tag of this tag.
func (t *T) Intersects(f *T) (has bo) {
func (t *T) Intersects(f *T) (has bool) {
if t == nil || f == nil {
// if either are empty there can't be a match (if caller wants to know if both are empty
// that's not the same as an intersection).
@@ -263,11 +263,11 @@ func (t *T) Intersects(f *T) (has bo) {
matches := len(f.t)
for _, v := range f.t {
for _, w := range t.t {
if equals(v.FilterKey(), w.Key()) {
if bytes.Equal(v.FilterKey(), w.Key()) {
// we have a matching tag key, and both have a first field, check if tag has any
// of the subsequent values in the filter tag.
for _, val := range v.F()[1:] {
if equals(val, w.Value()) {
if bytes.Equal(val, w.Value()) {
matches--
}
}
@@ -279,9 +279,9 @@ func (t *T) Intersects(f *T) (has bo) {
// ContainsProtectedMarker returns true if an event may only be published to the relay by a user
// authed with the same pubkey as in the event. This is for implementing relayinfo.NIP70.
func (t *T) ContainsProtectedMarker() (does bo) {
func (t *T) ContainsProtectedMarker() (does bool) {
for _, v := range t.t {
if equals(v.Key(), by("-")) {
if bytes.Equal(v.Key(), []byte("-")) {
return true
}
}
@@ -290,7 +290,7 @@ func (t *T) ContainsProtectedMarker() (does bo) {
// ContainsAny returns true if any of the strings given in `values` matches any of the tag
// elements.
func (t *T) ContainsAny(tagName by, values *tag.T) bo {
func (t *T) ContainsAny(tagName []byte, values *tag.T) bool {
if len(tagName) < 1 {
return false
}
@@ -303,11 +303,11 @@ func (t *T) ContainsAny(tagName by, values *tag.T) bo {
if v.Len() < 2 {
continue
}
if !equals(v.Key(), tagName) {
if !bytes.Equal(v.Key(), tagName) {
continue
}
for _, candidate := range values.F() {
if equals(v.Value(), candidate) {
if bytes.Equal(v.Value(), candidate) {
return true
}
}
@@ -315,7 +315,7 @@ func (t *T) ContainsAny(tagName by, values *tag.T) bo {
return false
}
func (t *T) Contains(filterTags *T) (has bo) {
func (t *T) Contains(filterTags *T) (has bool) {
for _, v := range filterTags.t {
if t.ContainsAny(v.FilterKey(), v) {
return true
@@ -325,7 +325,7 @@ func (t *T) Contains(filterTags *T) (has bo) {
}
// MarshalTo appends the JSON encoded byte of T as [][]string to dst. String escaping is as described in RFC8259.
func (t *T) MarshalTo(dst by) by {
func (t *T) MarshalTo(dst []byte) []byte {
dst = append(dst, '[')
for i, tt := range t.t {
if i > 0 {
@@ -351,7 +351,7 @@ func (t *T) MarshalTo(dst by) by {
// return buf.String()
// }
func (t *T) Marshal(dst by) (b by) {
func (t *T) Marshal(dst []byte) (b []byte) {
b = dst
b = append(b, '[')
if t == nil || t.t == nil {
@@ -371,7 +371,7 @@ func (t *T) Marshal(dst by) (b by) {
return
}
func (t *T) Unmarshal(b by) (r by, err er) {
func (t *T) Unmarshal(b []byte) (r []byte, err error) {
r = b[:]
for len(r) > 0 {
switch r[0] {

View File

@@ -1,6 +1,7 @@
package tags
import (
"bytes"
"testing"
"lukechampine.com/frand"
@@ -10,8 +11,8 @@ import (
)
func TestMarshalUnmarshal(t *testing.T) {
var b, rem by
var err er
var b, rem []byte
var err error
for _ = range 100000 {
n := frand.Intn(3)
tgs := &T{}
@@ -19,23 +20,23 @@ func TestMarshalUnmarshal(t *testing.T) {
n1 := frand.Intn(5)
tg := tag.NewWithCap(n)
for _ = range n1 {
b1 := make(by, frand.Intn(40)+2)
b1 := make([]byte, frand.Intn(40)+2)
_, _ = frand.Read(b1)
tg = tg.Append(b1)
}
tgs.t = append(tgs.t, tg)
}
b = tgs.Marshal(b)
bb := make(by, len(b))
bb := make([]byte, len(b))
copy(bb, b)
ta := &T{}
rem, err = ta.Unmarshal(b)
if chk.E(err) {
t.Fatal(err)
}
var bc by
var bc []byte
bc = ta.Marshal(bc)
if !equals(bb, bc) {
if !bytes.Equal(bb, bc) {
t.Fatalf("got\n%s\nwant\n%s\n", bc, bb)
}
b, rem, bb, bc = b[:0], rem[:0], bb[:0], bc[:0]
@@ -43,13 +44,13 @@ func TestMarshalUnmarshal(t *testing.T) {
}
func TestEmpty(t *testing.T) {
var err er
var b0, bc, b1 by
var err error
var b0, bc, b1 []byte
var empty, empty1, empty2 *T
_, _, _ = empty, empty1, empty2
empty = New()
b0 = empty.Marshal(b0)
bc = make(by, len(b0))
bc = make([]byte, len(b0))
copy(bc, b0)
empty1 = New()
if b0, err = empty1.Unmarshal(b0); chk.E(err) {
@@ -57,13 +58,13 @@ func TestEmpty(t *testing.T) {
}
empty2 = New()
b1 = empty2.Marshal(b1)
if !equals(bc, b1) {
t.Fatalf("'%s' == '%s' -> %v", bc, b1, equals(bc, b1))
if !bytes.Equal(bc, b1) {
t.Fatalf("'%s' == '%s' -> %v", bc, b1, bytes.Equal(bc, b1))
}
b0, bc, b1 = b0[:0], bc[:0], b1[:0]
empty = New(&tag.T{})
b0 = empty.Marshal(b0)
bc = make(by, len(b0))
bc = make([]byte, len(b0))
copy(bc, b0)
empty1 = New()
if b0, err = empty1.Unmarshal(b0); chk.E(err) {
@@ -71,15 +72,15 @@ func TestEmpty(t *testing.T) {
}
empty2 = New()
b1 = empty1.Marshal(b1)
if !equals(bc, b1) {
t.Fatalf("'%s' == '%s' -> %v", bc, b1, equals(bc, b1))
if !bytes.Equal(bc, b1) {
t.Fatalf("'%s' == '%s' -> %v", bc, b1, bytes.Equal(bc, b1))
}
b0, bc, b1 = b0[:0], bc[:0], b1[:0]
}
func BenchmarkMarshalUnmarshal(bb *testing.B) {
var b, rem by
var err er
var b, rem []byte
var err error
bb.Run("tag.Marshal", func(bb *testing.B) {
bb.ReportAllocs()
for i := 0; i < bb.N; i++ {
@@ -89,7 +90,7 @@ func BenchmarkMarshalUnmarshal(bb *testing.B) {
n1 := frand.Intn(40) + 2
tg := tag.NewWithCap(n)
for _ = range n1 {
b1 := make(by, frand.Intn(40)+2)
b1 := make([]byte, frand.Intn(40)+2)
_, _ = frand.Read(b1)
tg = tg.Append(b1)
// tg.Field = append(tg.Field, b1)
@@ -109,7 +110,7 @@ func BenchmarkMarshalUnmarshal(bb *testing.B) {
n1 := frand.Intn(40) + 2
tg := tag.NewWithCap(n)
for _ = range n1 {
b1 := make(by, frand.Intn(40)+2)
b1 := make([]byte, frand.Intn(40)+2)
_, _ = frand.Read(b1)
tg = tg.Append(b1)
}
@@ -139,7 +140,7 @@ func TestT_Clone_Equal(t *testing.T) {
n1 := frand.Intn(40) + 2
tg := tag.NewWithCap(n)
for _ = range n1 {
b1 := make(by, frand.Intn(40)+2)
b1 := make([]byte, frand.Intn(40)+2)
_, _ = frand.Read(b1)
tg = tg.Append(b1)
}
@@ -174,7 +175,7 @@ func TestTagHelpers(t *testing.T) {
if tags.GetFirst(tag.New("p", "abcdef", "")) == nil {
t.Error("failed to get with existing prefix (blank last string)")
}
if st(tags.GetLast(tag.New("e")).S(1)) != "ffffff" {
if string(tags.GetLast(tag.New("e")).S(1)) != "ffffff" {
t.Error("failed to get last")
}
if tags.GetAll(tag.New("e", "")).Len() != 2 {
@@ -186,25 +187,25 @@ func TestTagHelpers(t *testing.T) {
if tags.AppendUnique(tag.New("e", "bbbbbb")).Len() != 6 {
t.Error("append unique failed to append when didn't exist")
}
if st(tags.AppendUnique(tag.New("e", "eeeeee")).N(4).S(1)) != "ffffff" {
if string(tags.AppendUnique(tag.New("e", "eeeeee")).N(4).S(1)) != "ffffff" {
t.Error("append unique changed the order")
}
if st(tags.AppendUnique(tag.New("e", "eeeeee")).N(3).S(1)) != "eeeeee" {
if string(tags.AppendUnique(tag.New("e", "eeeeee")).N(3).S(1)) != "eeeeee" {
t.Error("append unique changed the order")
}
}
func TestT_ContainsAny(t *testing.T) {
var v, a, b, c by
var err er
var v, a, b, c []byte
var err error
v, err = hex.Dec("4c800257a588a82849d049817c2bdaad984b25a45ad9f6dad66e47d3b47e3b2f")
a, err = hex.Dec("3c800257a588a82849d049817c2bdaad984b25a45ad9f6dad66e47d3b47e3b2f")
b, err = hex.Dec("2c800257a588a82849d049817c2bdaad984b25a45ad9f6dad66e47d3b47e3b2f")
c, err = hex.Dec("1c800257a588a82849d049817c2bdaad984b25a45ad9f6dad66e47d3b47e3b2f")
w := tag.New(by{'b'}, v, a, b, c)
x := tag.New(by{'b'}, c, b, a)
y := tag.New(by{'b'}, b, a, c)
z := tag.New(by{'b'}, v)
w := tag.New([]byte{'b'}, v, a, b, c)
x := tag.New([]byte{'b'}, c, b, a)
y := tag.New([]byte{'b'}, b, a, c)
z := tag.New([]byte{'b'}, v)
tt := New(w, x, y)
ttt := New(x, y)
_, _, _, _, _ = v, err, z, tt, ttt

View File

@@ -1,22 +1,9 @@
package tags
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -11,12 +11,12 @@ import (
"realy.lol/timestamp"
)
func GenerateEvent(maxSize no) (ev *event.T, binSize no, err er) {
func GenerateEvent(maxSize int) (ev *event.T, binSize int, err error) {
l := frand.Intn(maxSize * 6 / 8) // account for base64 expansion
ev = &event.T{
Kind: kind.TextNote,
CreatedAt: timestamp.Now(),
Content: by(base64.StdEncoding.EncodeToString(frand.Bytes(l))),
Content: []byte(base64.StdEncoding.EncodeToString(frand.Bytes(l))),
}
signer := new(p256k.Signer)
if err = signer.Generate(); chk.E(err) {
@@ -25,7 +25,7 @@ func GenerateEvent(maxSize no) (ev *event.T, binSize no, err er) {
if err = ev.Sign(signer); chk.E(err) {
return
}
var bin by
var bin []byte
bin = ev.Marshal(bin)
binSize = len(bin)
return

View File

@@ -1,22 +1,9 @@
package tests
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -5,11 +5,11 @@ import (
)
// AppendBinary is a straight append with length prefix.
func AppendBinary(dst, src by) (b by) {
func AppendBinary(dst, src []byte) (b []byte) {
// if an allocation or two may occur, do it all in one immediately.
minLen := len(src) + len(dst) + binary.MaxVarintLen32
if cap(dst) < minLen {
tmp := make(by, 0, minLen)
tmp := make([]byte, 0, minLen)
dst = append(tmp, dst...)
}
dst = binary.AppendUvarint(dst, uint64(len(src)))
@@ -20,18 +20,18 @@ func AppendBinary(dst, src by) (b by) {
// ExtractBinary decodes the data based on the length prefix and returns a the the
// remaining data from the provided slice.
func ExtractBinary(b by) (str, rem by, err er) {
func ExtractBinary(b []byte) (str, rem []byte, err error) {
l, read := binary.Uvarint(b)
if read < 1 {
err = errorf.E("failed to read uvarint length prefix")
return
}
if len(b) < no(l)+read {
if len(b) < int(l)+read {
err = errorf.E("insufficient data in buffer, require %d have %d",
no(l)+read, len(b))
int(l)+read, len(b))
return
}
str = b[read : read+no(l)]
rem = b[read+no(l):]
str = b[read : read+int(l)]
rem = b[read+int(l):]
return
}

View File

@@ -20,7 +20,7 @@ package text
// - A form feed, 0x0C, as \f
//
// UTF-8 should be used for encoding.
func NostrEscape(dst, src by) by {
func NostrEscape(dst, src []byte) []byte {
l := len(src)
for i := 0; i < l; i++ {
c := src[i]
@@ -55,8 +55,8 @@ func NostrEscape(dst, src by) by {
// appending it to the provided slice, it rewrites it, eliminating a memory
// copy. Keep in mind that the original JSON will be mangled by this operation,
// but the resultant slices will cost zero allocations.
func NostrUnescape(dst by) (b by) {
var r, w no
func NostrUnescape(dst []byte) (b []byte) {
var r, w int
for ; r < len(dst); r++ {
if dst[r] == '\\' {
r++

View File

@@ -9,7 +9,7 @@ import (
)
func TestUnescapeByteString(t *testing.T) {
b := make(by, 256)
b := make([]byte, 256)
for i := range b {
b[i] = byte(i)
}
@@ -22,11 +22,11 @@ func TestUnescapeByteString(t *testing.T) {
}
}
func GenRandString(l no, src *frand.RNG) (str by) {
func GenRandString(l int, src *frand.RNG) (str []byte) {
return src.Bytes(l)
}
var seed = sha256.Sum256(by(`
var seed = sha256.Sum256([]byte(`
The tao that can be told
is not the eternal Tao
The name that can be named
@@ -55,13 +55,13 @@ func TestRandomEscapeByteString(t *testing.T) {
for i := 0; i < 1000; i++ {
l := src.Intn(1<<8) + 32
s1 := GenRandString(l, src)
s2 := make(by, l)
orig := make(by, l)
s2 := make([]byte, l)
orig := make([]byte, l)
copy(s2, s1)
copy(orig, s1)
// first we are checking our implementation comports to the one from go-nostr.
escapeStringVersion := NostrEscape(by{}, s1)
escapeStringVersion := NostrEscape([]byte{}, s1)
escapeJSONStringAndWrapVersion := NostrEscape(nil, s2)
if len(escapeJSONStringAndWrapVersion) != len(escapeStringVersion) {
t.Logf("escapeString\nlength: %d\n%s\n%v\n",
@@ -117,8 +117,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
const size = 65536
b.Run("frand64k", func(b *testing.B) {
b.ReportAllocs()
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -127,9 +127,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
})
b.Run("NostrEscape64k", func(b *testing.B) {
b.ReportAllocs()
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -140,9 +140,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
})
b.Run("NostrEscapeNostrUnescape64k", func(b *testing.B) {
b.ReportAllocs()
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -156,8 +156,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("frand32k", func(b *testing.B) {
b.ReportAllocs()
size := size / 2
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -167,9 +167,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscape32k", func(b *testing.B) {
b.ReportAllocs()
size := size / 2
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -181,9 +181,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscapeNostrUnescape32k", func(b *testing.B) {
b.ReportAllocs()
size := size / 2
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -197,8 +197,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("frand16k", func(b *testing.B) {
b.ReportAllocs()
size := size / 4
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -208,9 +208,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscape16k", func(b *testing.B) {
b.ReportAllocs()
size := size / 4
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -222,9 +222,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscapeNostrUnescape16k", func(b *testing.B) {
b.ReportAllocs()
size := size / 4
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -238,8 +238,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("frand8k", func(b *testing.B) {
b.ReportAllocs()
size := size / 8
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -249,9 +249,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscape8k", func(b *testing.B) {
b.ReportAllocs()
size := size / 8
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -263,9 +263,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscapeNostrUnescape8k", func(b *testing.B) {
b.ReportAllocs()
size := size / 8
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -279,8 +279,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("frand4k", func(b *testing.B) {
b.ReportAllocs()
size := size / 16
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -290,9 +290,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscape4k", func(b *testing.B) {
b.ReportAllocs()
size := size / 16
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -304,9 +304,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscapeNostrUnescape4k", func(b *testing.B) {
b.ReportAllocs()
size := size / 16
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -320,8 +320,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("frand2k", func(b *testing.B) {
b.ReportAllocs()
size := size / 32
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -331,9 +331,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscape2k", func(b *testing.B) {
b.ReportAllocs()
size := size / 32
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -345,9 +345,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscapeNostrUnescape2k", func(b *testing.B) {
b.ReportAllocs()
size := size / 32
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -361,8 +361,8 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("frand1k", func(b *testing.B) {
b.ReportAllocs()
size := size / 64
in := make(by, size)
var err er
in := make([]byte, size)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -372,9 +372,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscape1k", func(b *testing.B) {
b.ReportAllocs()
size := size / 64
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)
@@ -386,9 +386,9 @@ func BenchmarkNostrEscapeNostrUnescape(b *testing.B) {
b.Run("NostrEscapeNostrUnescape1k", func(b *testing.B) {
b.ReportAllocs()
size := size / 64
in := make(by, size)
out := make(by, size*2)
var err er
in := make([]byte, size)
out := make([]byte, size*2)
var err error
for i := 0; i < b.N; i++ {
if _, err = frand.Read(in); chk.E(err) {
b.Fatal(err)

View File

@@ -1,6 +1,7 @@
package text
import (
"bytes"
"io"
"github.com/templexxx/xhex"
@@ -13,7 +14,7 @@ import (
// JSONKey generates the JSON format for an object key and terminates with the
// semicolon.
func JSONKey(dst, k by) (b by) {
func JSONKey(dst, k []byte) (b []byte) {
dst = append(dst, '"')
dst = append(dst, k...)
dst = append(dst, '"', ':')
@@ -25,10 +26,10 @@ func JSONKey(dst, k by) (b by) {
// encoded value, decodes it in-place using a SIMD hex codec and returns the
// decoded truncated bytes (the other half will be as it was but no allocation
// is required).
func UnmarshalHex(b by) (h by, rem by, err er) {
func UnmarshalHex(b []byte) (h []byte, rem []byte, err error) {
rem = b[:]
var inQuote bo
var start no
var inQuote bool
var start int
for i := 0; i < len(b); i++ {
if !inQuote {
if b[i] == '"' {
@@ -58,7 +59,7 @@ func UnmarshalHex(b by) (h by, rem by, err er) {
}
// UnmarshalQuoted performs an in-place unquoting of NIP-01 quoted byte string.
func UnmarshalQuoted(b by) (content, rem by, err er) {
func UnmarshalQuoted(b []byte) (content, rem []byte, err error) {
if len(b) == 0 {
err = io.EOF
return
@@ -76,8 +77,8 @@ func UnmarshalQuoted(b by) (content, rem by, err er) {
err = io.EOF
return
}
var escaping bo
var contentLen no
var escaping bool
var contentLen int
for len(rem) > 0 {
if rem[0] == '\\' {
if !escaping {
@@ -117,7 +118,7 @@ func UnmarshalQuoted(b by) (content, rem by, err er) {
return
}
func MarshalHexArray(dst by, ha []by) (b by) {
func MarshalHexArray(dst []byte, ha [][]byte) (b []byte) {
dst = append(dst, '[')
for i := range ha {
dst = AppendQuote(dst, ha[i], hex.EncAppend)
@@ -132,9 +133,9 @@ func MarshalHexArray(dst by, ha []by) (b by) {
// UnmarshalHexArray unpacks a JSON array containing strings with hexadecimal,
// and checks all values have the specified byte size..
func UnmarshalHexArray(b by, size no) (t []by, rem by, err er) {
func UnmarshalHexArray(b []byte, size int) (t [][]byte, rem []byte, err error) {
rem = b
var openBracket bo
var openBracket bool
for ; len(rem) > 0; rem = rem[1:] {
if rem[0] == '[' {
openBracket = true
@@ -145,7 +146,7 @@ func UnmarshalHexArray(b by, size no) (t []by, rem by, err er) {
rem = rem[1:]
return
} else if rem[0] == '"' {
var h by
var h []byte
if h, rem, err = UnmarshalHex(rem); chk.E(err) {
return
}
@@ -167,9 +168,9 @@ func UnmarshalHexArray(b by, size no) (t []by, rem by, err er) {
}
// UnmarshalStringArray unpacks a JSON array containing strings.
func UnmarshalStringArray(b by) (t []by, rem by, err er) {
func UnmarshalStringArray(b []byte) (t [][]byte, rem []byte, err error) {
rem = b
var openBracket bo
var openBracket bool
for ; len(rem) > 0; rem = rem[1:] {
if rem[0] == '[' {
openBracket = true
@@ -180,7 +181,7 @@ func UnmarshalStringArray(b by) (t []by, rem by, err er) {
rem = rem[1:]
return
} else if rem[0] == '"' {
var h by
var h []byte
if h, rem, err = UnmarshalQuoted(rem); chk.E(err) {
return
}
@@ -196,7 +197,7 @@ func UnmarshalStringArray(b by) (t []by, rem by, err er) {
return
}
func MarshalKindsArray(dst by, ka *kinds.T) (b by) {
func MarshalKindsArray(dst []byte, ka *kinds.T) (b []byte) {
dst = append(dst, '[')
for i := range ka.K {
dst = ka.K[i].Marshal(dst)
@@ -209,10 +210,10 @@ func MarshalKindsArray(dst by, ka *kinds.T) (b by) {
return
}
func UnmarshalKindsArray(b by) (k *kinds.T, rem by, err er) {
func UnmarshalKindsArray(b []byte) (k *kinds.T, rem []byte, err error) {
rem = b
k = &kinds.T{}
var openedBracket bo
var openedBracket bool
for ; len(rem) > 0; rem = rem[1:] {
if !openedBracket && rem[0] == '[' {
openedBracket = true
@@ -243,17 +244,17 @@ func UnmarshalKindsArray(b by) (k *kinds.T, rem by, err er) {
return
}
func True() by { return by("true") }
func False() by { return by("false") }
func True() []byte { return []byte("true") }
func False() []byte { return []byte("false") }
func MarshalBool(src by, truth bo) by {
func MarshalBool(src []byte, truth bool) []byte {
if truth {
return append(src, True()...)
}
return append(src, False()...)
}
func UnmarshalBool(src by) (rem by, truth bo, err er) {
func UnmarshalBool(src []byte) (rem []byte, truth bool, err error) {
rem = src
t, f := True(), False()
for i := range rem {
@@ -262,7 +263,7 @@ func UnmarshalBool(src by) (rem by, truth bo, err er) {
err = io.EOF
return
}
if equals(t, rem[i:i+len(t)]) {
if bytes.Equal(t, rem[i:i+len(t)]) {
truth = true
rem = rem[i+len(t):]
return
@@ -273,7 +274,7 @@ func UnmarshalBool(src by) (rem by, truth bo, err er) {
err = io.EOF
return
}
if equals(f, rem[i:i+len(f)]) {
if bytes.Equal(f, rem[i:i+len(f)]) {
rem = rem[i+len(f):]
return
}
@@ -284,7 +285,7 @@ func UnmarshalBool(src by) (rem by, truth bo, err er) {
return
}
func Comma(b by) (rem by, err er) {
func Comma(b []byte) (rem []byte, err error) {
rem = b
for i := range rem {
if rem[i] == ',' {

View File

@@ -1,6 +1,7 @@
package text
import (
"bytes"
"testing"
"lukechampine.com/frand"
@@ -10,10 +11,10 @@ import (
)
func TestUnmarshalHexArray(t *testing.T) {
var ha []by
h := make(by, sha256.Size)
var ha [][]byte
h := make([]byte, sha256.Size)
frand.Read(h)
var dst by
var dst []byte
for _ = range 20 {
hh := sha256.Sum256(h)
h = hh[:]
@@ -27,9 +28,9 @@ func TestUnmarshalHexArray(t *testing.T) {
}
}
dst = append(dst, ']')
var ha2 []by
var rem by
var err er
var ha2 [][]byte
var rem []byte
var err error
if ha2, rem, err = UnmarshalHexArray(dst, 32); chk.E(err) {
t.Fatal(err)
}
@@ -41,7 +42,7 @@ func TestUnmarshalHexArray(t *testing.T) {
t.Fatalf("failed to unmarshal, remnant afterwards '%s'", rem)
}
for i := range ha2 {
if !equals(ha[i], ha2[i]) {
if !bytes.Equal(ha[i], ha2[i]) {
t.Fatalf("failed to unmarshal at element %d; got %x, expected %x",
i, ha[i], ha2[i])
}

View File

@@ -4,7 +4,7 @@ import (
"realy.lol/hex"
)
func AppendHexFromBinary(dst, src by, quote bo) (b by) {
func AppendHexFromBinary(dst, src []byte, quote bool) (b []byte) {
if quote {
dst = AppendQuote(dst, src, hex.EncAppend)
} else {
@@ -14,8 +14,8 @@ func AppendHexFromBinary(dst, src by, quote bo) (b by) {
return
}
func AppendBinaryFromHex(dst, src by, unquote bo) (b by,
err er) {
func AppendBinaryFromHex(dst, src []byte, unquote bool) (b []byte,
err error) {
if unquote {
if dst, err = hex.DecAppend(dst,
Unquote(src)); chk.E(err) {

View File

@@ -1,22 +1,9 @@
package text
import (
"bytes"
"realy.lol/context"
"realy.lol/lol"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)

View File

@@ -1,59 +1,59 @@
package text
type AppendBytesClosure func(dst, src by) by
type AppendBytesClosure func(dst, src []byte) []byte
type AppendClosure func(dst by) by
type AppendClosure func(dst []byte) []byte
func Unquote(b by) by { return b[1 : len(b)-1] }
func Unquote(b []byte) []byte { return b[1 : len(b)-1] }
func Noop(dst, src by) by { return append(dst, src...) }
func Noop(dst, src []byte) []byte { return append(dst, src...) }
func AppendQuote(dst, src by, ac AppendBytesClosure) by {
func AppendQuote(dst, src []byte, ac AppendBytesClosure) []byte {
dst = append(dst, '"')
dst = ac(dst, src)
dst = append(dst, '"')
return dst
}
func Quote(dst, src by) by { return AppendQuote(dst, src, Noop) }
func Quote(dst, src []byte) []byte { return AppendQuote(dst, src, Noop) }
func AppendSingleQuote(dst, src by, ac AppendBytesClosure) by {
func AppendSingleQuote(dst, src []byte, ac AppendBytesClosure) []byte {
dst = append(dst, '\'')
dst = ac(dst, src)
dst = append(dst, '\'')
return dst
}
func AppendBackticks(dst, src by, ac AppendBytesClosure) by {
func AppendBackticks(dst, src []byte, ac AppendBytesClosure) []byte {
dst = append(dst, '`')
dst = ac(dst, src)
dst = append(dst, '`')
return dst
}
func AppendBrace(dst, src by, ac AppendBytesClosure) by {
func AppendBrace(dst, src []byte, ac AppendBytesClosure) []byte {
dst = append(dst, '(')
dst = ac(dst, src)
dst = append(dst, ')')
return dst
}
func AppendParenthesis(dst, src by, ac AppendBytesClosure) by {
func AppendParenthesis(dst, src []byte, ac AppendBytesClosure) []byte {
dst = append(dst, '{')
dst = ac(dst, src)
dst = append(dst, '}')
return dst
}
func AppendBracket(dst, src by, ac AppendBytesClosure) by {
func AppendBracket(dst, src []byte, ac AppendBytesClosure) []byte {
dst = append(dst, '[')
dst = ac(dst, src)
dst = append(dst, ']')
return dst
}
func AppendList(dst by, src []by, separator byte,
ac AppendBytesClosure) by {
func AppendList(dst []byte, src [][]byte, separator byte,
ac AppendBytesClosure) []byte {
last := len(src) - 1
for i := range src {
dst = append(dst, ac(dst, src[i])...)