MCPcopy
hub / github.com/grpc-ecosystem/grpc-gateway / NewPattern

Function NewPattern

runtime/pattern.go:53–140  ·  view source on GitHub ↗

NewPattern returns a new Pattern from the given definition values. "ops" is a sequence of op codes. "pool" is a constant pool. "verb" is the verb part of the pattern. It is empty if the pattern does not have the part. "version" must be 1 for now. It returns an error if the given definition is invali

(version int, ops []int, pool []string, verb string)

Source from the content-addressed store, hash-verified

51// "version" must be 1 for now.
52// It returns an error if the given definition is invalid.
53func NewPattern(version int, ops []int, pool []string, verb string) (Pattern, error) {
54 if version != 1 {
55 grpclog.Errorf("unsupported version: %d", version)
56 return Pattern{}, ErrInvalidPattern
57 }
58
59 l := len(ops)
60 if l%2 != 0 {
61 grpclog.Errorf("odd number of ops codes: %d", l)
62 return Pattern{}, ErrInvalidPattern
63 }
64
65 var (
66 typedOps []op
67 stack, maxstack int
68 tailLen int
69 pushMSeen bool
70 vars []string
71 )
72 for i := 0; i < l; i += 2 {
73 op := op{code: utilities.OpCode(ops[i]), operand: ops[i+1]}
74 switch op.code {
75 case utilities.OpNop:
76 continue
77 case utilities.OpPush:
78 if pushMSeen {
79 tailLen++
80 }
81 stack++
82 case utilities.OpPushM:
83 if pushMSeen {
84 grpclog.Error("pushM appears twice")
85 return Pattern{}, ErrInvalidPattern
86 }
87 pushMSeen = true
88 stack++
89 case utilities.OpLitPush:
90 if op.operand < 0 || len(pool) <= op.operand {
91 grpclog.Errorf("negative literal index: %d", op.operand)
92 return Pattern{}, ErrInvalidPattern
93 }
94 if pushMSeen {
95 tailLen++
96 }
97 stack++
98 case utilities.OpConcatN:
99 if op.operand <= 0 {
100 grpclog.Errorf("negative concat size: %d", op.operand)
101 return Pattern{}, ErrInvalidPattern
102 }
103 stack -= op.operand
104 if stack < 0 {
105 grpclog.Error("stack underflow")
106 return Pattern{}, ErrInvalidPattern
107 }
108 stack++
109 case utilities.OpCapture:
110 if op.operand < 0 || len(pool) <= op.operand {

Calls 2

OpCodeTypeAlias · 0.92
ErrorMethod · 0.45

Tested by 8

TestMuxServeHTTPFunction · 0.74
TestNewPatternFunction · 0.68
TestMatchFunction · 0.68
TestMatchWithBindingFunction · 0.68
TestPatternStringFunction · 0.68