Skip to content

Commit

Permalink
capnp: add Ptr.TextBytes method
Browse files Browse the repository at this point in the history
Update FooBytes() code generation to use it.  This should decrease
generated code size and correct the NUL byte check.
  • Loading branch information
zombiezen committed Jun 19, 2016
1 parent b6ff639 commit 98005f9
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 212 deletions.
2 changes: 1 addition & 1 deletion capnpc-go/templates.go

Large diffs are not rendered by default.

11 changes: 2 additions & 9 deletions capnpc-go/templates/structTextField
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ func (s {{.Node.Name}}) {{.Field.Name|title}}() (string, error) {

func (s {{.Node.Name}}) {{.Field.Name|title}}Bytes() ([]byte, error) {
p, err := s.Struct.Ptr({{.Field.Slot.Offset}})
if err != nil {
return nil, err
}
{{with .Default -}}
d := p.DataDefault([]byte({{printf "%q" .}} + "\x00"))
return p.TextBytesDefault({{printf "%q" .}}), err
{{- else -}}
d := p.Data()
return p.TextBytes(), err
{{- end}}
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
}

func (s {{.Node.Name}}) Set{{.Field.Name|title}}(v string) error {
Expand Down
90 changes: 10 additions & 80 deletions internal/aircraftlib/aircraft.capnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,7 @@ func (s PlaneBase) HasName() bool {

func (s PlaneBase) NameBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s PlaneBase) SetName(v string) error {
Expand Down Expand Up @@ -1142,14 +1135,7 @@ func (s Z) HasText() bool {

func (s Z) TextBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Z) SetText(v string) error {
Expand Down Expand Up @@ -1931,14 +1917,7 @@ func (s Counter) HasWords() bool {

func (s Counter) WordsBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Counter) SetWords(v string) error {
Expand Down Expand Up @@ -2154,14 +2133,7 @@ func (s Zjob) HasCmd() bool {

func (s Zjob) CmdBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Zjob) SetCmd(v string) error {
Expand Down Expand Up @@ -3276,14 +3248,7 @@ func (s HoldsText) HasTxt() bool {

func (s HoldsText) TxtBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s HoldsText) SetTxt(v string) error {
Expand Down Expand Up @@ -3915,14 +3880,7 @@ func (s Echo_echo_Params) HasIn() bool {

func (s Echo_echo_Params) InBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Echo_echo_Params) SetIn(v string) error {
Expand Down Expand Up @@ -3985,14 +3943,7 @@ func (s Echo_echo_Results) HasOut() bool {

func (s Echo_echo_Results) OutBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Echo_echo_Results) SetOut(v string) error {
Expand Down Expand Up @@ -4560,14 +4511,7 @@ func (s Defaults) HasText() bool {

func (s Defaults) TextBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.DataDefault([]byte("foo" + "\x00"))
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytesDefault("foo"), err
}

func (s Defaults) SetText(v string) error {
Expand Down Expand Up @@ -4670,14 +4614,7 @@ func (s BenchmarkA) HasName() bool {

func (s BenchmarkA) NameBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s BenchmarkA) SetName(v string) error {
Expand Down Expand Up @@ -4708,14 +4645,7 @@ func (s BenchmarkA) HasPhone() bool {

func (s BenchmarkA) PhoneBytes() ([]byte, error) {
p, err := s.Struct.Ptr(1)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s BenchmarkA) SetPhone(v string) error {
Expand Down
9 changes: 1 addition & 8 deletions internal/demo/books/books.capnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ func (s Book) HasTitle() bool {

func (s Book) TitleBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Book) SetTitle(v string) error {
Expand Down
6 changes: 1 addition & 5 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,7 @@ func (l TextList) BytesAt(i int) ([]byte, error) {
if err != nil {
return nil, err
}
b := p.Data()
if len(b) == 0 {
return b, nil
}
return b[:len(b)-1 : len(b)], nil
return p.TextBytes(), nil
}

// Set sets the i'th string in the list to v.
Expand Down
44 changes: 39 additions & 5 deletions pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,56 @@ func (p Ptr) Interface() Interface {
// Text attempts to convert p into Text, returning an empty string if
// p is not a valid 1-byte list pointer.
func (p Ptr) Text() string {
return p.TextDefault("")
b, ok := p.text()
if !ok {
return ""
}
return string(b)
}

// TextDefault attempts to convert p into Text, returning def if p is
// not a valid 1-byte list pointer.
func (p Ptr) TextDefault(def string) string {
if !isOneByteList(p) {
b, ok := p.text()
if !ok {
return def
}
return string(b)
}

// TextBytes attempts to convert p into Text, returning nil if p is not
// a valid 1-byte list pointer. It returns a slice directly into the
// segment.
func (p Ptr) TextBytes() []byte {
b, ok := p.text()
if !ok {
return nil
}
return b
}

// TextBytesDefault attempts to convert p into Text, returning def if p
// is not a valid 1-byte list pointer. It returns a slice directly into
// the segment.
func (p Ptr) TextBytesDefault(def string) []byte {
b, ok := p.text()
if !ok {
return []byte(def)
}
return b
}

func (p Ptr) text() (b []byte, ok bool) {
if !isOneByteList(p) {
return nil, false
}
l := p.List()
b := l.seg.slice(l.off, Size(l.length))
b = l.seg.slice(l.off, Size(l.length))
if len(b) == 0 || b[len(b)-1] != 0 {
// Text must be null-terminated.
return def
return nil, false
}
return string(b[:len(b)-1])
return b[:len(b)-1 : len(b)], true
}

// Data attempts to convert p into Data, returning nil if p is not a
Expand Down
27 changes: 3 additions & 24 deletions std/capnp/json/json.capnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@ func (s JsonValue) HasString() bool {

func (s JsonValue) StringBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s JsonValue) SetString(v string) error {
Expand Down Expand Up @@ -251,14 +244,7 @@ func (s JsonValue_Field) HasName() bool {

func (s JsonValue_Field) NameBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s JsonValue_Field) SetName(v string) error {
Expand Down Expand Up @@ -350,14 +336,7 @@ func (s JsonValue_Call) HasFunction() bool {

func (s JsonValue_Call) FunctionBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s JsonValue_Call) SetFunction(v string) error {
Expand Down
9 changes: 1 addition & 8 deletions std/capnp/rpc/rpc.capnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2260,14 +2260,7 @@ func (s Exception) HasReason() bool {

func (s Exception) ReasonBytes() ([]byte, error) {
p, err := s.Struct.Ptr(0)
if err != nil {
return nil, err
}
d := p.Data()
if len(d) == 0 {
return d, nil
}
return d[:len(d)-1], nil
return p.TextBytes(), err
}

func (s Exception) SetReason(v string) error {
Expand Down
Loading

0 comments on commit 98005f9

Please sign in to comment.