fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

Add regexp raw pattern API (#1061)

Adds query.Regexp.RegexpString() as the public API for getting the marshaled raw regexp pattern. Existing query.Regexp string, proto and gob serialization paths now route through that method.

author
Jan Hartman
committer
GitHub
date (May 15, 2026, 10:56 AM +0200) commit 5f844d45 parent 327cf11f
+39 -3
+7 -2
query/query.go
··· 79 79 CaseSensitive bool 80 80 } 81 81 82 + // RegexpString returns the marshaled raw regexp pattern for q. 83 + func (q *Regexp) RegexpString() string { 84 + return syntaxutil.RegexpString(q.Regexp) 85 + } 86 + 82 87 func (q *Regexp) String() string { 83 88 pref := "" 84 89 if q.FileName { ··· 87 92 if q.CaseSensitive { 88 93 pref = "case_" + pref 89 94 } 90 - return fmt.Sprintf("%sregex:%q", pref, syntaxutil.RegexpString(q.Regexp)) 95 + return fmt.Sprintf("%sregex:%q", pref, q.RegexpString()) 91 96 } 92 97 93 98 // gobRegexp wraps Regexp to make it gob-encodable/decodable. Regexp contains syntax.Regexp, which ··· 100 105 101 106 // GobEncode implements gob.Encoder. 102 107 func (q Regexp) GobEncode() ([]byte, error) { 103 - gobq := gobRegexp{Regexp: q, RegexpString: syntaxutil.RegexpString(q.Regexp)} 108 + gobq := gobRegexp{Regexp: q, RegexpString: q.RegexpString()} 104 109 gobq.Regexp.Regexp = nil // can't be gob-encoded/decoded 105 110 return json.Marshal(gobq) 106 111 }
+1 -1
query/query_proto.go
··· 115 115 116 116 func (r *Regexp) ToProto() *webserverv1.Regexp { 117 117 return &webserverv1.Regexp{ 118 - Regexp: r.Regexp.String(), 118 + Regexp: r.RegexpString(), 119 119 FileName: r.FileName, 120 120 Content: r.Content, 121 121 CaseSensitive: r.CaseSensitive,
+9
query/query_proto_test.go
··· 103 103 } 104 104 } 105 105 106 + func TestRegexpProtoUsesRegexpString(t *testing.T) { 107 + q := &Regexp{Regexp: regexpMustParse(`a.*b`)} 108 + protoQ := q.ToProto() 109 + 110 + if got, want := protoQ.GetRegexp(), q.RegexpString(); got != want { 111 + t.Fatalf("ToProto().Regexp = %q, want %q", got, want) 112 + } 113 + } 114 + 106 115 func regexpMustParse(s string) *syntax.Regexp { 107 116 re, err := syntax.Parse(s, syntax.Perl) 108 117 if err != nil {
+22
query/regexp_test.go
··· 101 101 }) 102 102 } 103 103 } 104 + 105 + func TestRegexpRegexpString(t *testing.T) { 106 + tests := []struct { 107 + in string 108 + want string 109 + }{ 110 + {in: `abc`, want: `abc`}, 111 + {in: `a.*b`, want: `a(?-s:.)*b`}, 112 + } 113 + 114 + for _, tt := range tests { 115 + t.Run(tt.in, func(t *testing.T) { 116 + q := &Regexp{Regexp: mustParseRE(tt.in)} 117 + if got := q.RegexpString(); got != tt.want { 118 + t.Fatalf("RegexpString(%q) = %q, want %q", tt.in, got, tt.want) 119 + } 120 + if got := q.String(); got == tt.want { 121 + t.Fatalf("String(%q) = raw pattern %q, want query formatting", tt.in, got) 122 + } 123 + }) 124 + } 125 + }