unescape reverses the "C" escaping that protoc does for default values of bytes fields. It is best effort in that it effectively ignores malformed input. Seemingly invalid escape sequences are conveyed, unmodified, into the decoded result.
(s string)
| 2451 | // It is best effort in that it effectively ignores malformed input. Seemingly invalid escape |
| 2452 | // sequences are conveyed, unmodified, into the decoded result. |
| 2453 | func unescape(s string) string { |
| 2454 | // NB: Sadly, we can't use strconv.Unquote because protoc will escape both |
| 2455 | // single and double quotes, but strconv.Unquote only allows one or the |
| 2456 | // other (based on actual surrounding quotes of its input argument). |
| 2457 | |
| 2458 | var out []byte |
| 2459 | for len(s) > 0 { |
| 2460 | // regular character, or too short to be valid escape |
| 2461 | if s[0] != '\\' || len(s) < 2 { |
| 2462 | out = append(out, s[0]) |
| 2463 | s = s[1:] |
| 2464 | } else if c := escapeChars[s[1]]; c != 0 { |
| 2465 | // escape sequence |
| 2466 | out = append(out, c) |
| 2467 | s = s[2:] |
| 2468 | } else if s[1] == 'x' || s[1] == 'X' { |
| 2469 | // hex escape, e.g. "\x80 |
| 2470 | if len(s) < 4 { |
| 2471 | // too short to be valid |
| 2472 | out = append(out, s[:2]...) |
| 2473 | s = s[2:] |
| 2474 | continue |
| 2475 | } |
| 2476 | v, err := strconv.ParseUint(s[2:4], 16, 8) |
| 2477 | if err != nil { |
| 2478 | out = append(out, s[:4]...) |
| 2479 | } else { |
| 2480 | out = append(out, byte(v)) |
| 2481 | } |
| 2482 | s = s[4:] |
| 2483 | } else if '0' <= s[1] && s[1] <= '7' { |
| 2484 | // octal escape, can vary from 1 to 3 octal digits; e.g., "\0" "\40" or "\164" |
| 2485 | // so consume up to 2 more bytes or up to end-of-string |
| 2486 | n := len(s[1:]) - len(strings.TrimLeft(s[1:], "01234567")) |
| 2487 | if n > 3 { |
| 2488 | n = 3 |
| 2489 | } |
| 2490 | v, err := strconv.ParseUint(s[1:1+n], 8, 8) |
| 2491 | if err != nil { |
| 2492 | out = append(out, s[:1+n]...) |
| 2493 | } else { |
| 2494 | out = append(out, byte(v)) |
| 2495 | } |
| 2496 | s = s[1+n:] |
| 2497 | } else { |
| 2498 | // bad escape, just propagate the slash as-is |
| 2499 | out = append(out, s[0]) |
| 2500 | s = s[1:] |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | return string(out) |
| 2505 | } |
| 2506 | |
| 2507 | func (g *Generator) generateExtension(ext *ExtensionDescriptor) { |
| 2508 | ccTypeName := ext.DescName() |
no outgoing calls
no test coverage detected