/* ast_process_quotes_and_slashes rewritten to be java friendly
512906
private static String processQuotesAndSlashes(String start, char find, char replace_with)
    {
        String dataPut = "";
        int inEscape = 0;
        int inQuotes = 0;

        char[] startChars = start.toCharArray();
        for (char startChar : startChars)
        {
            if (inEscape != 0)
            {
                dataPut += startChar; /* Always goes verbatim */
                inEscape = 0;
            }
            else
            {
                if (startChar == '\\')
                {
                    inEscape = 1; /* Do not copy \ into the data */
                }
                else if (startChar == '\'')
                {
                    inQuotes = 1 - inQuotes; /* Do not copy ' into the data */
                }
                else
                {
                    /* Replace , with |, unless in quotes */
                    dataPut += inQuotes != 0 ? startChar : ((startChar == find) ? replace_with : startChar);
                }
            }
        }
        return dataPut;
    }
305599
private static EscapeStringResult escapeString(String s, char quote) {
        final String[] octal_strings = {
            "\\0",   "\\001", "\\002", "\\003", "\\004", "\\005", "\\006", "\\a",
            "\\b",   "\\t",   "\\n",   "\\v",   "\\f",   "\\r",   "\\016", "\\017",
            "\\020", "\\021", "\\022", "\\023", "\\024", "\\025", "\\026", "\\027",
            "\\030", "\\031", "\\032", "\\033", "\\034", "\\035", "\\036", "\\037",
        };

        StringBuilder result = new StringBuilder(s);
        boolean needQuotes = s.isEmpty();
        for (int i = 0, next_i; i < result.length(); i = next_i) {
            final int code_point = result.codePointAt(i);
            next_i = result.offsetByCodePoints(i, 1);

            if (code_point == 0) {
                result.replace(i, next_i, "\\0");
                next_i = i + 2;
                needQuotes = true;
            } else if (code_point == (int)'\\') {
                result.replace(i, next_i, "\\\\");
                next_i = i + 2;
                needQuotes = true;
            } else if (code_point == (int)quote) {
                result.replace(i, next_i, "\\" + quote);
                next_i = i + 2;
                needQuotes = true;
            } else if (code_point < 32) {
                String octal = octal_strings[code_point];
                result.replace(i, next_i, octal);
                next_i = i + octal.length();
                needQuotes = true;
            } else if (code_point >= 65536) {
                String repl = String.format("\\U%08x", code_point);
                result.replace(i, next_i, repl);
                next_i = i + repl.length();
                needQuotes = true;
            } else if (code_point >= 128) {
                String repl = String.format("\\u%04x", code_point);
                result.replace(i, next_i, repl);
                next_i = i + repl.length();
                needQuotes = true;
            }
        }
        return new EscapeStringResult(needQuotes, result);
    }
980144
private static StringBuilder simpleQuote(final StringBuilder sb, final String value) {
		for (int i = 0; i < value.length(); ++i) {
			final char c = value.charAt(i);
			switch (c) {
				case '\\':
				case '^':
				case '$':
				case '.':
				case '|':
				case '?':
				case '*':
				case '+':
				case '(':
				case ')':
				case '[':
				case '{':
					sb.append('\\');
				default:
					sb.append(c);
			}
		}
		return sb;
	}
235061
function () {
                    var str, j = i, e;

                    if (input.charAt(j) === '~') { j++, e = true } // Escaped strings
                    if (input.charAt(j) !== '"' && input.charAt(j) !== "'") return;

                    e && $('~');

                    if (str = $(/^"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/)) {
                        return new(tree.Quoted)(str[0], str[1] || str[2], e);
                    }
                }
1195330
static Object appendQuotes(Object value)
    {
        if (value instanceof String || value instanceof Character)
        {
            StringBuilder builder = new StringBuilder();
            builder.append("\"");
            builder.append(value);
            builder.append("\"");
            return builder.toString();
        }
        return value;
    }
984222
function stringifyString( v , runtime , isTemplateSentence ) {
	var maybeDollar = '' ;

	if ( isTemplateSentence ) {
		if ( v.key ) {
			v = v.key ;
			maybeDollar = '$' ;
		}
		else {
			runtime.str += runtime.depth ? ' <Sentence>' : '<Sentence>' ;
			return ;
		}
	}

	if ( runtime.preferQuotes ) {
		return stringifyStringMaybeQuotes( v , runtime , maybeDollar ) ;
	}

	return stringifyStringMaybeStringLine( v , runtime , maybeDollar ) ;

}
92320
public static String quoteEscaped(final String str) {
        if (N.isNullOrEmpty(str)) {
            return str;
        }

        final StringBuilder sb = Objectory.createStringBuilder();
        final char[] chars = getCharsForReadOnly(str);

        try {
            char ch = 0;
            for (int i = 0, len = str.length(); i < len; i++) {
                ch = chars[i];

                if ((ch == _BACKSLASH) && (i < (len - 1))) {
                    sb.append(ch);
                    sb.append(str.charAt(++i));
                } else if ((ch == _QUOTATION_S) || (ch == _QUOTATION_D)) {
                    sb.append(_BACKSLASH);
                    sb.append(ch);
                } else {
                    sb.append(ch);
                }
            }

            return sb.toString();
        } finally {
            Objectory.recycle(sb);
        }
    }
830861
private static String convertToQuotedString(String s) {
    if (s == null || s.isEmpty()) {
      return null;
    }
    // If already quoted, return as-is
    if (s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
      return s;
    }
    return '\"' + s + '\"';
  }
998572
private static Node makeForwardSlashBracketSafe(Node n) {
    String s = n.getString();
    // sb contains everything in s[0:pos]
    StringBuilder sb = null;
    int pos = 0;
    boolean isEscaped = false;
    boolean inCharset = false;
    for (int i = 0; i < s.length(); ++i) {
      char ch = s.charAt(i);
      switch (ch) {
        case '\\':
          isEscaped = !isEscaped;
          continue;
        case '/':
          // Escape a literal forward slash if it is not already escaped and is
          // not inside a character set.
          //     new RegExp('/') -> /\//
          // but the following do not need extra escaping
          //     new RegExp('\\/') -> /\//
          //     new RegExp('[/]') -> /[/]/
          if (!isEscaped && !inCharset) {
            if (null == sb) { sb = new StringBuilder(s.length() + 16); }
            sb.append(s, pos, i).append('\\');
            pos = i;
          }
          break;
        case '[':
          if (!isEscaped) {
            inCharset = true;
          }
          break;
        case ']':
          if (!isEscaped) {
            inCharset = false;
          }
          break;
        case '\r': case '\n': case '\u2028': case '\u2029':
          // LineTerminators cannot appear raw inside a regular
          // expression literal.
          // They can't appear legally in a quoted string, but when
          // the quoted string from
          //     new RegExp('\n')
          // reaches here, the quoting has been removed.
          // Requote just these code-points.
          if (null == sb) { sb = new StringBuilder(s.length() + 16); }
          if (isEscaped) {
            sb.append(s, pos, i - 1);
          } else {
            sb.append(s, pos, i);
          }
          switch (ch) {
            case '\r': sb.append("\\r"); break;
            case '\n': sb.append("\\n"); break;
            case '\u2028': sb.append("\\u2028"); break;
            case '\u2029': sb.append("\\u2029"); break;
          }
          pos = i + 1;
          break;
      }
      isEscaped = false;
    }

    if (null == sb) { return n.cloneTree(); }

    sb.append(s, pos, s.length());
    return IR.string(sb.toString()).srcref(n);
  }
300552
function(str) {
      var quoteStack = [];
      var output = "";

      for(var i=0; i<str.length; i++) {
        var c = str.charAt(i);

        // Quote?
        if((c == '"' || c== "'") && (i==0 || str.charAt(i-1) != '\\')) {
          // Found non-escaped quote, either deepen stack or pop stack
          if(quoteStack[quoteStack.length-1] == c) quoteStack.pop();
          else quoteStack.push(c);
          // Also push character
          output += c;
        }
        else if(c == " ") {
          // Found space, append to output only if quote stack populated
          if(quoteStack.length > 0) output += c;
        }
        else {
          // Found other char, append
          output += c;
        }
      }

     return output; // Strip spaces from query
   }
717361
function(args) {
            args = this.trim(args, '&\?#');

            var parts = args.split('&');
            var output = {};

            var key;
            var value;
            var equalsSegments;
            var lim = parts.length;
            for (var i=0,l=lim; i<l; i++) {
                equalsSegments = parts[i].split('=');
                key = decodeURIComponent(equalsSegments.shift());
                value = equalsSegments.join("=");
                output[key] = decodeURIComponent(value);
            }
            return output;
        }
1254885
function() {
        var str = "",
          quote = exprI(index++),
          closed = false,
          ch;

        while (index < length) {
          ch = exprI(index++);
          if (ch === quote) {
            closed = true;
            break;
          } else if (ch === "\\") {
            // Check for all of the common escape codes
            ch = exprI(index++);
            switch (ch) {
              case "n":
                str += "\n";
                break;
              case "r":
                str += "\r";
                break;
              case "t":
                str += "\t";
                break;
              case "b":
                str += "\b";
                break;
              case "f":
                str += "\f";
                break;
              case "v":
                str += "\x0B";
                break;
              default:
                str += "\\" + ch;
            }
          } else {
            str += ch;
          }
        }

        if (!closed) {
          throwError('Unclosed quote after "' + str + '"', index);
        }

        return {
          type: code.LITERAL,
          value: str,
          raw: quote + str + quote
        };
      }
17622
function process(content, encoding, attrs, node) {
    if (typeof content === 'undefined') {
      console.error(
        colors.red('process() : Undefined "content" parameters.')
      );
      return '';
    }
    if (!encoding || encoding === 'original') {
      return content;
    }
    var items = encoding.split(/\s*,\s*/);
    if (items.length > 1) {
      items.forEach(function (item) {
        content = process(content, item, attrs, node);
      });
      return content;
    }
    var processor = getProcessor(encoding);
    if (!processor) {
      console.error(
        colors.red('The "%s" processor does not exist.'), encoding
      );
      return content;
    }
    return processor(content, attrs, instance, node);
  }
684189
static String _dumpAsString(
                                String indent,
                                DERObject obj)
    {
        if (obj instanceof ASN1Sequence)
        {
            StringBuilder buf = new StringBuilder();
            Enumeration e = ((ASN1Sequence) obj).getObjects();
            String tab = indent + TAB;

            buf.append(indent);
            if (obj instanceof BERConstructedSequence)
            {
                buf.append("BER ConstructedSequence");
            }
            else if (obj instanceof DERConstructedSequence)
            {
                buf.append("DER ConstructedSequence");
            }
            else if (obj instanceof BERSequence)
            {
                buf.append("BER Sequence");
            }
            else if (obj instanceof DERSequence)
            {
                buf.append("DER Sequence");
            }
            else
            {
                buf.append("Sequence");
            }

            buf.append(System.getProperty("line.separator"));

            while (e.hasMoreElements())
            {
                Object o = e.nextElement();

                if (o == null || o.equals(new DERNull()))
                {
                    buf.append(tab);
                    buf.append("NULL");
                    buf.append(System.getProperty("line.separator"));
                }
                else if (o instanceof DERObject)
                {
                    buf.append(_dumpAsString(tab, (DERObject) o));
                }
                else
                {
                    buf.append(_dumpAsString(tab, ((DEREncodable) o).getDERObject()));
                }
            }
            return buf.toString();
        }
        else if (obj instanceof DERTaggedObject)
        {
            StringBuilder buf = new StringBuilder();
            String tab = indent + TAB;

            buf.append(indent);
            if (obj instanceof BERTaggedObject)
            {
                buf.append("BER Tagged [");
            }
            else
            {
                buf.append("Tagged [");
            }

            DERTaggedObject o = (DERTaggedObject) obj;

            buf.append(Integer.toString(o.getTagNo()));
            buf.append("]");

            if (!o.isExplicit())
            {
                buf.append(" IMPLICIT ");
            }

            buf.append(System.getProperty("line.separator"));

            if (o.isEmpty())
            {
                buf.append(tab);
                buf.append("EMPTY");
                buf.append(System.getProperty("line.separator"));
            }
            else
            {
                buf.append(_dumpAsString(tab, o.getObject()));
            }

            return buf.toString();
        }
        else if (obj instanceof DERConstructedSet)
        {
            StringBuilder buf = new StringBuilder();
            Enumeration e = ((ASN1Set) obj).getObjects();
            String tab = indent + TAB;

            buf.append(indent);
            buf.append("ConstructedSet");
            buf.append(System.getProperty("line.separator"));

            while (e.hasMoreElements())
            {
                Object o = e.nextElement();

                if (o == null)
                {
                    buf.append(tab);
                    buf.append("NULL");
                    buf.append(System.getProperty("line.separator"));
                }
                else if (o instanceof DERObject)
                {
                    buf.append(_dumpAsString(tab, (DERObject) o));
                }
                else
                {
                    buf.append(_dumpAsString(tab, ((DEREncodable) o).getDERObject()));
                }
            }
            return buf.toString();
        }
        else if (obj instanceof BERSet)
        {
            StringBuilder buf = new StringBuilder();
            Enumeration e = ((ASN1Set) obj).getObjects();
            String tab = indent + TAB;

            buf.append(indent);
            buf.append("BER Set");
            buf.append(System.getProperty("line.separator"));

            while (e.hasMoreElements())
            {
                Object o = e.nextElement();

                if (o == null)
                {
                    buf.append(tab);
                    buf.append("NULL");
                    buf.append(System.getProperty("line.separator"));
                }
                else if (o instanceof DERObject)
                {
                    buf.append(_dumpAsString(tab, (DERObject) o));
                }
                else
                {
                    buf.append(_dumpAsString(tab, ((DEREncodable) o).getDERObject()));
                }
            }
            return buf.toString();
        }
        else if (obj instanceof DERSet)
        {
            StringBuilder buf = new StringBuilder();
            Enumeration e = ((ASN1Set) obj).getObjects();
            String tab = indent + TAB;

            buf.append(indent);
            buf.append("DER Set");
            buf.append(System.getProperty("line.separator"));

            while (e.hasMoreElements())
            {
                Object o = e.nextElement();

                if (o == null)
                {
                    buf.append(tab);
                    buf.append("NULL");
                    buf.append(System.getProperty("line.separator"));
                }
                else if (o instanceof DERObject)
                {
                    buf.append(_dumpAsString(tab, (DERObject) o));
                }
                else
                {
                    buf.append(_dumpAsString(tab, ((DEREncodable) o).getDERObject()));
                }
            }
            return buf.toString();
        }
        else if (obj instanceof DERObjectIdentifier)
        {
            return indent + "ObjectIdentifier(" + ((DERObjectIdentifier) obj).getId() + ")" + System.getProperty("line.separator");
        }
        else if (obj instanceof DERBoolean)
        {
            return indent + "Boolean(" + ((DERBoolean) obj).isTrue() + ")" + System.getProperty("line.separator");
        }
        else if (obj instanceof DERInteger)
        {
            return indent + "Integer(" + ((DERInteger) obj).getValue() + ")" + System.getProperty("line.separator");
        }
        else if (obj instanceof BERConstructedOctetString)
        {
            return indent + "BER Constructed Octet String" + "[" + ((ASN1OctetString) obj).getOctets().length + "] " + System.getProperty("line.separator");
        }
        else if (obj instanceof DEROctetString)
        {
            return indent + "DER Octet String" + "[" + ((ASN1OctetString) obj).getOctets().length + "] " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERBitString)
        {
            return indent + "DER Bit String" + "[" + ((DERBitString) obj).getBytes().length + ", " + ((DERBitString) obj).getPadBits() + "] "
                   + System.getProperty("line.separator");
        }
        else if (obj instanceof DERIA5String)
        {
            return indent + "IA5String(" + ((DERIA5String) obj).getString() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERUTF8String)
        {
            return indent + "UTF8String(" + ((DERUTF8String) obj).getString() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERPrintableString)
        {
            return indent + "PrintableString(" + ((DERPrintableString) obj).getString() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERVisibleString)
        {
            return indent + "VisibleString(" + ((DERVisibleString) obj).getString() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERBMPString)
        {
            return indent + "BMPString(" + ((DERBMPString) obj).getString() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERT61String)
        {
            return indent + "T61String(" + ((DERT61String) obj).getString() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERUTCTime)
        {
            return indent + "UTCTime(" + ((DERUTCTime) obj).getTime() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERGeneralizedTime)
        {
            return indent + "GeneralizedTime(" + ((DERGeneralizedTime) obj).getTime() + ") " + System.getProperty("line.separator");
        }
        else if (obj instanceof DERUnknownTag)
        {
            //TODO liberty if we keep this find Hex
//            return indent + "Unknown " + Integer.toString(((DERUnknownTag)obj).getTag(), 16) + " " + new String(Hex.encode(((DERUnknownTag)obj).getData())) + System.getProperty("line.separator");
            return indent + "Unknown " + Integer.toString(((DERUnknownTag) obj).getTag(), 16) + " " + new String(((DERUnknownTag) obj).getData())
                   + System.getProperty("line.separator");
        }
        else
        {
            return indent + obj.toString() + System.getProperty("line.separator");
        }
    }
863058
private void getQuotedString(String pattern, ParsePosition pos, boolean escapingOn)
    {

        appendQuotedString(pattern, pos, null, escapingOn);
    }
1068831
private static int quote(final StringBuilder target, final String string, final int start) {
        int i = quoteSingle(target, string, start);
        if (i < 0) i = quoteBlock(target, string, start, -i);
        return i;
    }
1082052
public static void quote(StringBuilder buf, String arg)
   {
      if (!mightNeedQuotes(arg)) {
         buf.append(arg);
         return;
      }
      buf.append('"');

      // The length of the current run of backslashes.
      int nPending = 0;

      for (int i = 0; i < arg.length(); i++) {
         char c = arg.charAt(i);

         if (c == '\\') {
            nPending++;
         }
         else {
            if (c == '"') {
               // Escape all the backslashes we've collected up till now.
               for (int j = 0; j < nPending; j++) {
                  buf.append('\\');
               }
               // Escape the quote.
               buf.append('\\');
            }
            nPending = 0;
         }

         buf.append(c);
      }

      // Escape all the backslashes that appear before the final closing quote.
      for (int j = 0; j < nPending; j++) {
         buf.append('\\');
      }

      buf.append('"');
   }