gitea

Development moved to Codeberg

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
package vfsgen

import (
	"io"
)

// stringWriter writes given bytes to underlying io.Writer as a Go interpreted string literal value,
// not including double quotes. It tracks the total number of bytes written.
type stringWriter struct {
	io.Writer
	N int64 // Total bytes written.
}

func (sw *stringWriter) Write(p []byte) (n int, err error) {
	const hex = "0123456789abcdef"
	buf := []byte{'\\', 'x', 0, 0}
	for _, b := range p {
		buf[2], buf[3] = hex[b/16], hex[b%16]
		_, err = sw.Writer.Write(buf)
		if err != nil {
			return n, err
		}
		n++
		sw.N++
	}
	return n, nil
}