aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/shurcooL/vfsgen/commentwriter.go
blob: b6847f52b0af8cd6be6c34d3ad8cfd378bdee94d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package vfsgen

import "io"

// commentWriter writes a Go comment to the underlying io.Writer,
// using line comment form (//).
type commentWriter struct {
	W            io.Writer
	wroteSlashes bool // Wrote "//" at the beginning of the current line.
}

func (c *commentWriter) Write(p []byte) (int, error) {
	var n int
	for i, b := range p {
		if !c.wroteSlashes {
			s := "//"
			if b != '\n' {
				s = "// "
			}
			if _, err := io.WriteString(c.W, s); err != nil {
				return n, err
			}
			c.wroteSlashes = true
		}
		n0, err := c.W.Write(p[i : i+1])
		n += n0
		if err != nil {
			return n, err
		}
		if b == '\n' {
			c.wroteSlashes = false
		}
	}
	return len(p), nil
}

func (c *commentWriter) Close() error {
	if !c.wroteSlashes {
		if _, err := io.WriteString(c.W, "//"); err != nil {
			return err
		}
		c.wroteSlashes = true
	}
	return nil
}