| 22 | } |
| 23 | |
| 24 | func HardQuote(s string) string { |
| 25 | if s == "" { |
| 26 | return "\"\"" |
| 27 | } |
| 28 | |
| 29 | if safePattern.MatchString(s) { |
| 30 | return s |
| 31 | } |
| 32 | |
| 33 | if !checkQuoteSize(s) { |
| 34 | return "" |
| 35 | } |
| 36 | |
| 37 | buf := make([]byte, 0, len(s)+5) |
| 38 | buf = append(buf, '"') |
| 39 | |
| 40 | for i := 0; i < len(s); i++ { |
| 41 | switch s[i] { |
| 42 | case '"', '\\', '$', '`': |
| 43 | buf = append(buf, '\\', s[i]) |
| 44 | default: |
| 45 | buf = append(buf, s[i]) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | buf = append(buf, '"') |
| 50 | return string(buf) |
| 51 | } |
| 52 | |
| 53 | // does not encode newlines or backticks |
| 54 | func HardQuoteFish(s string) string { |