Skip to content

Commit

Permalink
formart
Browse files Browse the repository at this point in the history
  • Loading branch information
vlorc committed Sep 11, 2018
1 parent 1247c11 commit 5d05cb8
Show file tree
Hide file tree
Showing 36 changed files with 460 additions and 464 deletions.
14 changes: 7 additions & 7 deletions base/bit.go
Expand Up @@ -2,26 +2,26 @@ package base

type BitFactory struct{}

func(BitFactory)Left(v,r uint64) uint64 {
func (BitFactory) Left(v, r uint64) uint64 {
return v << r
}

func(BitFactory)Right(v,r uint64) uint64 {
func (BitFactory) Right(v, r uint64) uint64 {
return v >> r
}

func(BitFactory)Xor(v,r uint64) uint64 {
func (BitFactory) Xor(v, r uint64) uint64 {
return v ^ r
}

func(BitFactory)And(v,r uint64) uint64 {
func (BitFactory) And(v, r uint64) uint64 {
return v & r
}

func(BitFactory)Or(v,r uint64) uint64 {
func (BitFactory) Or(v, r uint64) uint64 {
return v | r
}

func(BitFactory)Not(v uint64) uint64 {
func (BitFactory) Not(v uint64) uint64 {
return ^v
}
}
10 changes: 5 additions & 5 deletions base/buffer.go
Expand Up @@ -14,7 +14,7 @@ func (f BufferFactory) New(length int) Buffer {
}

func (BufferFactory) Alloc(length int, args ...int) Buffer {
return __allocBuffer(length,args...)
return __allocBuffer(length, args...)
}

func (BufferFactory) Form(str string) Buffer {
Expand Down Expand Up @@ -89,7 +89,7 @@ func (b Buffer) Concat(src ...Buffer) Buffer {
for _, v := range src {
i += len(v)
}
dst := make(Buffer, i + len(b))
dst := make(Buffer, i+len(b))
i = len(b)
if i > 0 {
copy(dst, b)
Expand Down Expand Up @@ -124,7 +124,7 @@ func (b Buffer) ToString(args ...string) string {
func (b Buffer) ToNumber(args ...int) (v int64) {
n := b.Slice(args...)
for i := len(n) - 1; i >= 0; i-- {
v = (v<<8) + int64(n[i])
v = (v << 8) + int64(n[i])
}
return v
}
Expand All @@ -134,9 +134,9 @@ func (b Buffer) ToLine(args ...int) string {
if len(n) <= 0 {
return ""
}
pos := bytes.IndexByte(n,byte('\n'))
pos := bytes.IndexByte(n, byte('\n'))
if pos > 0 {
return __rawString(n[:pos])
}
return ""
}
}
24 changes: 12 additions & 12 deletions base/encode.go
Expand Up @@ -5,25 +5,25 @@ import (
"unsafe"
)

var EncodeTable = map[string]func(Buffer) string {
"raw": __rawString,
"HEX": __hexUpperString,
"hex": __hexLowerString,
var EncodeTable = map[string]func(Buffer) string{
"raw": __rawString,
"HEX": __hexUpperString,
"hex": __hexLowerString,
"base64": __base64String,
"gbk": __gbkString,
"gbk": __gbkString,
}

func __hexUpperString(src Buffer) string {
return __hexString(src,"0123456789ABCDEF")
return __hexString(src, "0123456789ABCDEF")
}
func __hexLowerString(src Buffer) string {
return __hexString(src,"0123456789abcdef")
return __hexString(src, "0123456789abcdef")
}
func __hexString(src Buffer,tab string) string {
dst := make([]byte, len(src) * 2)
for i,v := range src {
dst[i * 2 + 0] = tab[v >> 4]
dst[i * 2 + 1] = tab[v & 15]
func __hexString(src Buffer, tab string) string {
dst := make([]byte, len(src)*2)
for i, v := range src {
dst[i*2+0] = tab[v>>4]
dst[i*2+1] = tab[v&15]
}
return *(*string)(unsafe.Pointer(&dst))
}
Expand Down
62 changes: 31 additions & 31 deletions base/time.go
Expand Up @@ -2,98 +2,98 @@ package base

import "time"

type TimeFactory struct {}
type TimeFactory struct{}
type Timer time.Timer
type Ticker time.Ticker

func(TimeFactory)Now() time.Time{
func (TimeFactory) Now() time.Time {
return time.Now()
}

func Duration(ms int) time.Duration{
func Duration(ms int) time.Duration {
return time.Duration(ms) * time.Millisecond
}

func(TimeFactory)Unix() int64{
func (TimeFactory) Unix() int64 {
return time.Now().Unix()
}

func(TimeFactory)Zero() time.Time{
func (TimeFactory) Zero() time.Time {
return time.Time{}
}

func(TimeFactory)UnixNano() int64{
func (TimeFactory) UnixNano() int64 {
return time.Now().UnixNano()
}

func(TimeFactory)Time(args ...int) time.Time{
func (TimeFactory) Time(args ...int) time.Time {
var val = [4]int{}
if len(args) > 4 {
args = args[:4]
}
for i,v := range args {
for i, v := range args {
val[i] = v
}
y,m,d := time.Now().Date()
return time.Date(y,m,d,val[0],val[1],val[2],val[3],time.Local)
y, m, d := time.Now().Date()
return time.Date(y, m, d, val[0], val[1], val[2], val[3], time.Local)
}

func(TimeFactory)Date(args ...int) time.Time{
var val = [7]int{0,1,1}
func (TimeFactory) Date(args ...int) time.Time {
var val = [7]int{0, 1, 1}
if len(args) > 7 {
args = args[:7]
}
for i,v := range args {
for i, v := range args {
val[i] = v
}
return time.Date(val[0],time.Month(val[1]),val[2],val[3],val[4],val[5],val[6],time.Local)
return time.Date(val[0], time.Month(val[1]), val[2], val[3], val[4], val[5], val[6], time.Local)
}

func(TimeFactory)Format(layout string) string{
func (TimeFactory) Format(layout string) string {
return time.Now().Format(layout)
}

func(TimeFactory)Parse(layout,value string) (time.Time,error){
return time.Parse(layout,value)
func (TimeFactory) Parse(layout, value string) (time.Time, error) {
return time.Parse(layout, value)
}

func(TimeFactory)After(s int,callback func()) *Timer{
return (*Timer)(time.AfterFunc(Duration(s),callback))
func (TimeFactory) After(s int, callback func()) *Timer {
return (*Timer)(time.AfterFunc(Duration(s), callback))
}

func(TimeFactory)Sleep(s int) {
func (TimeFactory) Sleep(s int) {
time.Sleep(Duration(s))
}

func(TimeFactory)Timer(s int) *Timer{
func (TimeFactory) Timer(s int) *Timer {
return (*Timer)(time.NewTimer(Duration(s)))
}

func(TimeFactory)Ticker(s int) *Ticker{
func (TimeFactory) Ticker(s int) *Ticker {
return (*Ticker)(time.NewTicker(Duration(s)))
}

func(t *Timer)Wait() bool{
_,ok := <- t.C
func (t *Timer) Wait() bool {
_, ok := <-t.C
return ok
}
func(t *Timer)Close() {
func (t *Timer) Close() {
t.Stop()
}
func(t *Timer)Stop() bool{
func (t *Timer) Stop() bool {
return (*time.Timer)(t).Stop()
}
func(t *Timer)Reset(s int) bool{
func (t *Timer) Reset(s int) bool {
return (*time.Timer)(t).Reset(Duration(s))
}

func(t *Ticker)Wait() bool{
_,ok := <- t.C
func (t *Ticker) Wait() bool {
_, ok := <-t.C
return ok
}
func(t *Ticker)Close() {
func (t *Ticker) Close() {
t.Stop()
}
func(t *Ticker)Stop(){
func (t *Ticker) Stop() {
(*time.Ticker)(t).Stop()
}
10 changes: 5 additions & 5 deletions base/tobuffer.go
Expand Up @@ -9,7 +9,7 @@ import (

func ToBuffer(L *lua.LState) int {
buf := __toBuffer(L)
L.Push(luar.New(L,buf))
L.Push(luar.New(L, buf))
return 1
}

Expand All @@ -21,12 +21,12 @@ func __toBuffer(L *lua.LState) Buffer {
return nil
}

func __newBuffer(v lua.LValue) Buffer {
func __newBuffer(v lua.LValue) Buffer {
switch v.Type() {
case lua.LTString:
__stringBuffer(string(v.(lua.LString)))
case lua.LTNumber:
__allocBuffer(int(v.(lua.LNumber)),0)
__allocBuffer(int(v.(lua.LNumber)), 0)
case lua.LTUserData:
__dataBuffer(v.(*lua.LUserData).Value)
}
Expand All @@ -35,7 +35,7 @@ func __newBuffer(v lua.LValue) Buffer {

func __dataBuffer(v interface{}) Buffer {
reflect.TypeOf(v)
switch r := v.(type) {
switch r := v.(type) {
case []byte:
return Buffer(r)
}
Expand Down Expand Up @@ -75,4 +75,4 @@ func __intBuffer(val ...int) Buffer {
buf[i] = byte(v)
}
return buf
}
}
40 changes: 20 additions & 20 deletions crypto/hash/hash.go
Expand Up @@ -5,50 +5,50 @@ import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"github.com/vlorc/lua-vm/base"
"hash"
)

type SHA1Factory struct {}
type SHA256Factory struct {}
type SHA512Factory struct {}
type MD5Factory struct {}
type SHA1Factory struct{}
type SHA256Factory struct{}
type SHA512Factory struct{}
type MD5Factory struct{}

func __sum(h hash.Hash,buf ...base.Buffer) base.Buffer{
for _,v := range buf {
func __sum(h hash.Hash, buf ...base.Buffer) base.Buffer {
for _, v := range buf {
h.Write(v)
}
return base.Buffer(h.Sum(nil))
}

func(SHA1Factory)New() hash.Hash{
func (SHA1Factory) New() hash.Hash {
return sha1.New()
}

func(SHA1Factory)Sum(buf ...base.Buffer) base.Buffer{
return __sum(sha1.New(),buf...)
func (SHA1Factory) Sum(buf ...base.Buffer) base.Buffer {
return __sum(sha1.New(), buf...)
}

func(MD5Factory)New() hash.Hash{
func (MD5Factory) New() hash.Hash {
return md5.New()
}

func(MD5Factory)Sum(buf ...base.Buffer) base.Buffer{
return __sum(md5.New(),buf...)
func (MD5Factory) Sum(buf ...base.Buffer) base.Buffer {
return __sum(md5.New(), buf...)
}

func(SHA256Factory)New() hash.Hash{
func (SHA256Factory) New() hash.Hash {
return sha256.New()
}

func(SHA256Factory)Sum(buf ...base.Buffer) base.Buffer{
return __sum(sha256.New(),buf...)
func (SHA256Factory) Sum(buf ...base.Buffer) base.Buffer {
return __sum(sha256.New(), buf...)
}

func(SHA512Factory)New() hash.Hash{
func (SHA512Factory) New() hash.Hash {
return sha512.New()
}

func(SHA512Factory)Sum(buf ...base.Buffer) base.Buffer{
return __sum(sha512.New(),buf...)
}
func (SHA512Factory) Sum(buf ...base.Buffer) base.Buffer {
return __sum(sha512.New(), buf...)
}
6 changes: 3 additions & 3 deletions crypto/rand/rand.go
@@ -1,9 +1,9 @@
package rand

import (
"github.com/vlorc/lua-vm/base"
"math/rand"
"time"
"github.com/vlorc/lua-vm/base"
)

type RandFactory struct{}
Expand All @@ -14,11 +14,11 @@ func (RandFactory) New(seed int64) *rand.Rand {
return rand.New(rand.NewSource(seed))
}

func (RandFactory) Shuffle(n int, swap func(i, j int)){
func (RandFactory) Shuffle(n int, swap func(i, j int)) {
__rand.Shuffle(n, swap)
}

func (RandFactory) Read(buf base.Buffer) (int,error) {
func (RandFactory) Read(buf base.Buffer) (int, error) {
return __rand.Read(buf)
}

Expand Down
4 changes: 2 additions & 2 deletions fs/define.go
Expand Up @@ -8,10 +8,10 @@ type FileDriver interface {
}

type FileSystem interface {
Open(file string) (FileDriver,error)
Open(file string) (FileDriver, error)
/*Create(file string,mode int) (File,error)
Remove(src,dst string) error
Delete(file string) error
MkDir(file string) error
MkTmp(file string) (string,error)*/
}
}

0 comments on commit 5d05cb8

Please sign in to comment.