-
Notifications
You must be signed in to change notification settings - Fork 117
Megular Expressions #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Megular Expressions #263
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
550e3cc
Add multiaddr expression group matching
MarcoPolo b86244a
Move meg package to /x/ for experimental
MarcoPolo 0e1db60
Add fuzz cases
MarcoPolo 6088fcd
faster Code() method
MarcoPolo 3342cbf
avoid copying an empty map if no captures
MarcoPolo 09e5347
Faster megular expressions (#265)
MarcoPolo ae47e22
feat(x/meg): Support capturing components (#269)
MarcoPolo 6a85b40
Fix typo in rebase
MarcoPolo 0c5383d
workaround for go generics
MarcoPolo c6d7d99
less hacky workaround for go generics
MarcoPolo d841a27
Avoid unnecessary copy
MarcoPolo 002620b
Make `matchAny` less greedy when there are alternatives
MarcoPolo 930039b
PR comments
MarcoPolo 9f1651b
Update Pattern type to return next state
MarcoPolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package multiaddr | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"net/netip" | ||
|
||
"github.com/multiformats/go-multiaddr/x/meg" | ||
) | ||
|
||
func CaptureAddrPort(network *string, ipPort *netip.AddrPort) (capturePattern meg.Pattern) { | ||
var ipOnly netip.Addr | ||
capturePort := func(s meg.Matchable) error { | ||
switch s.Code() { | ||
case P_UDP: | ||
*network = "udp" | ||
case P_TCP: | ||
*network = "tcp" | ||
default: | ||
return fmt.Errorf("invalid network: %s", s.Value()) | ||
} | ||
|
||
port := binary.BigEndian.Uint16(s.RawValue()) | ||
*ipPort = netip.AddrPortFrom(ipOnly, port) | ||
return nil | ||
} | ||
|
||
pattern := meg.Cat( | ||
meg.Or( | ||
meg.CaptureWithF(P_IP4, func(s meg.Matchable) error { | ||
var ok bool | ||
ipOnly, ok = netip.AddrFromSlice(s.RawValue()) | ||
if !ok { | ||
return fmt.Errorf("invalid ip4 address: %s", s.Value()) | ||
} | ||
return nil | ||
}), | ||
meg.CaptureWithF(P_IP6, func(s meg.Matchable) error { | ||
var ok bool | ||
ipOnly, ok = netip.AddrFromSlice(s.RawValue()) | ||
if !ok { | ||
return fmt.Errorf("invalid ip6 address: %s", s.Value()) | ||
} | ||
return nil | ||
}), | ||
), | ||
meg.Or( | ||
meg.CaptureWithF(P_UDP, capturePort), | ||
meg.CaptureWithF(P_TCP, capturePort), | ||
), | ||
) | ||
|
||
return pattern | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package multiaddr | ||
|
||
import ( | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/multiformats/go-multiaddr/x/meg" | ||
) | ||
|
||
func TestMatchAndCaptureMultiaddr(t *testing.T) { | ||
m := StringCast("/ip4/1.2.3.4/udp/8231/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41") | ||
|
||
var udpPort string | ||
var certhashes []string | ||
found, _ := m.Match( | ||
meg.Or( | ||
meg.Val(P_IP4), | ||
meg.Val(P_IP6), | ||
), | ||
meg.CaptureStringVal(P_UDP, &udpPort), | ||
meg.Val(P_QUIC_V1), | ||
meg.Val(P_WEBTRANSPORT), | ||
meg.CaptureZeroOrMoreStrings(P_CERTHASH, &certhashes), | ||
) | ||
if !found { | ||
t.Fatal("failed to match") | ||
} | ||
if udpPort != "8231" { | ||
t.Fatal("unexpected value") | ||
} | ||
|
||
if len(certhashes) != 2 { | ||
t.Fatal("Didn't capture all certhashes") | ||
} | ||
|
||
{ | ||
m, c := SplitLast(m) | ||
if c.Value() != certhashes[1] { | ||
t.Fatal("unexpected value. Expected", c.RawValue(), "but got", []byte(certhashes[1])) | ||
} | ||
_, c = SplitLast(m) | ||
if c.Value() != certhashes[0] { | ||
t.Fatal("unexpected value. Expected", c.RawValue(), "but got", []byte(certhashes[0])) | ||
} | ||
} | ||
} | ||
|
||
func TestCaptureAddrPort(t *testing.T) { | ||
m := StringCast("/ip4/1.2.3.4/udp/8231/quic-v1/webtransport") | ||
var addrPort netip.AddrPort | ||
var network string | ||
|
||
found, err := m.Match( | ||
CaptureAddrPort(&network, &addrPort), | ||
meg.ZeroOrMore(meg.Any), | ||
) | ||
if err != nil { | ||
t.Fatal("error", err) | ||
} | ||
if !found { | ||
t.Fatal("failed to match") | ||
} | ||
if !addrPort.IsValid() { | ||
t.Fatal("failed to capture addrPort") | ||
} | ||
if network != "udp" { | ||
t.Fatal("unexpected network", network) | ||
} | ||
if addrPort.String() != "1.2.3.4:8231" { | ||
t.Fatal("unexpected ipPort", addrPort) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?
I'd prefer a method in
manet
that returns(network string, ipPort netip.AddrPort)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This composes better. You can use this and capture other parts of the multiaddr in a single pass.
We don't need this, but I think it's helpful to demonstrate this pattern.