From 0902b0afb18817b7016dc76d14e70a99a3ce7b95 Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Fri, 5 Jan 2024 14:59:06 +0100 Subject: [PATCH] restructure examples and doc --- example_test.go | 56 ++++++++++++++++++++++++++++++++++++------------- treap.go | 51 -------------------------------------------- 2 files changed, 41 insertions(+), 66 deletions(-) diff --git a/example_test.go b/example_test.go index 67c7e0a..4f67474 100644 --- a/example_test.go +++ b/example_test.go @@ -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 () @@ -49,6 +71,10 @@ func ExampleTable_Fprint() { // ├─ 2000::/3 () // │ └─ 2001:db8::/32 () // └─ fe80::/10 () + // + // Lookup: 42.0.0.0 lpm: invalid Prefix value: , ok: false + // Lookup: 10.0.1.17 lpm: 10.0.1.0/24 value: , ok: true + // Lookup: 2001:7c0:3100:1::111 lpm: 2000::/3 value: , ok: true } func ExampleTable_Walk() { diff --git a/treap.go b/treap.go index 78aabdb..9db6e03 100644 --- a/treap.go +++ b/treap.go @@ -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{}, , false) -// rtbl.Lookup(10.0.1.17) returns (10.0.1.0/24, , true) -// rtbl.Lookup(2001:7c0:3100:1::111) returns (2000::/3, , true) func (t Table) Lookup(ip netip.Addr) (lpm netip.Prefix, value any, ok bool) { if ip.Is4() { // don't return the depth @@ -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{}, , false) -// rtbl.LookupPrefix(10.0.1.0/29) returns (10.0.1.0/24, , true) -// rtbl.LookupPrefix(192.168.0.0/16) returns (192.168.0.0/16, , true) -// rtbl.LookupPrefix(2001:7c0:3100::/40) returns (2000::/3, , true) func (t Table) LookupPrefix(pfx netip.Prefix) (lpm netip.Prefix, value any, ok bool) { pfx = pfx.Masked() // always canonicalize!