]> Cypherpunks repositories - gostls13.git/commitdiff
html/template: make more use of stringer
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 20 Feb 2018 10:02:10 +0000 (10:02 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 20 Feb 2018 15:44:01 +0000 (15:44 +0000)
The code was maintaining manual versions of it in multiple places -
replace all of them.

Change-Id: I04c3063877b05ba914de9f5dddb33ffe09f308fe
Reviewed-on: https://go-review.googlesource.com/95356
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/html/template/context.go
src/html/template/delim_string.go [new file with mode: 0644]
src/html/template/element_string.go [new file with mode: 0644]
src/html/template/jsctx_string.go [new file with mode: 0644]
src/html/template/state_string.go [new file with mode: 0644]
src/html/template/urlpart_string.go [new file with mode: 0644]

index 7e28cf47e213b8845c91e5f18d3da91e0482a003..fdbf7e25ee2dffb713c88d2d239e27752db701f7 100644 (file)
@@ -77,6 +77,8 @@ func (c context) mangle(templateName string) string {
 // is a single token in HTML's grammar but in a template spans several nodes.
 type state uint8
 
+//go:generate stringer -type state
+
 const (
        // stateText is parsed character data. An HTML parser is in
        // this state when its parse position is outside an HTML tag,
@@ -137,41 +139,6 @@ const (
        stateError
 )
 
-var stateNames = [...]string{
-       stateText:        "stateText",
-       stateTag:         "stateTag",
-       stateAttrName:    "stateAttrName",
-       stateAfterName:   "stateAfterName",
-       stateBeforeValue: "stateBeforeValue",
-       stateHTMLCmt:     "stateHTMLCmt",
-       stateRCDATA:      "stateRCDATA",
-       stateAttr:        "stateAttr",
-       stateURL:         "stateURL",
-       stateSrcset:      "stateSrcset",
-       stateJS:          "stateJS",
-       stateJSDqStr:     "stateJSDqStr",
-       stateJSSqStr:     "stateJSSqStr",
-       stateJSRegexp:    "stateJSRegexp",
-       stateJSBlockCmt:  "stateJSBlockCmt",
-       stateJSLineCmt:   "stateJSLineCmt",
-       stateCSS:         "stateCSS",
-       stateCSSDqStr:    "stateCSSDqStr",
-       stateCSSSqStr:    "stateCSSSqStr",
-       stateCSSDqURL:    "stateCSSDqURL",
-       stateCSSSqURL:    "stateCSSSqURL",
-       stateCSSURL:      "stateCSSURL",
-       stateCSSBlockCmt: "stateCSSBlockCmt",
-       stateCSSLineCmt:  "stateCSSLineCmt",
-       stateError:       "stateError",
-}
-
-func (s state) String() string {
-       if int(s) < len(stateNames) {
-               return stateNames[s]
-       }
-       return fmt.Sprintf("illegal state %d", int(s))
-}
-
 // isComment is true for any state that contains content meant for template
 // authors & maintainers, not for end-users or machines.
 func isComment(s state) bool {
@@ -194,6 +161,8 @@ func isInTag(s state) bool {
 // delim is the delimiter that will end the current HTML attribute.
 type delim uint8
 
+//go:generate stringer -type delim
+
 const (
        // delimNone occurs outside any attribute.
        delimNone delim = iota
@@ -206,24 +175,12 @@ const (
        delimSpaceOrTagEnd
 )
 
-var delimNames = [...]string{
-       delimNone:          "delimNone",
-       delimDoubleQuote:   "delimDoubleQuote",
-       delimSingleQuote:   "delimSingleQuote",
-       delimSpaceOrTagEnd: "delimSpaceOrTagEnd",
-}
-
-func (d delim) String() string {
-       if int(d) < len(delimNames) {
-               return delimNames[d]
-       }
-       return fmt.Sprintf("illegal delim %d", int(d))
-}
-
 // urlPart identifies a part in an RFC 3986 hierarchical URL to allow different
 // encoding strategies.
 type urlPart uint8
 
+//go:generate stringer -type urlPart
+
 const (
        // urlPartNone occurs when not in a URL, or possibly at the start:
        // ^ in "^http://auth/path?k=v#frag".
@@ -239,24 +196,12 @@ const (
        urlPartUnknown
 )
 
-var urlPartNames = [...]string{
-       urlPartNone:        "urlPartNone",
-       urlPartPreQuery:    "urlPartPreQuery",
-       urlPartQueryOrFrag: "urlPartQueryOrFrag",
-       urlPartUnknown:     "urlPartUnknown",
-}
-
-func (u urlPart) String() string {
-       if int(u) < len(urlPartNames) {
-               return urlPartNames[u]
-       }
-       return fmt.Sprintf("illegal urlPart %d", int(u))
-}
-
 // jsCtx determines whether a '/' starts a regular expression literal or a
 // division operator.
 type jsCtx uint8
 
+//go:generate stringer -type jsCtx
+
 const (
        // jsCtxRegexp occurs where a '/' would start a regexp literal.
        jsCtxRegexp jsCtx = iota
@@ -266,18 +211,6 @@ const (
        jsCtxUnknown
 )
 
-func (c jsCtx) String() string {
-       switch c {
-       case jsCtxRegexp:
-               return "jsCtxRegexp"
-       case jsCtxDivOp:
-               return "jsCtxDivOp"
-       case jsCtxUnknown:
-               return "jsCtxUnknown"
-       }
-       return fmt.Sprintf("illegal jsCtx %d", int(c))
-}
-
 // element identifies the HTML element when inside a start tag or special body.
 // Certain HTML element (for example <script> and <style>) have bodies that are
 // treated differently from stateText so the element type is necessary to
@@ -285,6 +218,8 @@ func (c jsCtx) String() string {
 // end delimiter for the body.
 type element uint8
 
+//go:generate stringer -type element
+
 const (
        // elementNone occurs outside a special tag or special element body.
        elementNone element = iota
@@ -299,21 +234,6 @@ const (
        elementTitle
 )
 
-var elementNames = [...]string{
-       elementNone:     "elementNone",
-       elementScript:   "elementScript",
-       elementStyle:    "elementStyle",
-       elementTextarea: "elementTextarea",
-       elementTitle:    "elementTitle",
-}
-
-func (e element) String() string {
-       if int(e) < len(elementNames) {
-               return elementNames[e]
-       }
-       return fmt.Sprintf("illegal element %d", int(e))
-}
-
 //go:generate stringer -type attr
 
 // attr identifies the current HTML attribute when inside the attribute,
diff --git a/src/html/template/delim_string.go b/src/html/template/delim_string.go
new file mode 100644 (file)
index 0000000..6d80e09
--- /dev/null
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type delim"; DO NOT EDIT.
+
+package template
+
+import "strconv"
+
+const _delim_name = "delimNonedelimDoubleQuotedelimSingleQuotedelimSpaceOrTagEnd"
+
+var _delim_index = [...]uint8{0, 9, 25, 41, 59}
+
+func (i delim) String() string {
+       if i >= delim(len(_delim_index)-1) {
+               return "delim(" + strconv.FormatInt(int64(i), 10) + ")"
+       }
+       return _delim_name[_delim_index[i]:_delim_index[i+1]]
+}
diff --git a/src/html/template/element_string.go b/src/html/template/element_string.go
new file mode 100644 (file)
index 0000000..4573e08
--- /dev/null
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type element"; DO NOT EDIT.
+
+package template
+
+import "strconv"
+
+const _element_name = "elementNoneelementScriptelementStyleelementTextareaelementTitle"
+
+var _element_index = [...]uint8{0, 11, 24, 36, 51, 63}
+
+func (i element) String() string {
+       if i >= element(len(_element_index)-1) {
+               return "element(" + strconv.FormatInt(int64(i), 10) + ")"
+       }
+       return _element_name[_element_index[i]:_element_index[i+1]]
+}
diff --git a/src/html/template/jsctx_string.go b/src/html/template/jsctx_string.go
new file mode 100644 (file)
index 0000000..dd1d87e
--- /dev/null
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type jsCtx"; DO NOT EDIT.
+
+package template
+
+import "strconv"
+
+const _jsCtx_name = "jsCtxRegexpjsCtxDivOpjsCtxUnknown"
+
+var _jsCtx_index = [...]uint8{0, 11, 21, 33}
+
+func (i jsCtx) String() string {
+       if i >= jsCtx(len(_jsCtx_index)-1) {
+               return "jsCtx(" + strconv.FormatInt(int64(i), 10) + ")"
+       }
+       return _jsCtx_name[_jsCtx_index[i]:_jsCtx_index[i+1]]
+}
diff --git a/src/html/template/state_string.go b/src/html/template/state_string.go
new file mode 100644 (file)
index 0000000..05104be
--- /dev/null
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type state"; DO NOT EDIT.
+
+package template
+
+import "strconv"
+
+const _state_name = "stateTextstateTagstateAttrNamestateAfterNamestateBeforeValuestateHTMLCmtstateRCDATAstateAttrstateURLstateSrcsetstateJSstateJSDqStrstateJSSqStrstateJSRegexpstateJSBlockCmtstateJSLineCmtstateCSSstateCSSDqStrstateCSSSqStrstateCSSDqURLstateCSSSqURLstateCSSURLstateCSSBlockCmtstateCSSLineCmtstateError"
+
+var _state_index = [...]uint16{0, 9, 17, 30, 44, 60, 72, 83, 92, 100, 111, 118, 130, 142, 155, 170, 184, 192, 205, 218, 231, 244, 255, 271, 286, 296}
+
+func (i state) String() string {
+       if i >= state(len(_state_index)-1) {
+               return "state(" + strconv.FormatInt(int64(i), 10) + ")"
+       }
+       return _state_name[_state_index[i]:_state_index[i+1]]
+}
diff --git a/src/html/template/urlpart_string.go b/src/html/template/urlpart_string.go
new file mode 100644 (file)
index 0000000..813eea9
--- /dev/null
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type urlPart"; DO NOT EDIT.
+
+package template
+
+import "strconv"
+
+const _urlPart_name = "urlPartNoneurlPartPreQueryurlPartQueryOrFragurlPartUnknown"
+
+var _urlPart_index = [...]uint8{0, 11, 26, 44, 58}
+
+func (i urlPart) String() string {
+       if i >= urlPart(len(_urlPart_index)-1) {
+               return "urlPart(" + strconv.FormatInt(int64(i), 10) + ")"
+       }
+       return _urlPart_name[_urlPart_index[i]:_urlPart_index[i+1]]
+}