Skip to content

Commit

Permalink
restructure examples and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 5, 2024
1 parent 0b6d460 commit c87ea26
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 70 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
test:
strategy:
matrix:
go-version: ['stable']
go-version: ['1.21']
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -27,6 +27,7 @@ jobs:
steps:
- uses: golang/govulncheck-action@v1
with:
go-version-input: 1.21
check-latest: true

coverage:
Expand All @@ -35,7 +36,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
check-latest: true
go-version: 1.21

- name: Test Coverage
run: go test -v -coverprofile=profile.cov ./...
Expand All @@ -50,8 +51,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
check-latest: true

go-version: 1.21
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v0.3.0 ...
* API CHANGE!!!
* bump go version to 1.21 (for stdlib cmp)
* simplified the names of the standard methods

v0.2.1 Fri Dec 29 23:47:49 2023 +0100
Expand Down
56 changes: 41 additions & 15 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,52 @@ import (
"github.com/gaissmai/cidrtree"
)

func a(s string) netip.Addr {
return netip.MustParseAddr(s)
}

func p(s string) netip.Prefix {
return netip.MustParsePrefix(s)
}

var input = []netip.Prefix{
netip.MustParsePrefix("fe80::/10"),
netip.MustParsePrefix("172.16.0.0/12"),
netip.MustParsePrefix("10.0.0.0/24"),
netip.MustParsePrefix("::1/128"),
netip.MustParsePrefix("192.168.0.0/16"),
netip.MustParsePrefix("10.0.0.0/8"),
netip.MustParsePrefix("::/0"),
netip.MustParsePrefix("10.0.1.0/24"),
netip.MustParsePrefix("169.254.0.0/16"),
netip.MustParsePrefix("2000::/3"),
netip.MustParsePrefix("2001:db8::/32"),
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("127.0.0.1/32"),
netip.MustParsePrefix("192.168.1.0/24"),
p("fe80::/10"),
p("172.16.0.0/12"),
p("10.0.0.0/24"),
p("::1/128"),
p("192.168.0.0/16"),
p("10.0.0.0/8"),
p("::/0"),
p("10.0.1.0/24"),
p("169.254.0.0/16"),
p("2000::/3"),
p("2001:db8::/32"),
p("127.0.0.0/8"),
p("127.0.0.1/32"),
p("192.168.1.0/24"),
}

func ExampleTable_Fprint() {
func ExampleTable_Lookup() {
rtbl := new(cidrtree.Table)
for _, cidr := range input {
rtbl.Insert(cidr, nil)
}
rtbl.Fprint(os.Stdout)

fmt.Println()

ip := a("42.0.0.0")
lpm, value, ok := rtbl.Lookup(ip)
fmt.Printf("Lookup: %-20v lpm: %-15v value: %v, ok: %v\n", ip, lpm, value, ok)

ip = a("10.0.1.17")
lpm, value, ok = rtbl.Lookup(ip)
fmt.Printf("Lookup: %-20v lpm: %-15v value: %v, ok: %v\n", ip, lpm, value, ok)

ip = a("2001:7c0:3100:1::111")
lpm, value, ok = rtbl.Lookup(ip)
fmt.Printf("Lookup: %-20v lpm: %-15v value: %v, ok: %v\n", ip, lpm, value, ok)

// Output:
// ▼
// ├─ 10.0.0.0/8 (<nil>)
Expand All @@ -49,6 +71,10 @@ func ExampleTable_Fprint() {
// ├─ 2000::/3 (<nil>)
// │ └─ 2001:db8::/32 (<nil>)
// └─ fe80::/10 (<nil>)
//
// Lookup: 42.0.0.0 lpm: invalid Prefix value: <nil>, ok: false
// Lookup: 10.0.1.17 lpm: 10.0.1.0/24 value: <nil>, ok: true
// Lookup: 2001:7c0:3100:1::111 lpm: 2000::/3 value: <nil>, ok: true
}

func ExampleTable_Walk() {
Expand Down
51 changes: 0 additions & 51 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,31 +273,6 @@ func (n *node) walk(cb func(netip.Prefix, any) bool) bool {
// If the ip isn't covered by any CIDR, the zero value and false is returned.
//
// Lookup does not allocate memory.
//
// example:
//
// ▼
// ├─ 10.0.0.0/8
// │ ├─ 10.0.0.0/24
// │ └─ 10.0.1.0/24
// ├─ 127.0.0.0/8
// │ └─ 127.0.0.1/32
// ├─ 169.254.0.0/16
// ├─ 172.16.0.0/12
// └─ 192.168.0.0/16
// └─ 192.168.1.0/24
// ▼
// └─ ::/0
// ├─ ::1/128
// ├─ 2000::/3
// │ └─ 2001:db8::/32
// ├─ fc00::/7
// ├─ fe80::/10
// └─ ff00::/8
//
// rtbl.Lookup(42.0.0.0) returns (netip.Prefix{}, <nil>, false)
// rtbl.Lookup(10.0.1.17) returns (10.0.1.0/24, <value>, true)
// rtbl.Lookup(2001:7c0:3100:1::111) returns (2000::/3, <value>, true)
func (t Table) Lookup(ip netip.Addr) (lpm netip.Prefix, value any, ok bool) {
if ip.Is4() {
// don't return the depth
Expand Down Expand Up @@ -351,32 +326,6 @@ func (n *node) lpmIP(ip netip.Addr, depth int) (lpm netip.Prefix, value any, ok
// If the prefix isn't equal or covered by any CIDR in the table, the zero value and false is returned.
//
// LookupPrefix does not allocate memory.
//
// example:
//
// ▼
// ├─ 10.0.0.0/8
// │ ├─ 10.0.0.0/24
// │ └─ 10.0.1.0/24
// ├─ 127.0.0.0/8
// │ └─ 127.0.0.1/32
// ├─ 169.254.0.0/16
// ├─ 172.16.0.0/12
// └─ 192.168.0.0/16
// └─ 192.168.1.0/24
// ▼
// └─ ::/0
// ├─ ::1/128
// ├─ 2000::/3
// │ └─ 2001:db8::/32
// ├─ fc00::/7
// ├─ fe80::/10
// └─ ff00::/8
//
// rtbl.LookupPrefix(42.0.0.0/8) returns (netip.Prefix{}, <nil>, false)
// rtbl.LookupPrefix(10.0.1.0/29) returns (10.0.1.0/24, <value>, true)
// rtbl.LookupPrefix(192.168.0.0/16) returns (192.168.0.0/16, <value>, true)
// rtbl.LookupPrefix(2001:7c0:3100::/40) returns (2000::/3, <value>, true)
func (t Table) LookupPrefix(pfx netip.Prefix) (lpm netip.Prefix, value any, ok bool) {
pfx = pfx.Masked() // always canonicalize!

Expand Down

0 comments on commit c87ea26

Please sign in to comment.