GoPLS Viewer

Home|gopls/go/buildutil/tags.go
1// Copyright 2015 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
5package buildutil
6
7// This logic was copied from stringsFlag from $GOROOT/src/cmd/go/build.go.
8
9import "fmt"
10
11const TagsFlagDoc = "a list of `build tags` to consider satisfied during the build. " +
12    "For more information about build tags, see the description of " +
13    "build constraints in the documentation for the go/build package"
14
15// TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses
16// a flag value in the same manner as go build's -tags flag and
17// populates a []string slice.
18//
19// See $GOROOT/src/go/build/doc.go for description of build tags.
20// See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag.
21//
22// Example:
23//
24//    flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
25type TagsFlag []string
26
27func (v *TagsFlagSet(s stringerror {
28    var err error
29    *verr = splitQuotedFields(s)
30    if *v == nil {
31        *v = []string{}
32    }
33    return err
34}
35
36func (v *TagsFlagGet() interface{} { return *v }
37
38func splitQuotedFields(s string) ([]stringerror) {
39    // Split fields allowing '' or "" around elements.
40    // Quotes further inside the string do not count.
41    var f []string
42    for len(s) > 0 {
43        for len(s) > 0 && isSpaceByte(s[0]) {
44            s = s[1:]
45        }
46        if len(s) == 0 {
47            break
48        }
49        // Accepted quoted string. No unescaping inside.
50        if s[0] == '"' || s[0] == '\'' {
51            quote := s[0]
52            s = s[1:]
53            i := 0
54            for i < len(s) && s[i] != quote {
55                i++
56            }
57            if i >= len(s) {
58                return nilfmt.Errorf("unterminated %c string"quote)
59            }
60            f = append(fs[:i])
61            s = s[i+1:]
62            continue
63        }
64        i := 0
65        for i < len(s) && !isSpaceByte(s[i]) {
66            i++
67        }
68        f = append(fs[:i])
69        s = s[i:]
70    }
71    return fnil
72}
73
74func (v *TagsFlagString() string {
75    return "<tagsFlag>"
76}
77
78func isSpaceByte(c bytebool {
79    return c == ' ' || c == '\t' || c == '\n' || c == '\r'
80}
81
MembersX
TagsFlag.Set.s
splitQuotedFields.BlockStmt.i
TagsFlag.String
isSpaceByte
TagsFlag.Set.v
TagsFlag.Get
TagsFlag.String.v
isSpaceByte.c
TagsFlag.Set.err
splitQuotedFields
splitQuotedFields.f
splitQuotedFields.BlockStmt.BlockStmt.i
TagsFlag
TagsFlag.Get.v
splitQuotedFields.s
TagsFlag.Set
Members
X