private unsafe static bool TryWriteEncodedScalarAsNumericEntity(int unicodeScalar, char *buffer, int length, out int numberOfCharactersWritten) { Debug.Assert(buffer != null && length >= 0); if (UnicodeHelpers.IsSupplementaryCodePoint(unicodeScalar)) { // Convert this back to UTF-16 and write out both characters. char leadingSurrogate, trailingSurrogate; UnicodeHelpers.GetUtf16SurrogatePairFromAstralScalarValue(unicodeScalar, out leadingSurrogate, out trailingSurrogate); int leadingSurrogateCharactersWritten; if (TryWriteEncodedSingleCharacter(leadingSurrogate, buffer, length, out leadingSurrogateCharactersWritten) && TryWriteEncodedSingleCharacter(trailingSurrogate, buffer + leadingSurrogateCharactersWritten, length - leadingSurrogateCharactersWritten, out numberOfCharactersWritten) ) { numberOfCharactersWritten += leadingSurrogateCharactersWritten; return(true); } else { numberOfCharactersWritten = 0; return(false); } } else { // This is only a single character. return(TryWriteEncodedSingleCharacter(unicodeScalar, buffer, length, out numberOfCharactersWritten)); } }
// Writes an encoded scalar value (in the BMP) as a JavaScript-escaped character. private static unsafe bool TryWriteEncodedSingleCharacter(int unicodeScalar, char *buffer, int length, out int numberOfCharactersWritten) { Debug.Assert(buffer != null && length >= 0); Debug.Assert(!UnicodeHelpers.IsSupplementaryCodePoint(unicodeScalar), "The incoming value should've been in the BMP."); if (length < 6) { numberOfCharactersWritten = 0; return(false); } // Encode this as 6 chars "\uFFFF". *buffer = '\\'; buffer++; *buffer = 'u'; buffer++; *buffer = HexUtil.Int32LsbToHexDigit(unicodeScalar >> 12); buffer++; *buffer = HexUtil.Int32LsbToHexDigit((int)((unicodeScalar >> 8) & 0xFU)); buffer++; *buffer = HexUtil.Int32LsbToHexDigit((int)((unicodeScalar >> 4) & 0xFU)); buffer++; *buffer = HexUtil.Int32LsbToHexDigit((int)(unicodeScalar & 0xFU)); numberOfCharactersWritten = 6; return(true); }
private void EncodeCore(ref Writer writer, char *input, uint charsRemaining) { while (charsRemaining != 0) { int nextScalar = UnicodeHelpers.GetScalarValueFromUtf16(input, endOfString: (charsRemaining == 1)); if (UnicodeHelpers.IsSupplementaryCodePoint(nextScalar)) { // Supplementary characters should always be encoded numerically. WriteEncodedScalar(ref writer, (uint)nextScalar); // We consume two UTF-16 characters for a single supplementary character. input += 2; charsRemaining -= 2; } else { // Otherwise, this was a BMP character. input++; charsRemaining--; char c = (char)nextScalar; if (IsCharacterAllowed(c)) { writer.Write(c); } else { WriteEncodedScalar(ref writer, (uint)nextScalar); } } } }
public override bool WillEncode(int unicodeScalar) { if (UnicodeHelpers.IsSupplementaryCodePoint(unicodeScalar)) { return(true); } return(!_allowedCharacters.IsUnicodeScalarAllowed(unicodeScalar)); }
public override bool WillEncode(int unicodeScalar) { if (UnicodeHelpers.IsSupplementaryCodePoint(unicodeScalar)) { return(true); } Debug.Assert(unicodeScalar >= char.MinValue && unicodeScalar <= char.MaxValue); return(NeedsEscaping((char)unicodeScalar)); }
public override bool WillEncode(int unicodeScalar) { if (UnicodeHelpers.IsSupplementaryCodePoint(unicodeScalar)) { return(true); } Debug.Assert(unicodeScalar >= char.MinValue && unicodeScalar <= char.MaxValue); return(!_allowedCharacters.IsUnicodeScalarAllowed(unicodeScalar)); }
// Writes an encoded scalar value (in the BMP) as a JavaScript-escaped character. private static void WriteEncodedSingleCharacter(ref Writer writer, uint value) { Debug.Assert(!UnicodeHelpers.IsSupplementaryCodePoint((int)value), "The incoming value should've been in the BMP."); // Encode this as 6 chars "\uFFFF". writer.Write('\\'); writer.Write('u'); writer.Write(HexUtil.IntToChar(value >> 12)); writer.Write(HexUtil.IntToChar((value >> 8) & 0xFU)); writer.Write(HexUtil.IntToChar((value >> 4) & 0xFU)); writer.Write(HexUtil.IntToChar(value & 0xFU)); }
// Writes a scalar value as an JavaScript-escaped character (or sequence of characters). private static void WriteEncodedScalarAsNumericEntity(ref Writer writer, uint value) { if (UnicodeHelpers.IsSupplementaryCodePoint((int)value)) { // Convert this back to UTF-16 and write out both characters. char leadingSurrogate, trailingSurrogate; UnicodeHelpers.GetUtf16SurrogatePairFromAstralScalarValue((int)value, out leadingSurrogate, out trailingSurrogate); WriteEncodedSingleCharacter(ref writer, leadingSurrogate); WriteEncodedSingleCharacter(ref writer, trailingSurrogate); } else { // This is only a single character. WriteEncodedSingleCharacter(ref writer, value); } }
// Writes an encoded scalar value (in the BMP) as a JavaScript-escaped character. private unsafe static bool TryWriteEncodedSingleCharacter(int unicodeScalar, char *buffer, int length, out int writtenChars) { Debug.Assert(!UnicodeHelpers.IsSupplementaryCodePoint(unicodeScalar), "The incoming value should've been in the BMP."); if (length < 6) { writtenChars = 0; return(false); } // Encode this as 6 chars "\uFFFF". *buffer = '\\'; buffer++; *buffer = 'u'; buffer++; *buffer = HexUtil.ToHexDigit(unicodeScalar >> 12); buffer++; *buffer = HexUtil.ToHexDigit((int)((unicodeScalar >> 8) & 0xFU)); buffer++; *buffer = HexUtil.ToHexDigit((int)((unicodeScalar >> 4) & 0xFU)); buffer++; *buffer = HexUtil.ToHexDigit((int)(unicodeScalar & 0xFU)); buffer++; writtenChars = 6; return(true); }