Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkicoon committed Jul 28, 2024
1 parent 99eeb9a commit 93b48f1
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
file_age_test_file_test
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/nikkicoon/utility-go

go 1.22

require github.com/stretchr/testify v1.9.0
require (
github.com/phuslu/log v1.0.107
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/phuslu/log v1.0.107 h1:L6lEs2dKVgnXWapoz98YqmobxhtwPAfghUjluiSbPJ4=
github.com/phuslu/log v1.0.107/go.mod h1:F8osGJADo5qLK/0F88djWwdyoZZ9xDJQL1HYRHFEkS0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
8 changes: 8 additions & 0 deletions pkg/boolean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg

func ItoB(i int64) bool {
if i <= 0 {

Check failure on line 4 in pkg/boolean.go

View workflow job for this annotation

GitHub Actions / lint

S1008: should use 'return i > 0' instead of 'if i <= 0 { return false }; return true' (gosimple)
return false
}
return true
}
13 changes: 13 additions & 0 deletions pkg/boolean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pkg_test

import (
"github.com/nikkicoon/utility-go/pkg"
"github.com/stretchr/testify/assert"
"testing"
)

func TestItoB(t *testing.T) {
assert.True(t, pkg.ItoB(20))
assert.False(t, pkg.ItoB(0))
assert.False(t, pkg.ItoB(-1))
}
22 changes: 19 additions & 3 deletions pkg/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package pkg

import (
"fmt"
"os"
"runtime"
"time"
)

// GetCurrentFuncName returns a string holding the current
// function name.
func GetCurrentFuncName() string {
pc, _, _, _ := runtime.Caller(1)
// function name. Pass any boolean value to it to get the
// name of the function which called the function.
func GetCurrentFuncName(parent ...bool) string {
var pc uintptr
if len(parent) == 1 {
pc, _, _, _ = runtime.Caller(2)
} else {
pc, _, _, _ = runtime.Caller(1)
}
return fmt.Sprintf("%s", runtime.FuncForPC(pc).Name())

Check failure on line 20 in pkg/runtime.go

View workflow job for this annotation

GitHub Actions / lint

S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)
}

func TrackExecutionTime(pre time.Time, msg ...string) {
if len(msg) == 0 {
_, _ = fmt.Fprintf(os.Stderr, "%s\texecution time of %s: %s\n", time.Now(), GetCurrentFuncName(true), time.Since(pre).String())
} else {
_, _ = fmt.Fprintf(os.Stderr, "%s\texecution time of %s (%s): %s\n", time.Now(), GetCurrentFuncName(true), msg[0], time.Since(pre).String())
}
}
19 changes: 19 additions & 0 deletions pkg/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ func DuplicateElements[T cmp.Ordered](ts []T) []T {
}
return res
}

func PrependInsertReversed[T cmp.Ordered](ts []T, t ...T) []T {
var null T
if len(t) > 0 {
for _, v := range t {
ts = append(ts, null)
copy(ts[1:], ts)
ts[0] = v
}
}
return ts
}

func PrependInsertSliced[T cmp.Ordered](ts []T, t ...T) []T {
if len(t) > 0 {
ts = append(t, ts...)
}
return ts
}
29 changes: 29 additions & 0 deletions pkg/slices_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg_test

import (
"fmt"
"github.com/nikkicoon/utility-go/pkg"
"github.com/stretchr/testify/assert"
"testing"
Expand All @@ -13,3 +14,31 @@ func TestDuplicateElements(t *testing.T) {
assert.Len(t, out, 3)
assert.Equal(t, 1, out[0])
}

func TestPrependInsertReversed(t *testing.T) {
x := []int{9, 4, 1, 5}
res := pkg.PrependInsertReversed(x, 8, 7, 6)
fmt.Println(res)
assert.Len(t, res, 7)
}

func TestPrependInsertSliced(t *testing.T) {
x := []int{9, 4, 1, 5}
res := pkg.PrependInsertSliced(x, 8, 7, 6)
fmt.Println(res)
assert.Len(t, res, 7)
}

func TestPrependInsertSliced2(t *testing.T) {
x := []int{9, 4, 1, 5}
res := pkg.PrependInsertSliced(x, 1)
fmt.Println(res)
assert.Len(t, res, 5)
}

func TestPrependInsertSliced3(t *testing.T) {
x := []int{9, 4, 1, 5}
res := pkg.PrependInsertSliced(x)
fmt.Println(res)
assert.Len(t, res, 4)
}
10 changes: 4 additions & 6 deletions pkg/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,19 @@ func NewLDIFRegexps() LDIFRegexps {
// DissolveEmptyValues removes all empty value lines in `s`.
// This is a work-around for https://github.com/go-ldap/ldif/issues/21.
func (l LDIFRegexps) DissolveEmptyValues(s string, t ...bool) string {
start := time.Now()
if len(t) > 0 {
if t[1] {
defer TrackExecutionTime(time.Now())
}
defer TrackExecutionTime(start)
}
return l.EmptyValue.ReplaceAllString(s, "")
}

// DissolveDoubleColon replaces all `t::` with `t:` in `s`.
// This is a work-around for https://github.com/go-ldap/ldif/issues/23.
func (l LDIFRegexps) DissolveDoubleColon(s string, t ...bool) string {
start := time.Now()
if len(t) > 0 {
if t[1] {
defer TrackExecutionTime(time.Now())
}
defer TrackExecutionTime(start)
}
return l.DoubleColon.ReplaceAllString(s, "$1")
}
Expand Down

0 comments on commit 93b48f1

Please sign in to comment.