Start at Gapstr

This commit is contained in:
Neale Pickett 2014-08-06 18:01:26 -06:00
parent 6ada731c44
commit c992230e54
4 changed files with 60 additions and 0 deletions

View File

@ -7,6 +7,9 @@ export GOPATH
all: $(TARGETS)
%: src/%.go
go build $@
%: %.go
go build $<

3
src/Makefile Normal file
View File

@ -0,0 +1,3 @@
all:
%:
$(MAKE) -C .. $@

14
src/fred/gaptest.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"fmt"
"netarch"
)
func main() {
g := new(netarch.Gapstr)
g = g.AppendString("hello")
g = g.AppendGap(12)
g = g.AppendString("world")
fmt.Println("hi", g.String())
}

40
src/netarch/gapstr.go Normal file
View File

@ -0,0 +1,40 @@
package netarch
import (
"bytes"
)
type chunk interface{}
type Gapstr struct {
chunks []chunk
}
func GapstrOfString(s string) *Gapstr {
return &Gapstr{[]chunk{s}}
}
func (g *Gapstr) appendChunk(c chunk) *Gapstr {
return &Gapstr{append(g.chunks, c)}
}
func (g *Gapstr) AppendString(s string) *Gapstr {
return g.appendChunk(s)
}
func (g *Gapstr) AppendGap(len int) *Gapstr {
return g.appendChunk(len)
}
func (g *Gapstr) String() string {
var b bytes.Buffer
for _, c := range g.chunks {
switch c.(type) {
case int:
b.Write(bytes.Repeat([]byte{0}, c.(int)))
case string:
b.WriteString(c.(string))
}
}
return b.String()
}