From 4e4fda78a2126ac78c6c0fb88040e687bfc7950e Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 21 Feb 2019 08:35:02 +0700 Subject: memfs: add method to dump files as Go generated source If you familiar with go-bindata [1], this method is similar with that. [1] github.com/shuLhan/go-bindata --- lib/bytes/bytes.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'lib/bytes/bytes.go') diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 63a502ab..83cd99c9 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -328,6 +328,34 @@ func ReadUint32(data []byte, x uint) uint32 { return uint32(data[x])<<24 | uint32(data[x+1])<<16 | uint32(data[x+2])<<8 | uint32(data[x+3]) } +// +// InReplace do a reverse replace on input, any characters that is not on +// allowed, will be replaced with character c. +// +func InReplace(in, allowed []byte, c byte) (out []byte) { + if len(in) == 0 { + return + } + + out = make([]byte, len(in)) + copy(out, in) + found := false + for x := 0; x < len(in); x++ { + found = false + for y := 0; y < len(allowed); y++ { + if in[x] == allowed[y] { + found = true + break + } + } + if !found { + out[x] = c + } + } + + return +} + // // SkipAfterToken skip all bytes until matched token is found and return the // index after the token and boolean true. -- cgit v1.3