-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47c5da2
commit a500ad6
Showing
6 changed files
with
190 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,149 @@ | ||
package abnf_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ghettovoice/abnf" | ||
"github.com/ghettovoice/abnf/pkg/abnf_core" | ||
) | ||
|
||
func BenchmarkParseMessageStart(b *testing.B) { | ||
// str := "INVITE sip:[email protected] SIP/2.0" | ||
str := []byte("IN" + strings.Repeat("m", 64<<10) + "VITE") | ||
// str := []byte("INVITE") | ||
ns := make(abnf.Nodes, 0, 1) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
clear(ns) | ||
ns = Method(str, ns[:0]) | ||
if ns.Best().Len() != len(str) { | ||
b.Errorf("%d parse fail", i) | ||
} | ||
} | ||
} | ||
|
||
var method abnf.Operator | ||
|
||
func Method(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if method == nil { | ||
method = abnf.Alt( | ||
"Method", | ||
INVITEm, | ||
ACKm, | ||
OPTIONSm, | ||
BYEm, | ||
CANCELm, | ||
REGISTERm, | ||
ExtensionMethod, | ||
) | ||
} | ||
return method(s, ns) | ||
} | ||
|
||
var invitem abnf.Operator | ||
|
||
// INVITEm rule: INVITEm = %x49.4E.56.49.54.45 | ||
func INVITEm(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if invitem == nil { | ||
invitem = abnf.Literal("INVITEm", []byte{73, 78, 86, 73, 84, 69}) | ||
} | ||
return invitem(s, ns) | ||
} | ||
|
||
var ackm abnf.Operator | ||
|
||
// ACKm rule: ACKm = %x41.43.4B | ||
func ACKm(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if ackm == nil { | ||
ackm = abnf.Literal("ACKm", []byte{65, 67, 75}) | ||
} | ||
return ackm(s, ns) | ||
} | ||
|
||
var optionsm abnf.Operator | ||
|
||
// OPTIONSm rule: OPTIONSm = %x4F.50.54.49.4F.4E.53 | ||
func OPTIONSm(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if optionsm == nil { | ||
optionsm = abnf.Literal("OPTIONSm", []byte{79, 80, 84, 73, 79, 78, 83}) | ||
} | ||
return optionsm(s, ns) | ||
} | ||
|
||
var byem abnf.Operator | ||
|
||
// BYEm rule: BYEm = %x42.59.45 | ||
func BYEm(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if byem == nil { | ||
byem = abnf.Literal("BYEm", []byte{66, 89, 69}) | ||
} | ||
return byem(s, ns) | ||
} | ||
|
||
var cancelm abnf.Operator | ||
|
||
// CANCELm rule: CANCELm = %x43.41.4E.43.45.4C | ||
func CANCELm(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if cancelm == nil { | ||
cancelm = abnf.Literal("CANCELm", []byte{67, 65, 78, 67, 69, 76}) | ||
} | ||
return cancelm(s, ns) | ||
} | ||
|
||
var registerm abnf.Operator | ||
|
||
// REGISTERm rule: REGISTERm = %x52.45.47.49.53.54.45.52 | ||
func REGISTERm(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if registerm == nil { | ||
registerm = abnf.Literal("REGISTERm", []byte{82, 69, 71, 73, 83, 84, 69, 82}) | ||
} | ||
return registerm(s, ns) | ||
} | ||
|
||
var extensionMethod abnf.Operator | ||
|
||
// ExtensionMethod rule: extension-method = token | ||
func ExtensionMethod(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if extensionMethod == nil { | ||
extensionMethod = Token | ||
} | ||
return extensionMethod(s, ns) | ||
} | ||
|
||
var token abnf.Operator | ||
|
||
// Token rule: token = 1*(alphanum / "-" / "." / "!" / "%" / "*" / "_" / "+" / "`" / "'" / "~" ) | ||
func Token(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if token == nil { | ||
token = abnf.Repeat1Inf("token", abnf.Alt( | ||
"alphanum / \"-\" / \".\" / \"!\" / \"%\" / \"*\" / \"_\" / \"+\" / \"`\" / \"'\" / \"~\"", | ||
Alphanum, | ||
abnf.Literal("\"-\"", []byte{45}), | ||
abnf.Literal("\".\"", []byte{46}), | ||
abnf.Literal("\"!\"", []byte{33}), | ||
abnf.Literal("\"%\"", []byte{37}), | ||
abnf.Literal("\"*\"", []byte{42}), | ||
abnf.Literal("\"_\"", []byte{95}), | ||
abnf.Literal("\"+\"", []byte{43}), | ||
abnf.Literal("\"`\"", []byte{96}), | ||
abnf.Literal("\"'\"", []byte{39}), | ||
abnf.Literal("\"~\"", []byte{126}), | ||
)) | ||
} | ||
return token(s, ns) | ||
} | ||
|
||
var alphanum abnf.Operator | ||
|
||
// Alphanum rule: alphanum = ALPHA / DIGIT | ||
func Alphanum(s []byte, ns abnf.Nodes) abnf.Nodes { | ||
if alphanum == nil { | ||
alphanum = abnf.Alt( | ||
"alphanum", | ||
abnf_core.ALPHA, | ||
abnf_core.DIGIT, | ||
) | ||
} | ||
return alphanum(s, ns) | ||
} |