diff options
| author | Shulhan <ms@kilabit.info> | 2019-02-21 08:35:02 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-02-21 09:05:46 +0700 |
| commit | 4e4fda78a2126ac78c6c0fb88040e687bfc7950e (patch) | |
| tree | dae2766f54c6d589502336bd73421cb54cf18b21 /lib/bytes | |
| parent | 47af7f937043ba3c0f5920e1642438c2d2ca257a (diff) | |
| download | pakakeh.go-4e4fda78a2126ac78c6c0fb88040e687bfc7950e.tar.xz | |
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
Diffstat (limited to 'lib/bytes')
| -rw-r--r-- | lib/bytes/bytes.go | 28 | ||||
| -rw-r--r-- | lib/bytes/bytes_test.go | 16 |
2 files changed, 44 insertions, 0 deletions
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 @@ -329,6 +329,34 @@ func ReadUint32(data []byte, x uint) uint32 { } // +// 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. // diff --git a/lib/bytes/bytes_test.go b/lib/bytes/bytes_test.go index 2e1b3a24..5885eeb2 100644 --- a/lib/bytes/bytes_test.go +++ b/lib/bytes/bytes_test.go @@ -312,6 +312,22 @@ func TestTokenFind(t *testing.T) { testTokenFind(t, line, token, 0, exp) } +func TestInReplace(t *testing.T) { + cases := []struct { + in string + exp string + }{{ + in: "/a/path/to/file.ext", + exp: "_a_path_to_file_ext", + }} + + for _, c := range cases { + got := InReplace([]byte(c.in), []byte(ASCIILettersNumber), '_') + + test.Assert(t, "InReplace", c.exp, string(got), true) + } +} + func BenchmarkToLowerStd(b *testing.B) { randomInput256 := Random([]byte(HexaLetters), 256) |
