-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from zostay/bytes-and-indent
Bytes and indent
- Loading branch information
Showing
9 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package bytes | ||
|
||
import "bytes" | ||
|
||
// ContainsOnly returns true fi the given byte slice only contains the given | ||
// letters. | ||
func ContainsOnly(b []byte, chars []rune) bool { | ||
idx := make(map[rune]struct{}, len(chars)) | ||
for _, c := range chars { | ||
idx[c] = struct{}{} | ||
} | ||
return bytes.IndexFunc(b, func(c rune) bool { | ||
_, expectedChar := idx[c] | ||
return !expectedChar | ||
}) == -1 | ||
} | ||
|
||
// FromRange returns a byte slice containing all the characters between two given | ||
// bytes (inclusive). | ||
func FromRange(a, z byte) []byte { | ||
reverse := false | ||
if a > z { | ||
reverse = true | ||
a, z = z, a | ||
} | ||
|
||
out := make([]byte, z-a+1) | ||
for i := a; i <= z; i++ { | ||
out[i-a] = i | ||
} | ||
|
||
if reverse { | ||
return Reverse(out) | ||
} | ||
|
||
return out | ||
} | ||
|
||
// Reverse returns the byte slice reversed. That is, passing the byte slice | ||
// []byte("Reversing text.") will result in this function returning | ||
// []byte(".txet gnisreveR"). | ||
func Reverse(in []byte) []byte { | ||
out := make([]byte, len(in)) | ||
for i := 0; i < len(in); i++ { | ||
out[i] = in[len(in)-i-1] | ||
} | ||
return out | ||
} | ||
|
||
// Indent returns a new byte slice with each line in the input byte slice | ||
// indented by the given byte slice. | ||
func Indent(b, indent []byte) []byte { | ||
startIndent := indent | ||
if b[0] == '\n' { | ||
startIndent = []byte{} | ||
} | ||
newB := bytes.ReplaceAll(b, []byte("\n"), []byte("\n"+string(indent))) | ||
if len(startIndent) > 0 { | ||
newB = append(newB, startIndent...) | ||
copy(newB[len(startIndent):], newB) | ||
copy(newB, startIndent) | ||
} | ||
return bytes.TrimRight(newB, " ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package bytes_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/zostay/go-std/bytes" | ||
) | ||
|
||
func TestContainsOnly(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.True(t, bytes.ContainsOnly([]byte("abc"), []rune("abc"))) | ||
assert.True(t, bytes.ContainsOnly([]byte("a"), []rune("abc"))) | ||
assert.True(t, bytes.ContainsOnly([]byte("aaaaaabbbbbbccccc"), []rune("cba"))) | ||
assert.False(t, bytes.ContainsOnly([]byte("a b c"), []rune("abc"))) | ||
} | ||
|
||
func TestFromRange(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, []byte("abcd"), bytes.FromRange('a', 'd')) | ||
assert.Equal(t, []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), bytes.FromRange('A', 'Z')) | ||
assert.Equal(t, []byte("0123456789"), bytes.FromRange('0', '9')) | ||
assert.Equal(t, []byte("zyxwvutsrqponmlkjihgfedcba"), bytes.FromRange('z', 'a')) | ||
} | ||
|
||
func TestReverse(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, []byte(".txet gnisreveR"), bytes.Reverse([]byte("Reversing text."))) | ||
} | ||
|
||
func TestIndent(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, []byte("\n a\n b\n c\n"), bytes.Indent([]byte("\na\nb\nc\n"), []byte(" "))) | ||
assert.Equal(t, []byte(" a\n b\n c"), bytes.Indent([]byte("a\nb\nc"), []byte(" "))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package bytes provides additional tools for working with byte slices. | ||
package bytes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters