Skip to content

Commit

Permalink
Merge pull request #1 from wegoteam/dev
Browse files Browse the repository at this point in the history
发布版本[v1.0.0]
  • Loading branch information
wego-xuchang committed Apr 8, 2023
2 parents 5b77357 + ec4f75d commit e2d4f5c
Show file tree
Hide file tree
Showing 17 changed files with 3,806 additions and 4 deletions.
41 changes: 40 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,50 @@
*.so
*.dylib

# Test binary, built with `go test -c`
# Test binconv, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
go.sum
go.work.sum

./vendor/
vendor/
# IDEA #
.idea/
*.iml
.DS_Store

# Log #
logs/
*.log

node_modules/
dist/


# local env files
.env.local
.env.*.local

# lock file
-lock.*
*.lock

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Editor directories and files
.vscode/
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# wepkg
golang的工具库

- bean属性拷贝
- 属性复制
- 分布式雪花算法
- redis库
- 常用的工具集
- 缓存(redis)
- 字符
- 文件
- json
- 时间日期
- 条件表达式
- 协程池
- 切片
...
212 changes: 212 additions & 0 deletions conv/arrayconv/array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package arrayconv

import (
"sort"
)

type ArrayList []interface{}

// 添加, 如果存在, 不处理
func (arr *ArrayList) Add(items ...interface{}) {
for _, item := range items {
if arr.Contains(item) {
return
}
arr.Push(item)
}
}

func (arr *ArrayList) At(index int) interface{} {
return (*arr)[index]
}

func (arr *ArrayList) Clear() {
*arr = (*arr)[:0]
}

func (arr *ArrayList) Contains(item interface{}) bool {
return arr.IndexOf(item) != -1
}

func (arr *ArrayList) ContainsCond(cond func(item interface{}) bool) bool {
return arr.IndexOf(cond) != -1
}

// 每一个项都符合条件就返回true
func (arr *ArrayList) Every(cond func(item interface{}) bool) bool {
s := *arr
for i := 0; i < len(s); i++ {
val := s[i]
if cond(val) == false {
return false
}
}
return true
}

func (arr *ArrayList) First(cond func(item interface{}) bool) (val interface{}, has bool) {
s := *arr
for i := 0; i < len(s); i++ {
val := s[i]
if cond(val) {
return val, true
}
}
return nil, false
}

func (arr *ArrayList) Filter(cond func(index int, elem interface{}) bool) (r ArrayList) {
for i, x := range *arr {
if cond(i, x) {
r = append(r, x)
}
}
return r
}

func (arr *ArrayList) ForRange(handler func(item interface{})) {
for _, x := range *arr {
handler(x)
}
}

func (arr *ArrayList) IndexOfConf(cond func(item interface{}) bool) int {
s := *arr
for i := 0; i < len(s); i++ {
if cond(s[i]) {
return i
}
}
return -1
}

func (arr *ArrayList) IndexOf(item interface{}) int {
s := *arr
for i := 0; i < len(s); i++ {
if s[i] == item {
return i
}
}
return -1
}

func (arr *ArrayList) Last(cond func(item interface{}) bool) interface{} {
s := *arr
for i := len(s) - 1; i >= 0; i-- {
val := s[i]
if cond(val) {
return val
}
}
return nil
}

func (arr *ArrayList) Length() int {
return len(*arr)
}

func (arr *ArrayList) Pop() interface{} {
s := *arr
last := s[len(s)-1]
s[len(s)-1] = nil // GC
s2 := s[:len(s)-1]
*arr = s2

return last
}

func (arr *ArrayList) Push(item interface{}) {
s := *arr
s = append(s, item)
*arr = s
}

func (arr *ArrayList) PushList(list ArrayList) {
s := *arr
s = append(s, list...)
*arr = s
}

func (arr *ArrayList) Remove(item interface{}) {
i := arr.IndexOf(item)
if i != -1 {
arr.RemoveAt(i)
}
}

func (arr *ArrayList) RemoveAt(i int) {
s := *arr
copy(s[i:], s[i+1:])
s[len(s)-1] = nil // GC
s2 := s[:len(s)-1]
*arr = s2
}

func (arr *ArrayList) Replace(i int, item interface{}) {
s := *arr
over := i - len(s)
if over > -1 {
ss := make([]interface{}, i+1)
copy(ss[0:], s[:])
s = ss
}
s[i] = item
*arr = s
}

func (arr *ArrayList) Reverse() {
for i := len(*arr)/2 - 1; i >= 0; i-- {
opp := len(*arr) - 1 - i
(*arr)[i], (*arr)[opp] = (*arr)[opp], (*arr)[i]
}
}

func (arr *ArrayList) Shift() interface{} {
s := *arr
top := s[0]
s[0] = nil // GC
s2 := s[1:]
*arr = s2

return top
}

func (arr *ArrayList) Slice() []interface{} {
return []interface{}(*arr)
}

func (arr *ArrayList) Sort(compare func(a, b interface{}) int) {
l := *arr
sort.Slice(l, func(i, j int) bool {
return compare(l[i], l[j]) >= 0
})
}

func (arr *ArrayList) Unshift(item interface{}) {
s := *arr
l := len(s) + 1
ss := make([]interface{}, l, l)
ss[0] = item
copy(ss[1:], s[:])
*arr = ss
}

// 去重操作, 返回去重后的数组
func (arr *ArrayList) Unique(getKey func(a interface{}) string) (r ArrayList) {
l := *arr
m := map[string]interface{}{} // 存放不重复主键
for _, e := range l {
length := len(m)
m[getKey(e)] = 0
if len(m) != length { // 加入map后,map长度变化,则元素不重复
r = append(r, e)
}
}
return
}

// 并集
func (arr *ArrayList) Union(a ArrayList, getKey func(a interface{}) string) ArrayList {
arr.PushList(a)
return arr.Unique(getKey)
}
107 changes: 107 additions & 0 deletions conv/binconv/binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package binconv

import (
"encoding/binary"
"io"
)

func WriteBytes(w io.Writer, order binary.ByteOrder, data []byte) {
binary.Write(w, order, int16(len(data)))
binary.Write(w, order, data)

}

func WriteUTF(w io.Writer, order binary.ByteOrder, data string) (err error) {
err = binary.Write(w, order, int16(len(data)))
if err != nil {
return
}
err = binary.Write(w, order, []byte(data))
if err != nil {
return
}
return
}

func ReadBool(r io.Reader, order binary.ByteOrder) bool {
var val bool
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadInt8(r io.Reader, order binary.ByteOrder) int8 {
var val int8
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadInt16(r io.Reader, order binary.ByteOrder) int16 {
var val int16
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadInt32(r io.Reader, order binary.ByteOrder) int32 {
var val int32
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadInt64(r io.Reader, order binary.ByteOrder) int64 {
var val int64
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadUint8(r io.Reader, order binary.ByteOrder) uint8 {
var val uint8
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadUint16(r io.Reader, order binary.ByteOrder) uint16 {
var val uint16
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadUint32(r io.Reader, order binary.ByteOrder) uint32 {
var val uint32
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadUint64(r io.Reader, order binary.ByteOrder) uint64 {
var val uint64
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadFloat32(r io.Reader, order binary.ByteOrder) float32 {
var val float32
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadFloat64(r io.Reader, order binary.ByteOrder) float64 {
var val float64
binary.Read(r, binary.BigEndian, &val)
return val
}

func ReadBytes(r io.Reader, order binary.ByteOrder) (data []byte) {
var tlen int16
binary.Read(r, order, &tlen)
data = make([]byte, tlen)
binary.Read(r, order, &data)
return
}

func ReadUTF(r io.Reader, order binary.ByteOrder) (data string) {
tlen := int16(0)
binary.Read(r, order, &tlen)
tbs := make([]byte, tlen)
binary.Read(r, order, &tbs)
data = string(tbs)
return
}
Loading

0 comments on commit e2d4f5c

Please sign in to comment.