diff options
| author | Shulhan <ms@kilabit.info> | 2018-09-03 22:27:44 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2018-09-03 22:55:06 +0700 |
| commit | d3e6c2e902eb3a00c2dfdcde09bf35282ea63396 (patch) | |
| tree | 63432c09e0dddb08942e7172f24666a811432bb5 /lib | |
| parent | aab053ef5c49bcc67f6de5477d9760ea4e3bf6ab (diff) | |
| download | pakakeh.go-d3e6c2e902eb3a00c2dfdcde09bf35282ea63396.tar.xz | |
Move all byte(s) related constant and functions from package text to bytes
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/bytes/bytes.go | 20 | ||||
| -rw-r--r-- | lib/bytes/bytes_test.go | 3 | ||||
| -rw-r--r-- | lib/bytes/is.go (renamed from lib/text/is.go) | 6 | ||||
| -rw-r--r-- | lib/bytes/json.go (renamed from lib/text/bytes.go) | 28 | ||||
| -rw-r--r-- | lib/bytes/random.go | 22 | ||||
| -rw-r--r-- | lib/dns/masterfile.go | 9 | ||||
| -rw-r--r-- | lib/dns/message.go | 3 | ||||
| -rw-r--r-- | lib/io/reader.go | 4 | ||||
| -rw-r--r-- | lib/net/is.go | 8 | ||||
| -rw-r--r-- | lib/net/resolvconf_test.go | 4 | ||||
| -rw-r--r-- | lib/text/string.go | 12 | ||||
| -rw-r--r-- | lib/text/text.go | 18 | ||||
| -rw-r--r-- | lib/time/duration.go | 6 |
13 files changed, 82 insertions, 61 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 549d82db..ad777180 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -2,13 +2,31 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Packages bytes contains common functions to manipulate slice of bytes. +// Packages bytes provide a library for working with byte or slice of bytes. package bytes import ( "fmt" ) +const ( + // ASCIILetters contains list of lower and upper case characters in + // ASCII. + ASCIILetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + // 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" +) + +var ( + // ASCIISpaces contains list of white spaces in ASCII. + ASCIISpaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '} +) + // // PrintHex will print each byte in slice as hexadecimal value into N column // length. diff --git a/lib/bytes/bytes_test.go b/lib/bytes/bytes_test.go index d4fef2a6..50669c4f 100644 --- a/lib/bytes/bytes_test.go +++ b/lib/bytes/bytes_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/shuLhan/share/lib/test" - libtext "github.com/shuLhan/share/lib/text" ) func TestToLower(t *testing.T) { @@ -35,7 +34,7 @@ func TestToLower(t *testing.T) { } } -var randomInput256 = libtext.Random([]byte(libtext.HexaLetters), 256) +var randomInput256 = Random([]byte(HexaLetters), 256) func BenchmarkToLowerStd(b *testing.B) { in := make([]byte, len(randomInput256)) diff --git a/lib/text/is.go b/lib/bytes/is.go index 21829272..4ad06805 100644 --- a/lib/text/is.go +++ b/lib/bytes/is.go @@ -1,4 +1,8 @@ -package text +// 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 diff --git a/lib/text/bytes.go b/lib/bytes/json.go index 8b9a1e48..1a296a3b 100644 --- a/lib/text/bytes.go +++ b/lib/bytes/json.go @@ -1,9 +1,12 @@ -package text +// 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 ( "bytes" "fmt" - "math/rand" "strconv" ) @@ -23,7 +26,7 @@ const ( ) // -// BytesJSONEscape escape the following character: `"` (quotation mark), +// JSONEscape escape the following character: `"` (quotation mark), // `\` (reverse solidus), `/` (solidus), `\b` (backspace), `\f` (formfeed), // `\n` (newline), `\r` (carriage return`), `\t` (horizontal tab), and control // character from 0 - 31. @@ -32,7 +35,7 @@ const ( // // * https://tools.ietf.org/html/rfc7159#page-8 // -func BytesJSONEscape(in []byte) []byte { +func JSONEscape(in []byte) []byte { var buf bytes.Buffer for x := 0; x < len(in); x++ { @@ -78,14 +81,14 @@ func BytesJSONEscape(in []byte) []byte { } // -// BytesJSONUnescape unescape JSON bytes, reversing what BytesJSONEscape do. +// JSONUnescape unescape JSON bytes, reversing what BytesJSONEscape do. // // If strict is true, any unknown control character will be returned as error. // For example, in string "\x", "x" is not valid control character, and the // function will return empty string and error. // If strict is false, it will return "x". // -func BytesJSONUnescape(in []byte, strict bool) ([]byte, error) { +func JSONUnescape(in []byte, strict bool) ([]byte, error) { var ( buf bytes.Buffer uni bytes.Buffer @@ -167,16 +170,3 @@ func BytesJSONUnescape(in []byte, strict bool) ([]byte, error) { return buf.Bytes(), nil } - -// -// 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 -} diff --git a/lib/bytes/random.go b/lib/bytes/random.go new file mode 100644 index 00000000..73945788 --- /dev/null +++ b/lib/bytes/random.go @@ -0,0 +1,22 @@ +// 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 +} diff --git a/lib/dns/masterfile.go b/lib/dns/masterfile.go index 6011fa58..22c33511 100644 --- a/lib/dns/masterfile.go +++ b/lib/dns/masterfile.go @@ -17,7 +17,6 @@ import ( libbytes "github.com/shuLhan/share/lib/bytes" libio "github.com/shuLhan/share/lib/io" - libtext "github.com/shuLhan/share/lib/text" libtime "github.com/shuLhan/share/lib/time" ) @@ -429,7 +428,7 @@ func parseTTL(tok []byte, stok string) (seconds uint32, err error) { dur time.Duration ) - if libtext.IsDigits(tok) { + if libbytes.IsDigits(tok) { v, err = strconv.ParseUint(stok, 10, 32) if err != nil { return @@ -484,7 +483,7 @@ func (m *master) parseRR(prevRR *ResourceRecord, tok []byte) (*ResourceRecord, e rr.TTL = prevRR.TTL rr.Class = prevRR.Class - if libtext.IsDigit(tok[0]) { + if libbytes.IsDigit(tok[0]) { ttl, err := parseTTL(tok, stok) if err != nil { return nil, err @@ -519,7 +518,7 @@ func (m *master) parseRR(prevRR *ResourceRecord, tok []byte) (*ResourceRecord, e switch m.flag { case parseRRStart: - if libtext.IsDigit(tok[0]) { + if libbytes.IsDigit(tok[0]) { rr.TTL, err = parseTTL(tok, stok) if err != nil { return nil, err @@ -544,7 +543,7 @@ func (m *master) parseRR(prevRR *ResourceRecord, tok []byte) (*ResourceRecord, e } case parseRRClass: - if libtext.IsDigit(tok[0]) { + if libbytes.IsDigit(tok[0]) { rr.TTL, err = parseTTL(tok, stok) if err != nil { return nil, err diff --git a/lib/dns/message.go b/lib/dns/message.go index ee84182b..f786e230 100644 --- a/lib/dns/message.go +++ b/lib/dns/message.go @@ -12,7 +12,6 @@ import ( "strings" libbytes "github.com/shuLhan/share/lib/bytes" - libtext "github.com/shuLhan/share/lib/text" ) // @@ -128,7 +127,7 @@ func (msg *Message) packDomainName(dname []byte, doCompress bool) (n int) { // corresponding to the decimal number described by // DDD. The resulting octet is assumed to be text and // is not checked for special meaning. - if libtext.IsDigit(c) { + if libbytes.IsDigit(c) { if x+2 >= len(dname) { return } diff --git a/lib/io/reader.go b/lib/io/reader.go index 687e74ec..63e2e53e 100644 --- a/lib/io/reader.go +++ b/lib/io/reader.go @@ -7,7 +7,7 @@ package file import ( "io/ioutil" - libtext "github.com/shuLhan/share/lib/text" + libbytes "github.com/shuLhan/share/lib/bytes" ) // @@ -79,7 +79,7 @@ func (r *Reader) ReadUntil(seps []byte, terms []byte) (b []byte, isTerm bool, c // func (r *Reader) SkipSpace() byte { for r.p < len(r.v) { - if libtext.IsSpace(r.v[r.p]) { + if libbytes.IsSpace(r.v[r.p]) { r.p++ continue } diff --git a/lib/net/is.go b/lib/net/is.go index 2efbe475..68454552 100644 --- a/lib/net/is.go +++ b/lib/net/is.go @@ -1,7 +1,7 @@ package net import ( - libtext "github.com/shuLhan/share/lib/text" + libbytes "github.com/shuLhan/share/lib/bytes" ) // @@ -19,17 +19,17 @@ func IsHostnameValid(hname []byte) bool { if n == 0 { return false } - if !libtext.IsAlnum(hname[0]) && hname[0] != '_' { + if !libbytes.IsAlnum(hname[0]) && hname[0] != '_' { return false } - if !libtext.IsAlnum(hname[n-1]) { + if !libbytes.IsAlnum(hname[n-1]) { return false } for x := 1; x < n-1; x++ { if hname[x] == '-' || hname[x] == '_' || hname[x] == '.' { continue } - if libtext.IsAlnum(hname[x]) { + if libbytes.IsAlnum(hname[x]) { continue } return false diff --git a/lib/net/resolvconf_test.go b/lib/net/resolvconf_test.go index 92cdb3f8..29d06854 100644 --- a/lib/net/resolvconf_test.go +++ b/lib/net/resolvconf_test.go @@ -8,8 +8,8 @@ import ( "os" "testing" + libbytes "github.com/shuLhan/share/lib/bytes" "github.com/shuLhan/share/lib/test" - libtext "github.com/shuLhan/share/lib/text" ) func TestNewResolvConf(t *testing.T) { @@ -54,7 +54,7 @@ func TestResolvConf_Init(t *testing.T) { return "kilabit.info", nil } - veryLongName := string(libtext.Random([]byte(libtext.ASCIILetters), 255)) + veryLongName := string(libbytes.Random([]byte(libbytes.ASCIILetters), 255)) cases := []struct { desc string diff --git a/lib/text/string.go b/lib/text/string.go index 4e654cf4..27aa2aa3 100644 --- a/lib/text/string.go +++ b/lib/text/string.go @@ -1,5 +1,13 @@ +// 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 text +import ( + libbytes "github.com/shuLhan/share/lib/bytes" +) + // // StringJSONEscape escape the following character: `"` (quotation mark), // `\` (reverse solidus), `/` (solidus), `\b` (backspace), `\f` (formfeed), @@ -16,7 +24,7 @@ func StringJSONEscape(in string) string { } bin := []byte(in) - bout := BytesJSONEscape(bin) + bout := libbytes.JSONEscape(bin) return string(bout) } @@ -36,7 +44,7 @@ func StringJSONUnescape(in string, strict bool) (string, error) { } bin := []byte(in) - bout, err := BytesJSONUnescape(bin, strict) + bout, err := libbytes.JSONUnescape(bin, strict) out := string(bout) diff --git a/lib/text/text.go b/lib/text/text.go index 75f60bc8..2028cac6 100644 --- a/lib/text/text.go +++ b/lib/text/text.go @@ -1,21 +1,3 @@ // Package text provide a library for working with text, either as slice of // bytes or as a string. package text - -const ( - // ASCIILetters contains list of lower and upper case characters in - // ASCII. - ASCIILetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - // 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" -) - -var ( - // ASCIISpaces contains list of white spaces in ASCII. - ASCIISpaces = []byte{'\t', '\n', '\v', '\f', '\r', ' '} -) diff --git a/lib/time/duration.go b/lib/time/duration.go index 7c665a6d..18fb8d9f 100644 --- a/lib/time/duration.go +++ b/lib/time/duration.go @@ -9,8 +9,8 @@ import ( "strconv" "time" + libbytes "github.com/shuLhan/share/lib/bytes" libio "github.com/shuLhan/share/lib/io" - libtext "github.com/shuLhan/share/lib/text" ) const ( @@ -41,12 +41,12 @@ func ParseDuration(s string) (time.Duration, error) { reader.Init(s) c := reader.SkipSpace() - if !libtext.IsDigit(c) { + if !libbytes.IsDigit(c) { return 0, ErrDurationMissingValue } for { - tok, isTerm, c := reader.ReadUntil(seps, libtext.ASCIISpaces) + tok, isTerm, c := reader.ReadUntil(seps, libbytes.ASCIISpaces) if len(tok) == 0 { break } |
