Skip to content

Commit

Permalink
Add Steps
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed Dec 6, 2024
1 parent a58bc50 commit 86d2544
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
29 changes: 29 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,42 @@ import (
"encoding/hex"
"encoding/json"
"io"
"math"
"regexp"
"strings"
"time"

"github.com/hashicorp/go-version"
)

type Step struct {
Head int
Tail int
}

// Steps calculates the steps.
// example:
//
// arr := []int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
// for _, step := range yiigo.Steps(len(arr), 6) {
// cur := arr[step.Head:step.Tail]
// // todo: do something
// }
func Steps(total, step int) (steps []Step) {
steps = make([]Step, 0, int(math.Ceil(float64(total)/float64(step))))
for i := 0; i < total; i++ {
if i%step == 0 {
head := i
tail := head + step
if tail > total {
tail = total
}
steps = append(steps, Step{Head: head, Tail: tail})
}
}
return steps
}

// Nonce 生成随机串(size应为偶数)
func Nonce(size uint8) string {
nonce := make([]byte, size/2)
Expand Down
9 changes: 9 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ import (
"context"
"errors"
"fmt"
"log"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestSteps(t *testing.T) {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
for index, step := range Steps(len(arr), 6) {
ids := arr[step.Head:step.Tail]
log.Printf("step[%d], slice: %d\n", index, ids)
}
}

func TestMarshalNoEscapeHTML(t *testing.T) {
data := map[string]string{"url": "https://github.com/shenghui0779/yiigo?id=996&name=yiigo"}

Expand Down

0 comments on commit 86d2544

Please sign in to comment.