Skip to content
This repository has been archived by the owner on Jan 8, 2019. It is now read-only.

Commit

Permalink
encoding, utils/encodeio: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cosiner committed Nov 8, 2015
1 parent f8f57db commit 9e47a8e
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 318 deletions.
67 changes: 38 additions & 29 deletions encoding/codec.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
package encoding

import "encoding/json"
import (
"encoding/json"
"io"
)

type UnmarshalFunc func([]byte, interface{}) error

func (u UnmarshalFunc) Unmarshal(b []byte, v interface{}) error {
return u(b, v)
type Codec interface {
Encode(io.Writer, interface{}) error
Marshal(interface{}) ([]byte, error)
Decode(io.Reader, interface{}) error
Unmarshal([]byte, interface{}) error
Pool([]byte)
}

type MarshalFunc func(interface{}) ([]byte, error)
type JSON struct{}

func (m MarshalFunc) Marshal(v interface{}) ([]byte, error) {
return m(v)
func (JSON) Encode(w io.Writer, v interface{}) error {
return json.NewEncoder(w).Encode(v)
}

type PoolFunc func([]byte)
func (JSON) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

func (p PoolFunc) Pool(b []byte) {
p(b)
func (JSON) Decode(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}

type Codec interface {
Unmarshal([]byte, interface{}) error
Marshal(interface{}) ([]byte, error)
Pool([]byte)
func (JSON) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}

func (JSON) Pool([]byte) {}

var DefaultCodec Codec = JSON{}

func Encode(w io.Writer, v interface{}) error {
return DefaultCodec.Encode(w, v)
}

type funcCodec struct {
UnmarshalFunc
MarshalFunc
PoolFunc
func Marshal(v interface{}) ([]byte, error) {
return DefaultCodec.Marshal(v)
}

func NewFuncCodec(unmarshal UnmarshalFunc, marshal MarshalFunc, pool PoolFunc) Codec {
if pool == nil {
pool = func([]byte) {}
}
return funcCodec{
UnmarshalFunc: unmarshal,
MarshalFunc: marshal,
PoolFunc: pool,
}
func Decode(r io.Reader, v interface{}) error {
return DefaultCodec.Decode(r, v)
}

var JSON = NewFuncCodec(json.Unmarshal, json.Marshal, nil)
func Unmarshal(data []byte, v interface{}) error {
return DefaultCodec.Unmarshal(data, v)
}
func Pool(data []byte) {
DefaultCodec.Pool(data)
}
89 changes: 67 additions & 22 deletions encoding/encoding.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
package encoding

import (
"bytes"
"compress/gzip"
"compress/zlib"
"encoding/base64"
"encoding/hex"
"io"
"io/ioutil"
)

var (
HEX = Hex{}
Base64Std = &Base64{
Encoding: base64.StdEncoding,
}
Base64URL = &Base64{
Encoding: base64.URLEncoding,
}
Gzip = Compress{
BufSize: 64,
NewReader: func(r io.Reader) (io.ReadCloser, error) {
rd, err := gzip.NewReader(r)
return rd, err
},
NewWriter: func(w io.Writer) io.WriteCloser {
return gzip.NewWriter(w)
},
}
Zlib = Compress{
BufSize: 64,
NewReader: func(r io.Reader) (io.ReadCloser, error) {
rd, err := zlib.NewReader(r)
return rd, err
},
NewWriter: func(w io.Writer) io.WriteCloser {
return zlib.NewWriter(w)
},
}
)

type Encoding interface {
Expand All @@ -13,21 +48,13 @@ type Encoding interface {
type Hex struct{}

func (Hex) Encode(src []byte) []byte {
return HexEncode(src)
}

func (Hex) Decode(src []byte) ([]byte, error) {
return HexDecode(src)
}

func HexEncode(src []byte) []byte {
l := len(src)
dst := make([]byte, hex.EncodedLen(l))
l = hex.Encode(dst, src)
return dst[:l]
}

func HexDecode(src []byte) ([]byte, error) {
func (Hex) Decode(src []byte) ([]byte, error) {
l := len(src)
dst := make([]byte, hex.DecodedLen(l))
l, err := hex.Decode(dst, src)
Expand All @@ -43,29 +70,47 @@ type Base64 struct {
}

func (b *Base64) Encode(src []byte) []byte {
return Base64Encode(b.Encoding, src)
l := len(src)
dst := make([]byte, b.Encoding.EncodedLen(l))
b.Encoding.Encode(dst, src)
return dst
}

func (b *Base64) Decode(src []byte) ([]byte, error) {
return Base64Decode(b.Encoding, src)
l := len(src)
dst := make([]byte, b.Encoding.DecodedLen(l))
l, err := b.Encoding.Decode(dst, src)
if err != nil {
return nil, err
}

return dst[:l], nil
}

func Base64Encode(enc *base64.Encoding, src []byte) []byte {
l := len(src)
dst := make([]byte, enc.EncodedLen(l))
enc.Encode(dst, src)
return dst
type Compress struct {
BufSize int
NewWriter func(io.Writer) io.WriteCloser
NewReader func(io.Reader) (io.ReadCloser, error)
}

func Base64Decode(enc *base64.Encoding, src []byte) ([]byte, error) {
l := len(src)
dst := make([]byte, enc.DecodedLen(l))
l, err := enc.Decode(dst, src)
func (c Compress) Encode(src []byte) []byte {
buf := bytes.NewBuffer(make([]byte, 0, c.BufSize))
w := c.NewWriter(buf)
w.Write(src)
w.Close()

return buf.Bytes()
}

func (c Compress) Decode(src []byte) ([]byte, error) {
r, err := c.NewReader(bytes.NewReader(src))
if err != nil {
return nil, err
}
dst, err := ioutil.ReadAll(r)
r.Close()

return dst[:l], nil
return dst, err
}

type Pipe []Encoding
Expand All @@ -90,7 +135,7 @@ func (p Pipe) Decode(src []byte) ([]byte, error) {
}

func (p Pipe) Prepend(encs ...Encoding) Pipe {
newP := make(Pipe, len(p) + len(encs))
newP := make(Pipe, len(p)+len(encs))
copy(newP, encs)
copy(newP[len(encs):], p)
return newP
Expand Down
8 changes: 8 additions & 0 deletions math2/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ func SegmentIndex(segs []int, distance int) (index, indexUsed, remains int) {
remains = sum - distance
return
}

func Round(f float64) int64 {
i := int64(f)
if f-float64(i) >= 0.5 {
i += 1
}
return i
}
22 changes: 11 additions & 11 deletions strings2/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,29 @@ func TrimAfter(s, delimiter string) string {
return strings.TrimSpace(s)
}

func TrimBefore(s, delimeter string) string {
if idx := strings.Index(s, delimeter); idx >= 0 {
s = s[idx+len(delimeter):]
func TrimBefore(s, delimiter string) string {
if idx := strings.Index(s, delimiter); idx >= 0 {
s = s[idx+len(delimiter):]
}

return strings.TrimSpace(s)
}

// IndexN find index of n-th sep string
func IndexN(str, sep string, n int) (index int) {
index, idx, seplen := 0, -1, len(sep)
index, idx, sepLen := 0, -1, len(sep)
for i := 0; i < n; i++ {
if idx = strings.Index(str, sep); idx == -1 {
break
}
str = str[idx+seplen:]
str = str[idx+sepLen:]
index += idx
}

if idx == -1 {
index = -1
} else {
index += (n - 1) * seplen
index += (n - 1) * sepLen
}

return
Expand All @@ -119,9 +119,9 @@ func LastIndexN(str, sep string, n int) (index int) {
return
}

// Seperate string by seperator, the seperator must in the middle of string,
// Separate string by separator, the separator must in the middle of string,
// not first and last
func Seperate(s string, sep byte) (string, string) {
func Separate(s string, sep byte) (string, string) {
i := MidIndex(s, sep)
if i < 0 {
return "", ""
Expand Down Expand Up @@ -150,7 +150,7 @@ func IsAllCharsIn(s, encoding string) bool {
return is
}

// MidIndex find middle seperator index of string, not first and last
// MidIndex find middle separator index of string, not first and last
func MidIndex(s string, sep byte) int {
i := strings.IndexByte(s, sep)
if i <= 0 || i == len(s)-1 {
Expand All @@ -174,7 +174,7 @@ func RemoveSpace(s string) string {
return string(bs[:idx])
}

// MergeSpace merge mutiple space to one, trim determin whether remove space at prefix and suffix
// MergeSpace merge multiple space to one, trim determine whether remove space at prefix and suffix
func MergeSpace(s string, trim bool) string {
space := false

Expand Down Expand Up @@ -226,7 +226,7 @@ func LastIndexNonSpace(s string) int {
return -1
}

// WriteStringsToBuffer write strings to buffer, it avoid memory allocaton of join
// WriteStringsToBuffer write strings to buffer, it avoid memory allocation of join
// strings
func WriteStringsToBuffer(buffer *bytes.Buffer, strings []string, sep string) {
i, last := 0, len(strings)-1
Expand Down
2 changes: 1 addition & 1 deletion strings2/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestMidSep(t *testing.T) {
Expect("", "").Arg("123", byte('3')).
Expect("", "").Arg("123", byte('4')).
Expect("1", "3").Arg("123", byte('2')).
Run(t, Seperate)
Run(t, Separate)
}

func TestWriteBuffer(t *testing.T) {
Expand Down
Loading

0 comments on commit 9e47a8e

Please sign in to comment.