/* Same as "_writeString2()", except needs additional escaping
for subset of characters
188582
private void _writeStringCustom(final int len)
			throws IOException, JsonGenerationException
	{
		// And then we'll need to verify need for escaping etc:
		int end = _outputTail + len;
		final int[] escCodes = _outputEscapes;
		final int maxNonEscaped = (_maximumNonEscapedChar < 1) ? 0xFFFF : _maximumNonEscapedChar;
		final int escLimit = Math.min(escCodes.length, maxNonEscaped+1);
		int escCode = 0;
		final CharacterEscapes customEscapes = _characterEscapes;

		output_loop:
		while (_outputTail < end) {
			char c;
			// Fast loop for chars not needing escaping
			escape_loop:
			while (true) {
				c = _outputBuffer[_outputTail];
				if (c < escLimit) {
					escCode = escCodes[c];
					if (escCode != 0) {
						break escape_loop;
					}
				} else if (c > maxNonEscaped) {
					escCode = CharacterEscapes.ESCAPE_STANDARD;
					break escape_loop;
				} else {
					if ((_currentEscape = customEscapes.getEscapeSequence(c)) != null) {
						escCode = CharacterEscapes.ESCAPE_CUSTOM;
						break escape_loop;
					}
				}
				if (++_outputTail >= end) {
					break output_loop;
				}
			}
			int flushLen = (_outputTail - _outputHead);
			if (flushLen > 0) {
				_writer.write(_outputBuffer, _outputHead, flushLen);
			}
			++_outputTail;
			_prependOrWriteCharacterEscape(c, escCode);
		}
	}
1090178
@Override
	public void writeString(String str) throws IOException {
		byte[] bytes = Utf8.getBytesFor(str);
		writeBytes(bytes, 0, bytes.length);
	}
450792
void writeString(String value) throws IOException {
        output.append('"');
        for (int i = 0; i < value.length(); i++) {
            char ch = value.charAt(i);
            if (ch < 128) {
                String replace = REPLACE[ch];
                if (replace != null) {
                    output.append(replace);
                } else {
                    output.append(ch);
                }
            } else if (ch == '\u2028') {
                output.append("\\u2028");  // match other JSON writers
            } else if (ch == '\u2029') {
                output.append("\\u2029");  // match other JSON writers
            } else {
                output.append(ch);
            }
        }
        output.append('"');
    }
345969
function writeTxt(buff, val) {
  //TODO XXX FIXME -- split on max char string and loop
  assertUndefined(val.data, 'TXT record requires "data"');
  for (var i=0,len=val.data.length; i<len; i++) {
    var dataLen = Buffer.byteLength(val.data[i], 'utf8');
    buff.writeUInt8(dataLen);
    buff.write(val.data[i], dataLen, 'utf8');
  }
  return WRITE_RESOURCE_DONE;
}
382497
private static String escapeJavaScriptChars(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            switch (ch) {
                case '\b':
                    sb.append("\\b");
                    break;
                case '\t':
                    sb.append("\\t");
                    break;
                case '\n':
                    sb.append("\\n");
                    break;
                case '\f':
                    sb.append("\\f");
                    break;
                case '\r':
                    sb.append("\\r");
                    break;
                case '"':
                    sb.append("\\\"");
                    break;
                case '\'':
                    sb.append("\\\'");
                    break;
                case '\\':
                    sb.append("\\\\");
                    break;
                default:
                    if (ch < 32 || ch >= 127) {
                        sb.append(String.format("\\u%04X", (int)ch));
                    } else {
                        sb.append(ch);
                    }
                    break;
            }
        }
        return sb.toString();
    }
310698
private static void escape(OutputAccessor out, int charToEscape) {

        out.write('\\');
        out.write('u');
        if (charToEscape > 0xFF) {
            int hi = (charToEscape >> 8) & 0xFF;
            out.write(HEX_BYTES[hi >> 4]);
            out.write(HEX_BYTES[hi & 0xF]);
            charToEscape &= 0xFF;
        } else {
            out.write((byte) '0');
            out.write((byte) '0');
        }
        // We know it's a control char, so only the last 2 chars are non-0
        out.write(HEX_BYTES[charToEscape >> 4]);
        out.write(HEX_BYTES[charToEscape & 0xF]);
    }
1146547
public String escape(String in) {
    StringBuffer out = new StringBuffer();

    for (char c : in.toCharArray()) {
      switch (c) {
        case '<':
          out.append("&lt;");
          break;
        case '>':
          out.append("&gt;");
          break;
        case '&':
          out.append("&amp;");
          break;
        case '"':
          out.append("&quot;");
          break;
        default:
          out.append(c);
      }
    }

    return out.toString();
  }
993297
private void simpleWriteString(String outString, OutputStream outStream) throws IOException
   {
      for (int i = 0; i < outString.length(); i++)
      {
         int charCode = outString.charAt(i);
         outStream.write(charCode & 0xFF);
         outStream.write((charCode >> 8) & 0xFF);
      }
   }
740644
protected int _writeCString(String string) throws IOException {
		//escape characters if necessary
		string = escapeCharacters(string);
		int l = _buffer.putUTF8(string);
		_buffer.putByte(BsonConstants.END_OF_STRING);
		return l + 1;
	}
174252
static void escapeString(byte b[], ByteBuffer content) {
        content.append_i('(');
        for (int k = 0; k < b.length; ++k) {
            byte c = b[k];
            switch (c) {
                case '\r':
                    content.append("\\r");
                    break;
                case '\n':
                    content.append("\\n");
                    break;
                case '\t':
                    content.append("\\t");
                    break;
                case '\b':
                    content.append("\\b");
                    break;
                case '\f':
                    content.append("\\f");
                    break;
                case '(':
                case ')':
                case '\\':
                    content.append_i('\\').append_i(c);
                    break;
                default:
                    content.append_i(c);
            }
        }
        content.append(")");
    }
1351793
private static String escape(String string) {
    return StringUtils.escapeString(string, StringUtils.ESCAPE_CHAR, 
                                    charsToEscape);
  }
958658
public static String escapeString(String data) {
    return StringUtils.escapeString(data, StringUtils.ESCAPE_CHAR, 
                                    charsToEscape);
  }
38908
def write(self, string):
        """"""
        self._output += self._options.indentation_character * \
            self._indentation + string + '\n'
162884
private final void _appendCharacterEscape(char ch, int escCode)
        throws IOException, JsonGenerationException
    {
        if (escCode >= 0) { // \\N (2 char)
            if ((_outputTail + 2) > _outputEnd) {
                _flushBuffer();
            }
            _outputBuffer[_outputTail++] = '\\';
            _outputBuffer[_outputTail++] = (char) escCode;
            return;
        }
        if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
            if ((_outputTail + 2) > _outputEnd) {
                _flushBuffer();
            }
            int ptr = _outputTail;
            char[] buf = _outputBuffer;
            buf[ptr++] = '\\';
            buf[ptr++] = 'u';
            // We know it's a control char, so only the last 2 chars are non-0
            if (ch > 0xFF) { // beyond 8 bytes
                int hi = (ch >> 8) & 0xFF;
                buf[ptr++] = HEX_CHARS[hi >> 4];
                buf[ptr++] = HEX_CHARS[hi & 0xF];
                ch &= 0xFF;
            } else {
                buf[ptr++] = '0';
                buf[ptr++] = '0';
            }
            buf[ptr++] = HEX_CHARS[ch >> 4];
            buf[ptr] = HEX_CHARS[ch & 0xF];
            _outputTail = ptr;
            return;
        }
    }
961856
private static String escapeString(String s, char quote,
                          String doublequoteEscape,
                          String singlequoteEscape,
                          String backslashEscape) {
    StringBuilder sb = new StringBuilder(s.length() + 2);
    sb.append(quote);
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      switch (c) {
        case '\n': sb.append("\\n"); break;
        case '\r': sb.append("\\r"); break;
        case '\t': sb.append("\\t"); break;
        case '\\': sb.append(backslashEscape); break;
        case '\"': sb.append(doublequoteEscape); break;
        case '\'': sb.append(singlequoteEscape); break;
        case '>':
          // Unicode-escape the '>' in '-->' and ']]>'
          if (i >= 2
              && ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-')
                  || (s.charAt(i - 1) == ']' && s.charAt(i - 2) == ']'))) {
            sb.append("\\u003e");
          } else {
            sb.append(c);
          }
          break;
        case '<':
          // Unicode-escape the '<' in '</script' and '<!--'
          final String END_SCRIPT = "/script";
          final String START_COMMENT = "!--";

          if (s.regionMatches(true, i + 1, END_SCRIPT, 0,
                              END_SCRIPT.length())) {
            sb.append("\\u003c");
          } else if (s.regionMatches(false, i + 1, START_COMMENT, 0,
                                     START_COMMENT.length())) {
            sb.append("\\u003c");
          } else {
            sb.append(c);
          }
          break;
        default:
          // No charsetEncoder provided - pass straight Latin characters
          // through, and escape the rest.  Doing the explicit character
          // check is measurably faster than using the CharsetEncoder.
          if (c > 0x1f && c <= 0x7f) {
            sb.append(c);
          } else {
            // Other characters can be misinterpreted by some JS parsers,
            // or perhaps mangled by proxies along the way,
            // so we play it safe and Unicode escape them.
            appendHexJavaScriptRepresentation(sb, c);
          }
      }
    }
    sb.append(quote);
    return sb.toString();
  }
1135235
private void writeQuotedAndEscaped(CharSequence string) {
        if (string != null && string.length() != 0) {
            int len = string.length();
            writer.write('\"');

            for (int i = 0; i < len; ++i) {
                char cp = string.charAt(i);
                if ((cp < 0x7f &&
                     cp >= 0x20 &&
                     cp != '\"' &&
                     cp != '\\') ||
                    (cp > 0x7f &&
                     isConsolePrintable(cp) &&
                     !isSurrogate(cp))) {
                    // quick bypass for direct printable chars.
                    writer.write(cp);
                } else {
                    switch (cp) {
                        case '\b':
                            writer.write("\\b");
                            break;
                        case '\t':
                            writer.write("\\t");
                            break;
                        case '\n':
                            writer.write("\\n");
                            break;
                        case '\f':
                            writer.write("\\f");
                            break;
                        case '\r':
                            writer.write("\\r");
                            break;
                        case '\"':
                        case '\\':
                            writer.write('\\');
                            writer.write(cp);
                            break;
                        default:
                            if (isSurrogate(cp) && (i + 1) < len) {
                                char c2 = string.charAt(i + 1);
                                writer.format("\\u%04x", (int) cp);
                                writer.format("\\u%04x", (int) c2);
                                ++i;
                            } else {
                                writer.format("\\u%04x", (int) cp);
                            }
                            break;
                    }
                }
            }

            writer.write('\"');
        } else {
            writer.write("\"\"");
        }
    }
1301035
private void _appendCharacterEscape(char ch, int escCode)
			throws IOException, JsonGenerationException
	{
		if (escCode >= 0) { // \\N (2 char)
			if ((_outputTail + 2) > _outputEnd) {
				_flushBuffer();
			}
			_outputBuffer[_outputTail++] = '\\';
			_outputBuffer[_outputTail++] = (char) escCode;
			return;
		}
		if (escCode != CharacterEscapes.ESCAPE_CUSTOM) { // std, \\uXXXX
			if ((_outputTail + 5) >= _outputEnd) {
				_flushBuffer();
			}
			int ptr = _outputTail;
			char[] buf = _outputBuffer;
			buf[ptr++] = '\\';
			buf[ptr++] = 'u';
			// We know it's a control char, so only the last 2 chars are non-0
			if (ch > 0xFF) { // beyond 8 bytes
				int hi = (ch >> 8) & 0xFF;
				buf[ptr++] = HEX_CHARS[hi >> 4];
				buf[ptr++] = HEX_CHARS[hi & 0xF];
				ch &= 0xFF;
			} else {
				buf[ptr++] = '0';
				buf[ptr++] = '0';
			}
			buf[ptr++] = HEX_CHARS[ch >> 4];
			buf[ptr++] = HEX_CHARS[ch & 0xF];
			_outputTail = ptr;
			return;
		}
		String escape;
		if (_currentEscape == null) {
			escape = _characterEscapes.getEscapeSequence(ch).getValue();
		} else {
			escape = _currentEscape.getValue();
			_currentEscape = null;
		}
		int len = escape.length();
		if ((_outputTail + len) > _outputEnd) {
			_flushBuffer();
			if (len > _outputEnd) { // very very long escape; unlikely but theoretically possible
				_writer.write(escape);
				return;
			}
		}
		escape.getChars(0, len, _outputBuffer, _outputTail);
		_outputTail += len;
	}