Skip to content

Commit 4833ac5

Browse files
committed
internal/mod: add workfile parser
This is a minimal copy of golang.org/x/mod/modfile that adds the parser in CL 324764. It will be used to add experimental support for go.work in gopls. Change-Id: I75a8171eda763506ea8b5ffa0c6d163d59010333 Reviewed-on: https://go-review.googlesource.com/c/tools/+/329069 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent bfc1674 commit 4833ac5

File tree

3 files changed

+1066
-0
lines changed

3 files changed

+1066
-0
lines changed

internal/mod/lazyregexp/lazyre.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package lazyregexp is a thin wrapper over regexp, allowing the use of global
6+
// regexp variables without forcing them to be compiled at init.
7+
package lazyregexp
8+
9+
import (
10+
"os"
11+
"regexp"
12+
"strings"
13+
"sync"
14+
)
15+
16+
// Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be
17+
// compiled the first time it is needed.
18+
type Regexp struct {
19+
str string
20+
once sync.Once
21+
rx *regexp.Regexp
22+
}
23+
24+
func (r *Regexp) re() *regexp.Regexp {
25+
r.once.Do(r.build)
26+
return r.rx
27+
}
28+
29+
func (r *Regexp) build() {
30+
r.rx = regexp.MustCompile(r.str)
31+
r.str = ""
32+
}
33+
34+
func (r *Regexp) MatchString(s string) bool {
35+
return r.re().MatchString(s)
36+
}
37+
38+
var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
39+
40+
// New creates a new lazy regexp, delaying the compiling work until it is first
41+
// needed. If the code is being run as part of tests, the regexp compiling will
42+
// happen immediately.
43+
func New(str string) *Regexp {
44+
lr := &Regexp{str: str}
45+
if inTest {
46+
// In tests, always compile the regexps early.
47+
lr.re()
48+
}
49+
return lr
50+
}

0 commit comments

Comments
 (0)