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:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user