Skip to content

Commit

Permalink
rewrite pack (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing924 authored Apr 14, 2023
1 parent 867ef79 commit b4feda4
Show file tree
Hide file tree
Showing 13 changed files with 779 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '^1.17.6'
go-version: '^1.20.3'

- name: Go Format
run: |
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.0.0 (2023-04-14)

* Support Go 1.20.
* Rewrite `Pack` algorithm

## 1.1.0 (2022-01-24)

* Support Go 1.17.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A golang library for packing and unpacking hosts list
## Install

```bash
go get github.com/Wing924/hostutils
go get github.com/Wing924/hostutils/v2
```

## Examples
Expand All @@ -22,7 +22,7 @@ package main
import (
"fmt"

"github.com/Wing924/hostutils"
"github.com/Wing924/hostutils/v2"
)

func main() {
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module github.com/Wing924/hostutils

go 1.20

require github.com/stretchr/testify v1.2.2

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
4 changes: 2 additions & 2 deletions pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"strings"
)

// PackString Pack space septated full hosts list into short abbreviated hosts.
// PackString Pack space separated full hosts list into short abbreviated hosts.
func PackString(hosts string) (packedHosts []string) {
return Pack([]string{hosts})
}

// Pack Pack full hosts list into short abbreviated hosts.
// Pack full hosts list into short abbreviated hosts.
func Pack(hosts []string) (packedHosts []string) {
regHosts := regularizeHosts(hosts[:])
if regHosts == nil {
Expand Down
43 changes: 43 additions & 0 deletions v2/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package hostutils

import (
"golang.org/x/exp/constraints"
"regexp"
)

var (
reComment = regexp.MustCompile(`#.*`)
reSpaces = regexp.MustCompile(`\s+`)
rePackedHost = regexp.MustCompile(`^([^\[]*)\[([-,:0-9\s]+)](.*)$`)
reCondSpace = regexp.MustCompile(`,\s*`)
reCondBlk = regexp.MustCompile(`^(\d+)([-:](\d+))?$`)
)

func max[T constraints.Ordered](a T, b T) T {
if a > b {
return a
}
return b
}

func regularizeHosts(hosts []string) []string {
if hosts == nil {
return nil
}
uniqHosts := make(map[string]bool)
for _, host := range hosts {
noCmtHosts := reComment.ReplaceAllString(host, "")
for _, h := range reSpaces.Split(noCmtHosts, -1) {
if h != "" {
uniqHosts[h] = true
}
}
}
result := make([]string, len(uniqHosts))
var i = 0
for host := range uniqHosts {
result[i] = host
i++
}
return result
}
11 changes: 11 additions & 0 deletions v2/normalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hostutils

// Normalize Unpack and pack hosts
func Normalize(hosts []string) (packedHosts []string) {
return Pack(Unpack(hosts))
}

// NormalizeString Unpack and pack hosts
func NormalizeString(hosts string) (packedHosts []string) {
return Pack(Unpack([]string{hosts}))
}
32 changes: 32 additions & 0 deletions v2/normalize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hostutils

import (
"testing"

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

func TestNormalize(t *testing.T) {
testNormalize(t, nil, nil)
testNormalize(t, []string{}, nil)
testNormalize(t, []string{"ww[101]"}, []string{"ww101"})
testNormalize(t, []string{"ww[101-103]", "ww[104]"}, []string{"ww[101-104]"})
testNormalize(t, []string{"a[101,103,105]", "a[102,104,107]"}, []string{"a[101-105,107]"})
testNormalize(t, []string{"[101-103]", "104"}, []string{"[101-104]"})
}

func TestNormalizeString(t *testing.T) {
testNormalizeString(t, "", nil)
testNormalizeString(t, "ww[101]", []string{"ww101"})
testNormalizeString(t, "ww[101-103] ww[104]", []string{"ww[101-104]"})
testNormalizeString(t, "a[101,103,105] a[102,104,107]", []string{"a[101-105,107]"})
testNormalizeString(t, "[101-103] 104", []string{"[101-104]"})
}

func testNormalize(t *testing.T, input []string, expected []string) {
assert.Equal(t, expected, Normalize(input))
}

func testNormalizeString(t *testing.T, input string, expected []string) {
assert.Equal(t, expected, NormalizeString(input))
}
Loading

0 comments on commit b4feda4

Please sign in to comment.