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)
| 397 | // as a key for a map or in general some output that |
| 398 | // cannot span multiple lines or have weird characters. |
| 399 | func quote(key string) string { |
| 400 | // strconv.Quote does not quote an empty string so we need this. |
| 401 | if key == "" { |
| 402 | return `""` |
| 403 | } |
| 404 | |
| 405 | var hasSpace bool |
| 406 | for _, r := range key { |
| 407 | if unicode.IsSpace(r) { |
| 408 | hasSpace = true |
| 409 | break |
| 410 | } |
| 411 | } |
| 412 | quoted := strconv.Quote(key) |
| 413 | // If the key doesn't need to be quoted, don't quote it. |
| 414 | // We do not use strconv.CanBackquote because it doesn't |
| 415 | // account tabs. |
| 416 | if !hasSpace && quoted[1:len(quoted)-1] == key { |
| 417 | return key |
| 418 | } |
| 419 | return quoted |
| 420 | } |
| 421 | |
| 422 | func quoteKey(key string) string { |
| 423 | // Replace spaces in the map keys with underscores. |
no outgoing calls
no test coverage detected