提交 6d11affd 编写于 作者: P Pharring

ObjectWriter was allocating temporary char arrays when serializing strings.

We just needed to override a few defaults on the MultiByteEncoding type.
Also, while testing this, I noticed that one of the tests that was supposed to be testing a long string was incorrect. (changeset 1263598)
上级 d9c684bf
......@@ -220,7 +220,7 @@ public void TestRoundTripStringCharacters()
// round trip single string with all possible characters
var sb = new StringBuilder();
for (int i = ushort.MinValue; i <= ushort.MinValue; i++)
for (int i = ushort.MinValue; i <= ushort.MaxValue; i++)
{
sb.Append((char)i);
}
......
......@@ -34,6 +34,27 @@ public override int GetByteCount(char[] chars, int index, int count)
return byteCount;
}
public override int GetByteCount(string s)
{
int byteCount = 0;
foreach (char c in s)
{
if (c >= DoubleByteEncoded)
{
// marker + 2 byte value
byteCount += 3;
}
else
{
// single byte value
byteCount += 1;
}
}
return byteCount;
}
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
int bi = byteIndex;
......@@ -58,6 +79,52 @@ public override int GetBytes(char[] chars, int charIndex, int charCount, byte[]
return bi - byteIndex;
}
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
int bi = byteIndex;
for (int i = 0; i < charCount; i++)
{
ushort c = (ushort)s[charIndex + i];
if (c >= DoubleByteEncoded)
{
bytes[bi] = DoubleByteEncoded;
bytes[bi + 1] = (byte)(c & 0xFF);
bytes[bi + 2] = (byte)((c >> 8) & 0xFF);
bi += 3;
}
else
{
bytes[bi] = (byte)c;
bi++;
}
}
return bi - byteIndex;
}
public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
{
byte* dest = bytes;
for (; charCount > 0; charCount--)
{
ushort c = *chars++;
if (c >= DoubleByteEncoded)
{
*dest++ = DoubleByteEncoded;
*dest++ = (byte)(c & 0xFF);
*dest++ = (byte)((c >> 8) & 0xFF);
}
else
{
*dest++ = (byte)c;
}
}
return (int)(dest - bytes);
}
public override int GetCharCount(byte[] bytes, int index, int count)
{
throw new NotImplementedException();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册