(String string)
| 522 | } |
| 523 | |
| 524 | private static String quote(String string) { |
| 525 | StringBuilder product = |
| 526 | new StringBuilder(string.length() + 2); // two extra chars for " on either side |
| 527 | product.append('"'); |
| 528 | int length = string.length(); |
| 529 | char prev = 0; |
| 530 | for (int i = 0; i < length; i++) { |
| 531 | char c = string.charAt(i); |
| 532 | switch (c) { |
| 533 | case '"': |
| 534 | product.append("\\\""); |
| 535 | break; |
| 536 | case '\\': |
| 537 | product.append("\\\\"); |
| 538 | break; |
| 539 | case '\b': |
| 540 | product.append("\\b"); |
| 541 | break; |
| 542 | case '\f': |
| 543 | product.append("\\f"); |
| 544 | break; |
| 545 | case '\n': |
| 546 | product.append("\\n"); |
| 547 | break; |
| 548 | case '\r': |
| 549 | product.append("\\r"); |
| 550 | break; |
| 551 | case '\t': |
| 552 | product.append("\\t"); |
| 553 | break; |
| 554 | default: |
| 555 | if (isLeadingSurrogate(c) |
| 556 | && i < length - 1 |
| 557 | && isTrailingSurrogate(string.charAt(i + 1))) { |
| 558 | // do nothing as the next case will add both surrogates |
| 559 | break; |
| 560 | } else if (isTrailingSurrogate(c) && isLeadingSurrogate(prev)) { |
| 561 | product.append(prev).append(c); |
| 562 | } else if (c < ' ' || isLeadingSurrogate(c) || isTrailingSurrogate(c)) { |
| 563 | product.append("\\u"); |
| 564 | String hex = String.format("%04x", Integer.valueOf(c)); |
| 565 | product.append(hex); |
| 566 | } else { |
| 567 | product.append(c); |
| 568 | } |
| 569 | break; |
| 570 | } |
| 571 | prev = c; |
| 572 | } |
| 573 | product.append('"'); |
| 574 | return product.toString(); |
| 575 | } |
| 576 | |
| 577 | static boolean isLeadingSurrogate(char c) { |
| 578 | return c >= 0xD800 && c <= 0xDBFF; |
no test coverage detected