private static string DecodeUtf8Prefixed(byte *bytes, int byteCount, byte[] prefix, MetadataStringDecoder utf8Decoder) { Debug.Assert(utf8Decoder != null); int prefixedByteCount = byteCount + prefix.Length; if (prefixedByteCount == 0) { return(String.Empty); } byte[] buffer = AcquireBuffer(prefixedByteCount); prefix.CopyTo(buffer, 0); Marshal.Copy((IntPtr)bytes, buffer, prefix.Length, byteCount); string result; fixed(byte *prefixedBytes = buffer) { result = utf8Decoder.GetString(prefixedBytes, prefixedByteCount); } ReleaseBuffer(buffer); return(result); }
private static string DecodeUtf8Prefixed(byte *bytes, int byteCount, byte[] prefix, MetadataStringDecoder utf8Decoder) { Debug.Assert(utf8Decoder != null); int prefixedByteCount = byteCount + prefix.Length; if (prefixedByteCount == 0) { return(string.Empty); } byte[] buffer = ArrayPool <byte> .Shared.Rent(prefixedByteCount); prefix.CopyTo(buffer, 0); Marshal.Copy((IntPtr)bytes, buffer, prefix.Length, byteCount); string result; fixed(byte *prefixedBytes = &buffer[0]) { result = utf8Decoder.GetString(prefixedBytes, prefixedByteCount); } ArrayPool <byte> .Shared.Return(buffer); return(result); }
public unsafe void LightUpTrickFromDifferentAssemblyWorks() { // This is a trick to use our portable light up outside the reader assembly (that // I will use in Roslyn). Check that it works with encoding other than UTF8 and that it // validates arguments like the the real thing. var decoder = new MetadataStringDecoder(Encoding.Unicode); Assert.Throws <ArgumentNullException>(() => decoder.GetString(null, 0)); Assert.Throws <ArgumentOutOfRangeException>(() => decoder.GetString((byte *)1, -1)); byte[] bytes; fixed(byte *ptr = (bytes = Encoding.Unicode.GetBytes("\u00C7a marche tr\u00E8s bien."))) { Assert.Equal("\u00C7a marche tr\u00E8s bien.", decoder.GetString(ptr, bytes.Length)); } }
public static string DecodeUtf8(byte *bytes, int byteCount, byte[] prefix, MetadataStringDecoder utf8Decoder) { Debug.Assert(utf8Decoder != null); if (prefix != null) { return(DecodeUtf8Prefixed(bytes, byteCount, prefix, utf8Decoder)); } if (byteCount == 0) { return(String.Empty); } return(utf8Decoder.GetString(bytes, byteCount)); }
public unsafe void LightUpTrickFromDifferentAssemblyWorks() { // This is a trick to use our portable light up outside the reader assembly (that // I will use in Roslyn). Check that it works with encoding other than UTF8 and that it // validates arguments like the the real thing. var decoder = new MetadataStringDecoder(Encoding.Unicode); Assert.Throws<ArgumentNullException>(() => decoder.GetString(null, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => decoder.GetString((byte*)1, -1)); byte[] bytes; fixed (byte* ptr = (bytes = Encoding.Unicode.GetBytes("\u00C7a marche tr\u00E8s bien."))) { Assert.Equal("\u00C7a marche tr\u00E8s bien.", decoder.GetString(ptr, bytes.Length)); } }