coreRuntime/src/sc/type/CTypeUtil.java
public static String escapeJavaString(String s, char delim, boolean escSlash) {
      StringBuilder sb = null;
      char c;

      if (s == null)
         return null;

      boolean escNewLines = delim != '`';

      int len = s.length();
      for (int i = 0; i < len; i++) {
         c = s.charAt(i);
         switch (c) {
            case '\b':
               sb = initsb(sb, s, i);
               sb.append("\\b");
               break;
            case '\f':
               sb = initsb(sb, s, i);
               sb.append("\\f");
               break;
            case '\t':
               sb = initsb(sb, s, i);
               sb.append("\\t");
               break;
            case '\n':
               if (escNewLines) {
                  sb = initsb(sb, s, i);
                  sb.append("\\n");
               }
               else if (sb != null)
                  sb.append(c);
               break;
            case '\r':
               if (escNewLines) {
                  sb = initsb(sb, s, i);
                  sb.append("\\r");
               }
               else if (sb != null)
                  sb.append(c);
               break;
            case '\'':
               if (delim == '\'') {
                  sb = initsb(sb, s, i);
                  sb.append("\\'");
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
            case '"':
               if (delim == '"') {
                  sb = initsb(sb, s, i);
                  sb.append("\\\"");
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
            case '`':
               if (delim == '`') {
                  sb = initsb(sb, s, i);
                  sb.append("\\`");
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
            case '\\':
               sb = initsb(sb, s, i);
               sb.append("\\\\");
               break;
            // TODO: we really only need to escape / when it follows <
            case '/':
               // When including a string literal inside of a script tag, need to escape the / so that </script> is not parsed out of the string literal
               if (escSlash) {
                  sb = initsb(sb, s, i);
                  sb.append("\\/");
                  break;
               }
               // else - fall through!
            default:
               if (c < 32 || c > 0x7f) {
                  sb = initsb(sb, s, i);
                  sb.append("\\u");
                  String hexRes = hex(c);
                  while (hexRes.length() < 4)
                     hexRes = "0" + hexRes;
                  sb.append(hexRes);
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
         }
      }
      if (sb != null)
         return sb.toString();
      return s;
   }
// Returns an escaped string using normal Java/JS rules for escaping for the given delimiter - either ', ", or ` (for js template literals)
Returns an escaped string using normal Java/JS rules for escaping for the given delimiter - either ', ", or ` (for js template literals)
Returns an escaped string using normal Java / JS rules for escaping for the given delimiter - either ' " or ` ( for js template literals )
public static String escapeJavaString(String s, char delim, boolean escSlash) {
      StringBuilder sb = null;
      char c;
      if (s == null)
         return null;
      boolean escNewLines = delim != '`';
      int len = s.length();
      for (int i = 0; i < len; i++) {
         c = s.charAt(i);
         switch (c) {
            case '\b':
               sb = initsb(sb, s, i);
               sb.append("\\b");
               break;
            case '\f':
               sb = initsb(sb, s, i);
               sb.append("\\f");
               break;
            case '\t':
               sb = initsb(sb, s, i);
               sb.append("\\t");
               break;
            case '\n':
               if (escNewLines) {
                  sb = initsb(sb, s, i);
                  sb.append("\\n");
               }
               else if (sb != null)
                  sb.append(c);
               break;
            case '\r':
               if (escNewLines) {
                  sb = initsb(sb, s, i);
                  sb.append("\\r");
               }
               else if (sb != null)
                  sb.append(c);
               break;
            case '\'':
               if (delim == '\'') {
                  sb = initsb(sb, s, i);
                  sb.append("\\'");
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
            case '"':
               if (delim == '"') {
                  sb = initsb(sb, s, i);
                  sb.append("\\\"");
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
            case '`':
               if (delim == '`') {
                  sb = initsb(sb, s, i);
                  sb.append("\\`");
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
            case '\\':
               sb = initsb(sb, s, i);
               sb.append("\\\\");
               break;
            case '/':
               if (escSlash) {
                  sb = initsb(sb, s, i);
                  sb.append("\\/");
                  break;
               }
            default:
               if (c < 32 || c > 0x7f) {
                  sb = initsb(sb, s, i);
                  sb.append("\\u");
                  String hexRes = hex(c);
                  while (hexRes.length() < 4)
                     hexRes = "0" + hexRes;
                  sb.append(hexRes);
               }
               else {
                  if (sb != null)
                     sb.append(c);
               }
               break;
         }
      }
      if (sb != null)
         return sb.toString();
      return s;
   }
public static String escapeJavaString ( String s , char delim , boolean escSlash ) { StringBuilder sb = null ; char c ; if ( s == null ) return null ; boolean escNewLines = delim != '`' ; int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { c = s . charAt ( i ) ; switch ( c ) { case '\b' : sb = initsb ( sb , s , i ) ; sb . append ( "\\b" ) ; break ; case '\f' : sb = initsb ( sb , s , i ) ; sb . append ( "\\f" ) ; break ; case '\t' : sb = initsb ( sb , s , i ) ; sb . append ( "\\t" ) ; break ; case '\n' : if ( escNewLines ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\n" ) ; } else if ( sb != null ) sb . append ( c ) ; break ; case '\r' : if ( escNewLines ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\r" ) ; } else if ( sb != null ) sb . append ( c ) ; break ; case '\'' : if ( delim == '\'' ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\'" ) ; } else { if ( sb != null ) sb . append ( c ) ; } break ; case '"' : if ( delim == '"' ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\\"" ) ; } else { if ( sb != null ) sb . append ( c ) ; } break ; case '`' : if ( delim == '`' ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\`" ) ; } else { if ( sb != null ) sb . append ( c ) ; } break ; case '\\' : sb = initsb ( sb , s , i ) ; sb . append ( "\\\\" ) ; break ; case '/' : if ( escSlash ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\/" ) ; break ; } default : if ( c < 32 || c > 0x7f ) { sb = initsb ( sb , s , i ) ; sb . append ( "\\u" ) ; String hexRes = hex ( c ) ; while ( hexRes . length ( ) < 4 ) hexRes = "0" + hexRes ; sb . append ( hexRes ) ; } else { if ( sb != null ) sb . append ( c ) ; } break ; } } if ( sb != null ) return sb . toString ( ) ; return s ; }
Returns an escaped string using normal Java/JS rules for escaping for the given delimiter - either ', ", or ` (for js template literals)
Returns an escaped string using normal Java / JS rules for escaping for the given delimiter - either ' " or ` ( for js template literals )
// TODO: we really only need to escape / when it follows < // When including a string literal inside of a script tag, need to escape the / so that </script> is not parsed out of the string literal // else - fall through!