MCPcopy Create free account
hub / github.com/mozilla/rhino / quote

Method quote

rhino/src/main/java/org/mozilla/javascript/NativeJSON.java:524–575  ·  view source on GitHub ↗
(String string)

Source from the content-addressed store, hash-verified

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;

Callers 3

strMethod · 0.95
joMethod · 0.95
regress-315974.jsFile · 0.80

Calls 8

isLeadingSurrogateMethod · 0.95
isTrailingSurrogateMethod · 0.95
formatMethod · 0.80
appendMethod · 0.65
toStringMethod · 0.65
lengthMethod · 0.45
charAtMethod · 0.45
valueOfMethod · 0.45

Tested by

no test coverage detected