(s string)
| 460 | } |
| 461 | |
| 462 | func unquote(s string) (string, error) { |
| 463 | r := []rune(sanitize(s)) |
| 464 | if r[0] != '"' || r[len(r)-1] != '"' { |
| 465 | return "", fmt.Errorf("expected given string to be enclosed in double quotes: %q", r) |
| 466 | } |
| 467 | var unquotedStrBuilder strings.Builder |
| 468 | noQuotes := r[1 : len(r)-1] |
| 469 | for i := 0; i < len(noQuotes); { |
| 470 | c := noQuotes[i] |
| 471 | hasNext := i+1 < len(noQuotes) |
| 472 | if c == '\\' { |
| 473 | if hasNext { |
| 474 | nextChar := noQuotes[i+1] |
| 475 | switch nextChar { |
| 476 | case 'a': |
| 477 | unquotedStrBuilder.WriteRune('\a') |
| 478 | case 'b': |
| 479 | unquotedStrBuilder.WriteRune('\b') |
| 480 | case 'f': |
| 481 | unquotedStrBuilder.WriteRune('\f') |
| 482 | case 'n': |
| 483 | unquotedStrBuilder.WriteRune('\n') |
| 484 | case 'r': |
| 485 | unquotedStrBuilder.WriteRune('\r') |
| 486 | case 't': |
| 487 | unquotedStrBuilder.WriteRune('\t') |
| 488 | case 'v': |
| 489 | unquotedStrBuilder.WriteRune('\v') |
| 490 | case '\\': |
| 491 | unquotedStrBuilder.WriteRune('\\') |
| 492 | case '"': |
| 493 | unquotedStrBuilder.WriteRune('"') |
| 494 | default: |
| 495 | unquotedStrBuilder.WriteRune(c) |
| 496 | unquotedStrBuilder.WriteRune(nextChar) |
| 497 | } |
| 498 | i += 2 |
| 499 | continue |
| 500 | } |
| 501 | } |
| 502 | unquotedStrBuilder.WriteRune(c) |
| 503 | i++ |
| 504 | } |
| 505 | return unquotedStrBuilder.String(), nil |
| 506 | } |
| 507 | |
| 508 | func TestQuoteUnquote(t *testing.T) { |
| 509 | tests := []struct { |
no test coverage detected