Compare commits
3 Commits
a854dd0618
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ab8fc16c2 | |||
| 5cc89d5b42 | |||
| dbffe4b7c4 |
@@ -1,4 +1,7 @@
|
|||||||
# ASV
|
# ASV
|
||||||
|
|
||||||
|
[](https://pkg.go.dev/git.tebibyte.media/sashakoshka/go-asv)
|
||||||
|
|
||||||
This module allows encoding and decoding data using ASV (ASCII-separated
|
This module allows encoding and decoding data using ASV (ASCII-separated
|
||||||
values). Data is organized into a hierarchy of files, groups, records, and
|
values). Data is organized into a hierarchy of files, groups, records, and
|
||||||
units, separated by ASCII FS, GS, RS, and US respectively.
|
units, separated by ASCII FS, GS, RS, and US respectively.
|
||||||
|
|||||||
13
asv.go
13
asv.go
@@ -156,12 +156,17 @@ func (this *Decoder) ReadUnit () (unit Unit, next rune, err error) {
|
|||||||
char, _, err := this.reader.ReadRune()
|
char, _, err := this.reader.ReadRune()
|
||||||
if err != nil { return Unit(str.String()), 0, err }
|
if err != nil { return Unit(str.String()), 0, err }
|
||||||
|
|
||||||
if esc {
|
switch {
|
||||||
|
case esc:
|
||||||
esc = false
|
esc = false
|
||||||
} else if IsSeparator(char) {
|
|
||||||
return Unit(str.String()), char, nil
|
|
||||||
}
|
|
||||||
str.WriteRune(char)
|
str.WriteRune(char)
|
||||||
|
case char == Escape:
|
||||||
|
esc = true
|
||||||
|
case IsSeparator(char):
|
||||||
|
return Unit(str.String()), char, nil
|
||||||
|
default:
|
||||||
|
str.WriteRune(char)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
70
asv_test.go
Normal file
70
asv_test.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package asv
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
import "strings"
|
||||||
|
import "testing"
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
func TestDecodeCollection (test *testing.T) {
|
||||||
|
decoder := NewDecoder(strings.NewReader(
|
||||||
|
"record\x1fone\x1erecord\x1ftwo\x1drecord one in group two\x1crecord one in file two" +
|
||||||
|
"\x1flots of \x1b\x1b\x1b\x1f\x1b\x1d\x1b\x1e\x1b\x1cescape sequences"))
|
||||||
|
got, err := decoder.ReadCollection()
|
||||||
|
testErr(test, err)
|
||||||
|
testEqual(test, "structures", got,
|
||||||
|
Collection {
|
||||||
|
File {
|
||||||
|
Group {
|
||||||
|
Record {
|
||||||
|
"record",
|
||||||
|
"one",
|
||||||
|
},
|
||||||
|
Record {
|
||||||
|
"record",
|
||||||
|
"two",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Group {
|
||||||
|
Record {
|
||||||
|
"record one in group two",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
File {
|
||||||
|
Group {
|
||||||
|
Record {
|
||||||
|
"record one in file two",
|
||||||
|
"lots of \x1b\x1f\x1d\x1e\x1cescape sequences",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeRecord (test *testing.T) {
|
||||||
|
decoder := NewDecoder(strings.NewReader(
|
||||||
|
"record\x1fone"))
|
||||||
|
got, next, err := decoder.ReadRecord()
|
||||||
|
testEqual(test, "errors", err, io.EOF)
|
||||||
|
testEqual(test, "next", next, rune(0))
|
||||||
|
testEqual(test, "structures", got,
|
||||||
|
Record {
|
||||||
|
"record",
|
||||||
|
"one",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testErr (test *testing.T, err error) {
|
||||||
|
if err != nil {
|
||||||
|
test.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testEqual (test *testing.T, desc string, got, correct any) {
|
||||||
|
if !reflect.DeepEqual(got, correct) {
|
||||||
|
test.Error(desc, "not equal")
|
||||||
|
test.Errorf("GOT:\n%v", got)
|
||||||
|
test.Errorf("CORRECT:\n%v", correct)
|
||||||
|
test.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user