From: Robert Griesemer Date: Tue, 14 Dec 2010 01:08:40 +0000 (-0800) Subject: token/position: provide files iterator X-Git-Tag: weekly.2010-12-15~32 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6fcae0f04431b6a02959c9d6476051bed967523d;p=gostls13.git token/position: provide files iterator R=golang-dev, r2 CC=golang-dev https://golang.org/cl/3541044 --- diff --git a/src/pkg/go/token/position.go b/src/pkg/go/token/position.go index 8eb8d138e6..0044a0ed77 100644 --- a/src/pkg/go/token/position.go +++ b/src/pkg/go/token/position.go @@ -385,3 +385,25 @@ func (s *FileSet) AddFile(filename string, base, size int) *File { s.files = append(s.files, f) return f } + + +// Files returns the files added to the file set. +func (s *FileSet) Files() <-chan *File { + ch := make(chan *File) + go func() { + for i := 0; ; i++ { + var f *File + s.mutex.RLock() + if i < len(s.files) { + f = s.files[i] + } + s.mutex.RUnlock() + if f == nil { + break + } + ch <- f + } + close(ch) + }() + return ch +} diff --git a/src/pkg/go/token/position_test.go b/src/pkg/go/token/position_test.go index bc10ef6c0a..1cffcc3c27 100644 --- a/src/pkg/go/token/position_test.go +++ b/src/pkg/go/token/position_test.go @@ -138,3 +138,21 @@ func TestLineInfo(t *testing.T) { checkPos(t, msg, fset.Position(p), Position{"bar", offs, 42, col}) } } + + +func TestFiles(t *testing.T) { + fset := NewFileSet() + for i, test := range tests { + fset.AddFile(test.filename, fset.Base(), test.size) + j := 0 + for g := range fset.Files() { + if g.Name() != tests[j].filename { + t.Errorf("expected filename = %s; got %s", tests[j].filename, g.Name()) + } + j++ + } + if j != i+1 { + t.Errorf("expected %d files; got %d", i+1, j) + } + } +}