quotes quotes a string so that it is suitable as a key for a map or in general some output that cannot span multiple lines or have weird characters.
(key string)
| 757 | // as a key for a map or in general some output that |
| 758 | // cannot span multiple lines or have weird characters. |
| 759 | func quote(key string) string { |
| 760 | // strconv.Quote does not quote an empty string so we need this. |
| 761 | if key == "" { |
| 762 | return `""` |
| 763 | } |
| 764 | |
| 765 | var hasSpace bool |
| 766 | for _, r := range key { |
| 767 | if unicode.IsSpace(r) { |
| 768 | hasSpace = true |
| 769 | break |
| 770 | } |
| 771 | } |
| 772 | quoted := strconv.Quote(key) |
| 773 | // If the key doesn't need to be quoted, don't quote it. |
| 774 | // We do not use strconv.CanBackquote because it doesn't |
| 775 | // account tabs. |
| 776 | if !hasSpace && quoted[1:len(quoted)-1] == key { |
| 777 | return key |
| 778 | } |
| 779 | return quoted |
| 780 | } |