Skip to content

Commit

Permalink
Remove use of non ASCII characters in literals.
Browse files Browse the repository at this point in the history
We would prefer that we don't use non ASCII characters in either string
or character literals.  In theory, every editor and tool would ensure
that these characters are not translated to some random encoding or
otherwise become corrupted.  In practice, we're just going to err on the
side of caution.
  • Loading branch information
ellismg committed Nov 15, 2014
1 parent cbae62a commit 2270158
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ public unsafe void DefaultDecodingFallbackMatchesBcl()
fixed (byte* ptr = (buffer = new byte[] { 0xC0 }))
{
string s = new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, null, decoder, out bytesRead);
Assert.Equal("", new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, null, decoder, out bytesRead));
Assert.Equal("\uFFFD", new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, null, decoder, out bytesRead));
Assert.Equal(s, Encoding.UTF8.GetString(buffer));
Assert.Equal(buffer.Length, bytesRead);

s = new MemoryBlock(ptr, buffer.Length).PeekUtf8NullTerminated(0, Encoding.UTF8.GetBytes("Hello"), decoder, out bytesRead);
Assert.Equal("Hello", s);
Assert.Equal("Hello\uFFFD", s);
Assert.Equal(s, "Hello" + Encoding.UTF8.GetString(buffer));
Assert.Equal(buffer.Length, bytesRead);

Assert.Equal("", new MemoryBlock(ptr, buffer.Length).PeekUtf8(0, buffer.Length));
Assert.Equal("\uFFFD", new MemoryBlock(ptr, buffer.Length).PeekUtf8(0, buffer.Length));
}

// overlong encoding
Expand All @@ -102,7 +102,7 @@ public unsafe void DecodingSuccessMatchesBcl()
var utf8 = Encoding.GetEncoding("utf-8", EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
byte[] buffer;
int bytesRead;
string str = "你好. Comment ça va?";
string str = "\u4F60\u597D. Comment \u00E7a va?";

var decoder = MetadataStringDecoder.DefaultUTF8;

Expand Down Expand Up @@ -142,9 +142,9 @@ public unsafe void LightUpTrickFromDifferentAssemblyWorks()
Assert.Throws<ArgumentOutOfRangeException>(() => decoder.GetString((byte*)1, -1));

byte[] bytes;
fixed (byte* ptr = (bytes = Encoding.Unicode.GetBytes("Ça marche très bien.")))
fixed (byte* ptr = (bytes = Encoding.Unicode.GetBytes("\u00C7a marche tr\u00E8s bien.")))
{
Assert.Equal("Ça marche très bien.", decoder.GetString(ptr, bytes.Length));
Assert.Equal("\u00C7a marche tr\u00E8s bien.", decoder.GetString(ptr, bytes.Length));
}
}

Expand Down

0 comments on commit 2270158

Please sign in to comment.