aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-06-14 19:02:22 +0700
committerShulhan <ms@kilabit.info>2019-06-14 19:02:22 +0700
commit8d5ffba2d9483f049385d37bc468f2a114548eb0 (patch)
treef545e93345f3c4273ec1b962f797bdcbc20dc80b /lib/bytes
parent381a0c6e6dfc702ffbbd398abaae356f4e075a0e (diff)
downloadpakakeh.go-8d5ffba2d9483f049385d37bc468f2a114548eb0.tar.xz
ascii: new library for working with ASCII characters
This library previously part of bytes package. To make it bytes package consistent (only working with slice of byte), we move all ASCII related constants, variables, and functions into new package.
Diffstat (limited to 'lib/bytes')
-rw-r--r--lib/bytes/bytes.go48
-rw-r--r--lib/bytes/bytes_test.go58
-rw-r--r--lib/bytes/is.go70
-rw-r--r--lib/bytes/random.go22
4 files changed, 2 insertions, 196 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index 8a1e94f7..62e7d307 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -10,30 +10,6 @@ import (
"reflect"
)
-const (
- // ASCIILetters contains list of lower and upper case characters in
- // ASCII.
- ASCIILetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
- // ASCIILettersNumber contains list of lower and upper case
- // characters in ASCII with numbers.
- ASCIILettersNumber = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
-
- // HexaLETTERS contains list of hexadecimal characters in upper cases.
- HexaLETTERS = "0123456789ABCDEF"
- // HexaLetters contains list of hexadecimal characters in lower and
- // upper cases.
- HexaLetters = "0123456789abcedfABCDEF"
- // Hexaletters contains list of hexadecimal characters in lower cases.
- Hexaletters = "0123456789abcedf"
-)
-
-//nolint:gochecknoglobals
-var (
- // ASCIISpaces contains list of white spaces in ASCII.
- ASCIISpaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '}
-)
-
//
// AppendInt16 into slice of byte.
//
@@ -445,30 +421,6 @@ func SkipAfterToken(line, token []byte, startAt int, checkEsc bool) (int, bool)
}
//
-// ToLower convert slice of bytes to lower cases, in places.
-//
-func ToLower(data *[]byte) {
- for x := 0; x < len(*data); x++ {
- if (*data)[x] < 'A' || (*data)[x] > 'Z' {
- continue
- }
- (*data)[x] += 32
- }
-}
-
-//
-// ToUpper convert slice of bytes to upper cases, in places.
-//
-func ToUpper(data *[]byte) {
- for x := 0; x < len(*data); x++ {
- if (*data)[x] < 'a' || (*data)[x] > 'z' {
- continue
- }
- (*data)[x] -= 32
- }
-}
-
-//
// TokenFind return the first index of matched token in line, start at custom
// index.
// If "startat" parameter is less than 0, then it will be set to 0.
diff --git a/lib/bytes/bytes_test.go b/lib/bytes/bytes_test.go
index e5eb2ac4..938f4ca1 100644
--- a/lib/bytes/bytes_test.go
+++ b/lib/bytes/bytes_test.go
@@ -1,9 +1,9 @@
package bytes
import (
- "bytes"
"testing"
+ "github.com/shuLhan/share/lib/ascii"
"github.com/shuLhan/share/lib/test"
)
@@ -280,33 +280,6 @@ func TestSkipAfterToken(t *testing.T) {
}
}
-func TestToLower(t *testing.T) {
- cases := []struct {
- in []byte
- exp []byte
- }{{
- in: []byte("@ABCDEFG"),
- exp: []byte("@abcdefg"),
- }, {
- in: []byte("@ABCDEFG12345678"),
- exp: []byte("@abcdefg12345678"),
- }, {
- in: []byte("@ABCDEFGhijklmno12345678"),
- exp: []byte("@abcdefghijklmno12345678"),
- }, {
- in: []byte("@ABCDEFGhijklmnoPQRSTUVW12345678"),
- exp: []byte("@abcdefghijklmnopqrstuvw12345678"),
- }, {
- in: []byte("@ABCDEFGhijklmnoPQRSTUVWxyz{12345678"),
- exp: []byte("@abcdefghijklmnopqrstuvwxyz{12345678"),
- }}
-
- for _, c := range cases {
- ToLower(&c.in)
- test.Assert(t, "ToLower", c.exp, c.in, true)
- }
-}
-
func testTokenFind(t *testing.T, line, token []byte, startat int, exp []int) {
got := []int{}
tokenlen := len(token)
@@ -354,35 +327,8 @@ func TestInReplace(t *testing.T) {
}}
for _, c := range cases {
- got := InReplace([]byte(c.in), []byte(ASCIILettersNumber), '_')
+ got := InReplace([]byte(c.in), []byte(ascii.LettersNumber), '_')
test.Assert(t, "InReplace", c.exp, string(got), true)
}
}
-
-func BenchmarkToLowerStd(b *testing.B) {
- randomInput256 := Random([]byte(HexaLetters), 256)
-
- in := make([]byte, len(randomInput256))
- copy(in, randomInput256)
-
- b.ResetTimer()
-
- for x := 0; x < b.N; x++ {
- bytes.ToLower(in)
- }
-}
-
-func BenchmarkToLower(b *testing.B) {
- randomInput256 := Random([]byte(HexaLetters), 256)
-
- in := make([]byte, len(randomInput256))
- copy(in, randomInput256)
-
- b.ResetTimer()
-
- for x := 0; x < b.N; x++ {
- ToLower(&in)
- copy(in, randomInput256)
- }
-}
diff --git a/lib/bytes/is.go b/lib/bytes/is.go
deleted file mode 100644
index 4ad06805..00000000
--- a/lib/bytes/is.go
+++ /dev/null
@@ -1,70 +0,0 @@
-// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package bytes
-
-//
-// IsAlpha will return true if byte is ASCII alphabet character, otherwise
-// it will return false.
-//
-func IsAlpha(b byte) bool {
- if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') {
- return true
- }
- return false
-}
-
-//
-// IsAlnum will return true if byte is ASCII alphanumeric character, otherwise
-// it will return false.
-//
-func IsAlnum(b byte) bool {
- return IsAlpha(b) || IsDigit(b)
-}
-
-//
-// IsDigit will return true if byte is ASCII digit, otherwise it will return
-// false.
-//
-func IsDigit(b byte) bool {
- if b >= '0' && b <= '9' {
- return true
- }
- return false
-}
-
-//
-// IsDigits will return true if all bytes are ASCII digit, otherwise it will
-// return false.
-//
-func IsDigits(data []byte) bool {
- for x := 0; x < len(data); x++ {
- if !IsDigit(data[x]) {
- return false
- }
- }
- return true
-}
-
-//
-// IsHex will return true if byte is hexadecimal number, otherwise it will
-// return false.
-//
-func IsHex(b byte) bool {
- if (b >= '1' && b <= '9') || (b >= 'a' && b <= 'f') || (b >= 'A' && b <= 'F') {
- return true
- }
- return false
-}
-
-//
-// IsSpace will return true if byte is ASCII white spaces character,
-// otherwise it will return false.
-//
-func IsSpace(b byte) bool {
- if b == '\t' || b == '\n' || b == '\v' || b == '\f' || b == '\r' || b == ' ' {
- return true
- }
- return false
-}
diff --git a/lib/bytes/random.go b/lib/bytes/random.go
deleted file mode 100644
index 73945788..00000000
--- a/lib/bytes/random.go
+++ /dev/null
@@ -1,22 +0,0 @@
-// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package bytes
-
-import (
- "math/rand"
-)
-
-//
-// Random generate random sequence of value from seed with fixed length.
-//
-// This function assume that random generator has been seeded.
-//
-func Random(seed []byte, n int) []byte {
- b := make([]byte, n)
- for x := 0; x < n; x++ {
- b[x] = seed[rand.Intn(len(seed))]
- }
- return b
-}