(s string)
| 5 | ) |
| 6 | |
| 7 | func StringEscape(s string) string { |
| 8 | |
| 9 | b := make([]byte, 0) |
| 10 | |
| 11 | var index int |
| 12 | |
| 13 | // 表中直接使用换行会干扰最终合并文件格式, 所以转成\n,由pbt文本解析层转回去 |
| 14 | for index < len(s) { |
| 15 | c := s[index] |
| 16 | |
| 17 | switch c { |
| 18 | case '"': |
| 19 | b = append(b, '\\') |
| 20 | b = append(b, '"') |
| 21 | case '\n': |
| 22 | b = append(b, '\\') |
| 23 | b = append(b, 'n') |
| 24 | case '\r': |
| 25 | b = append(b, '\\') |
| 26 | b = append(b, 'r') |
| 27 | case '\\': |
| 28 | |
| 29 | var nextChar byte |
| 30 | if index+1 < len(s) { |
| 31 | nextChar = s[index+1] |
| 32 | } |
| 33 | |
| 34 | b = append(b, '\\') |
| 35 | |
| 36 | switch nextChar { |
| 37 | case 'n', 'r': |
| 38 | default: |
| 39 | b = append(b, c) |
| 40 | } |
| 41 | |
| 42 | default: |
| 43 | b = append(b, c) |
| 44 | } |
| 45 | |
| 46 | index++ |
| 47 | |
| 48 | } |
| 49 | |
| 50 | return string(b) |
| 51 | } |
| 52 | |
| 53 | func StringWrap(s string) string { |
| 54 | return fmt.Sprintf("\"%s\"", s) |
no outgoing calls
no test coverage detected