From c992230e545e9f18f15cf76d7a9c8ac08be0e7dd Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Wed, 6 Aug 2014 18:01:26 -0600 Subject: [PATCH] Start at Gapstr --- Makefile | 3 +++ src/Makefile | 3 +++ src/fred/gaptest.go | 14 ++++++++++++++ src/netarch/gapstr.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 src/Makefile create mode 100644 src/fred/gaptest.go create mode 100644 src/netarch/gapstr.go diff --git a/Makefile b/Makefile index 5474dc2..e4e4162 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ export GOPATH all: $(TARGETS) +%: src/%.go + go build $@ + %: %.go go build $< diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..be33ee9 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,3 @@ +all: +%: + $(MAKE) -C .. $@ diff --git a/src/fred/gaptest.go b/src/fred/gaptest.go new file mode 100644 index 0000000..7797da6 --- /dev/null +++ b/src/fred/gaptest.go @@ -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()) +} diff --git a/src/netarch/gapstr.go b/src/netarch/gapstr.go new file mode 100644 index 0000000..9ecc54e --- /dev/null +++ b/src/netarch/gapstr.go @@ -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() +} \ No newline at end of file