task 2.5-2.6: string Read/Write with encoding tests

Add 8 tests for string IO: ASCII/UTF8/Unicode round-trip, null-terminator
stop, max-length truncation, auto-append of null terminator, empty string.
Fix ReadString null-terminator detection for multi-byte encodings (UTF-16):
decode string first, then find \0 in characters not bytes.

All passing (total: 38).
This commit is contained in:
kbe
2026-07-21 17:06:34 +02:00
parent 6b34e0fab8
commit b2a5090533
2 changed files with 187 additions and 3 deletions
+4 -3
View File
@@ -141,12 +141,13 @@ public abstract class MemoryBase : IDisposable
public virtual string ReadString(IntPtr address, Encoding encoding, int maxLength = 512, bool relative = false)
{
byte[] buffer = ReadBytes(address, maxLength, relative);
int nullIndex = Array.IndexOf<byte>(buffer, 0);
string decoded = encoding.GetString(buffer);
int nullIndex = decoded.IndexOf('\0');
if (nullIndex >= 0)
{
return encoding.GetString(buffer, 0, nullIndex);
return decoded[..nullIndex];
}
return encoding.GetString(buffer);
return decoded;
}
/// <summary>Writes a null-terminated string to the target address.</summary>