pkg/textparse: unescape help string

Signed-off-by: Fabian Reinartz <freinartz@google.com>
This commit is contained in:
Fabian Reinartz 2018-06-05 06:24:20 -04:00
parent ad4c33c1ff
commit 0ff42e754e

View file

@ -165,7 +165,13 @@ func (p *Parser) Series() ([]byte, *int64, float64) {
// Must only be called after Next returned a help entry. // Must only be called after Next returned a help entry.
// The returned byte slices become invalid after the next call to Next. // The returned byte slices become invalid after the next call to Next.
func (p *Parser) Help() ([]byte, []byte) { func (p *Parser) Help() ([]byte, []byte) {
return p.l.b[p.offsets[0]:p.offsets[1]], p.text m := p.l.b[p.offsets[0]:p.offsets[1]]
// Replacer causes allocations. Replace only when necessary.
if strings.IndexByte(yoloString(p.text), byte('\\')) >= 0 {
return m, []byte(helpReplacer.Replace(string(p.text)))
}
return m, p.text
} }
// Type returns the metric name and type in the current entry. // Type returns the metric name and type in the current entry.
@ -202,7 +208,7 @@ func (p *Parser) Metric(l *labels.Labels) string {
// Replacer causes allocations. Replace only when necessary. // Replacer causes allocations. Replace only when necessary.
if strings.IndexByte(s[c:d], byte('\\')) >= 0 { if strings.IndexByte(s[c:d], byte('\\')) >= 0 {
*l = append(*l, labels.Label{Name: s[a:b], Value: replacer.Replace(s[c:d])}) *l = append(*l, labels.Label{Name: s[a:b], Value: lvalReplacer.Replace(s[c:d])})
continue continue
} }
*l = append(*l, labels.Label{Name: s[a:b], Value: s[c:d]}) *l = append(*l, labels.Label{Name: s[a:b], Value: s[c:d]})
@ -394,12 +400,15 @@ func (p *Parser) parseLVals() error {
} }
} }
var replacer = strings.NewReplacer( var lvalReplacer = strings.NewReplacer(
`\"`, `"`, `\"`, "\"",
`\\`, `\`, `\\`, "\\",
`\n`, ` `\n`, "\n",
`, )
`\t`, ` `,
var helpReplacer = strings.NewReplacer(
`\\`, "\\",
`\n`, "\n",
) )
func yoloString(b []byte) string { func yoloString(b []byte) string {