Rewrite string reader/writer

This commit is contained in:
Yoshi Askharoun
2024-11-16 15:22:56 -06:00
parent f94e29a368
commit e7f1db85d9
2 changed files with 45 additions and 34 deletions
+33 -26
View File
@@ -251,46 +251,53 @@ namespace Microsoft.Iris.Markup
public unsafe string ReadString()
{
uint num1 = ReadUInt16();
if (num1 == ushort.MaxValue)
uint stringLength = ReadUInt16();
if (stringLength == ushort.MaxValue)
return null;
bool flag;
uint num2;
if (((int)num1 & 32768) != 0)
var isUtf16Encoded = ((int)stringLength & (1 << 15)) == 0;
uint byteCount;
if (isUtf16Encoded)
{
flag = true;
num1 &= (uint)short.MaxValue;
num2 = num1;
byteCount = stringLength * 2U;
}
else
{
flag = false;
num2 = num1 * 2U;
stringLength &= (uint)short.MaxValue;
byteCount = stringLength;
}
if (_offset + num2 > _size)
if (_offset + byteCount > _size)
ThrowReadError();
char[] chArray = num1 >= s_scratchCharArray.Length ? new char[num1] : ByteCodeReader.s_scratchCharArray;
byte* numPtr1 = _buffer + (int)_offset;
if (flag)
char[] chArray = stringLength >= s_scratchCharArray.Length
? new char[stringLength] : s_scratchCharArray;
byte* chPtr = _buffer + (int)_offset;
if (isUtf16Encoded)
{
for (int index = 0; index < num1; ++index)
chArray[index] = (char)*numPtr1++;
for (int index = 0; index < stringLength; ++index)
{
byte* lowerBytePtr = chPtr;
byte* upperBytePtr = chPtr + 1;
byte lowerByte = *lowerBytePtr;
byte upperByte = *upperBytePtr;
chArray[index] = (char)(lowerByte | (uint)upperByte << 8);
chPtr = upperBytePtr + 1;
}
}
else
{
for (int index = 0; index < num1; ++index)
for (int index = 0; index < stringLength; ++index)
{
byte* numPtr2 = numPtr1;
byte* numPtr3 = numPtr2 + 1;
byte num3 = *numPtr2;
byte* numPtr4 = numPtr3;
numPtr1 = numPtr4 + 1;
byte num4 = *numPtr4;
chArray[index] = (char)(num3 | (uint)num4 << 8);
chArray[index] = (char)*chPtr++;
}
}
_offset += num2;
return new string(chArray, 0, (int)num1);
_offset += byteCount;
return new string(chArray, 0, (int)stringLength);
}
public unsafe IntPtr ToIntPtr(out uint size)
+9 -5
View File
@@ -126,22 +126,26 @@ namespace Microsoft.Iris.Markup
{
if (value.Length >= short.MaxValue)
throw new ArgumentException("String too long");
bool flag = false;
bool useUtf16 = false;
foreach (char ch in value)
{
if (ch > 'ÿ')
{
flag = true;
useUtf16 = true;
break;
}
}
uint length = (uint)value.Length;
if (!flag)
length |= 32768U;
if (!useUtf16)
length |= 1 << 15;
WriteUInt16((ushort)length);
foreach (char ch in value)
{
if (flag)
if (useUtf16)
WriteChar(ch);
else
WriteByte((byte)ch);