You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commentwriter.go 854B

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