// Writes a scalar value as a percent-encoded sequence of UTF8 bytes, per RFC 3987. protected override void WriteEncodedScalar(ref Writer writer, uint value) { uint asUtf8 = (uint)UnicodeHelpers.GetUtf8RepresentationForScalarValue(value); do { char highNibble, lowNibble; HexUtil.WriteHexEncodedByte((byte)asUtf8, out highNibble, out lowNibble); writer.Write('%'); writer.Write(highNibble); writer.Write(lowNibble); } while ((asUtf8 >>= 8) != 0); }
public void GetUtf8RepresentationForScalarValue() { for (int i = 0; i <= 0x10FFFF; i++) { if (i <= 0xFFFF && Char.IsSurrogate((char)i)) { continue; // no surrogates } // Arrange byte[] expectedUtf8Bytes = _utf8EncodingThrowOnInvalidBytes.GetBytes(Char.ConvertFromUtf32(i)); // Act List <byte> actualUtf8Bytes = new List <byte>(4); uint asUtf8 = (uint)UnicodeHelpers.GetUtf8RepresentationForScalarValue((uint)i); do { actualUtf8Bytes.Add((byte)asUtf8); } while ((asUtf8 >>= 8) != 0); // Assert Assert.Equal(expectedUtf8Bytes, actualUtf8Bytes); } }