aboutsummaryrefslogtreecommitdiff
path: root/lib/text
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2018-09-03 22:27:44 +0700
committerShulhan <ms@kilabit.info>2018-09-03 22:55:06 +0700
commitd3e6c2e902eb3a00c2dfdcde09bf35282ea63396 (patch)
tree63432c09e0dddb08942e7172f24666a811432bb5 /lib/text
parentaab053ef5c49bcc67f6de5477d9760ea4e3bf6ab (diff)
downloadpakakeh.go-d3e6c2e902eb3a00c2dfdcde09bf35282ea63396.tar.xz
Move all byte(s) related constant and functions from package text to bytes
Diffstat (limited to 'lib/text')
-rw-r--r--lib/text/bytes.go182
-rw-r--r--lib/text/is.go66
-rw-r--r--lib/text/string.go12
-rw-r--r--lib/text/text.go18
4 files changed, 10 insertions, 268 deletions
diff --git a/lib/text/bytes.go b/lib/text/bytes.go
deleted file mode 100644
index 8b9a1e48..00000000
--- a/lib/text/bytes.go
+++ /dev/null
@@ -1,182 +0,0 @@
-package text
-
-import (
- "bytes"
- "fmt"
- "math/rand"
- "strconv"
-)
-
-const (
- errInvalidSyntax = "%s: invalid syntax at %d"
-)
-
-const (
- bDoubleQuote = '"'
- bRevSolidus = '\\'
- bSolidus = '/'
- bBackspace = '\b'
- bFormFeed = '\f'
- bLineFeed = '\n'
- bCarReturn = '\r'
- bTab = '\t'
-)
-
-//
-// BytesJSONEscape 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.
-//
-// References:
-//
-// * https://tools.ietf.org/html/rfc7159#page-8
-//
-func BytesJSONEscape(in []byte) []byte {
- var buf bytes.Buffer
-
- for x := 0; x < len(in); x++ {
- if in[x] == bDoubleQuote || in[x] == bRevSolidus || in[x] == bSolidus {
- buf.WriteByte(bRevSolidus)
- buf.WriteByte(in[x])
- continue
- }
- if in[x] == bBackspace {
- buf.WriteByte(bRevSolidus)
- buf.WriteByte('b')
- continue
- }
- if in[x] == bFormFeed {
- buf.WriteByte(bRevSolidus)
- buf.WriteByte('f')
- continue
- }
- if in[x] == bLineFeed {
- buf.WriteByte(bRevSolidus)
- buf.WriteByte('n')
- continue
- }
- if in[x] == bCarReturn {
- buf.WriteByte(bRevSolidus)
- buf.WriteByte('r')
- continue
- }
- if in[x] == bTab {
- buf.WriteByte(bRevSolidus)
- buf.WriteByte('t')
- continue
- }
- if in[x] <= 31 {
- buf.WriteString(fmt.Sprintf("\\u%04X", in[x]))
- continue
- }
-
- buf.WriteByte(in[x])
- }
-
- return buf.Bytes()
-}
-
-//
-// BytesJSONUnescape 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) {
- var (
- buf bytes.Buffer
- uni bytes.Buffer
- esc bool
- )
-
- for x := 0; x < len(in); x++ {
- if esc {
- if in[x] == 'u' {
- uni.Reset()
- x++
-
- for y := 0; y < 4 && x < len(in); x++ {
- uni.WriteByte(in[x])
- y++
- }
-
- dec, err := strconv.ParseUint(uni.String(), 16, 32)
- if err != nil {
- return nil, err
- }
-
- if dec <= 31 {
- buf.WriteByte(byte(dec))
- } else {
- buf.WriteRune(rune(dec))
- }
-
- esc = false
- x--
- continue
- }
- if in[x] == 't' {
- buf.WriteByte(bTab)
- esc = false
- continue
- }
- if in[x] == 'r' {
- buf.WriteByte(bCarReturn)
- esc = false
- continue
- }
- if in[x] == 'n' {
- buf.WriteByte(bLineFeed)
- esc = false
- continue
- }
- if in[x] == 'f' {
- buf.WriteByte(bFormFeed)
- esc = false
- continue
- }
- if in[x] == 'b' {
- buf.WriteByte(bBackspace)
- esc = false
- continue
- }
- if in[x] == bDoubleQuote || in[x] == bRevSolidus || in[x] == bSolidus {
- buf.WriteByte(in[x])
- esc = false
- continue
- }
-
- if strict {
- err := fmt.Errorf(errInvalidSyntax, "BytesJSONUnescape", x)
- return nil, err
- }
-
- buf.WriteByte(in[x])
- esc = false
- continue
- }
- if in[x] == bRevSolidus {
- esc = true
- continue
- }
- buf.WriteByte(in[x])
- }
-
- 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/text/is.go b/lib/text/is.go
deleted file mode 100644
index 21829272..00000000
--- a/lib/text/is.go
+++ /dev/null
@@ -1,66 +0,0 @@
-package text
-
-//
-// 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/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', ' '}
-)