commentwriter.go

 1package vfsgen
 2
 3import "io"
 4
 5// commentWriter writes a Go comment to the underlying io.Writer,
 6// using line comment form (//).
 7type commentWriter struct {
 8	W            io.Writer
 9	wroteSlashes bool // Wrote "//" at the beginning of the current line.
10}
11
12func (c *commentWriter) Write(p []byte) (int, error) {
13	var n int
14	for i, b := range p {
15		if !c.wroteSlashes {
16			s := "//"
17			if b != '\n' {
18				s = "// "
19			}
20			if _, err := io.WriteString(c.W, s); err != nil {
21				return n, err
22			}
23			c.wroteSlashes = true
24		}
25		n0, err := c.W.Write(p[i : i+1])
26		n += n0
27		if err != nil {
28			return n, err
29		}
30		if b == '\n' {
31			c.wroteSlashes = false
32		}
33	}
34	return len(p), nil
35}
36
37func (c *commentWriter) Close() error {
38	if !c.wroteSlashes {
39		if _, err := io.WriteString(c.W, "//"); err != nil {
40			return err
41		}
42		c.wroteSlashes = true
43	}
44	return nil
45}