Add Latin1 and BigEndianUnicode extensions

This commit is contained in:
Matt Nadareski
2025-10-27 12:22:02 -04:00
parent 7c63f44c75
commit 2b6fc200e2
9 changed files with 396 additions and 12 deletions

View File

@@ -715,6 +715,13 @@ namespace SabreTools.IO.Test.Extensions
string? actual = br.ReadNullTerminatedString(Encoding.ASCII);
Assert.Equal("ABC", actual);
// Encoding.Latin1
bytes = [0x41, 0x42, 0x43, 0x00];
stream = new MemoryStream(bytes);
br = new BinaryReader(stream);
actual = br.ReadNullTerminatedString(Encoding.Latin1);
Assert.Equal("ABC", actual);
// Encoding.UTF8
bytes = [0x41, 0x42, 0x43, 0x00];
stream = new MemoryStream(bytes);
@@ -729,6 +736,13 @@ namespace SabreTools.IO.Test.Extensions
actual = br.ReadNullTerminatedString(Encoding.Unicode);
Assert.Equal("ABC", actual);
// Encoding.BigEndianUnicode
bytes = [0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x00];
stream = new MemoryStream(bytes);
br = new BinaryReader(stream);
actual = br.ReadNullTerminatedString(Encoding.BigEndianUnicode);
Assert.Equal("ABC", actual);
// Encoding.UTF32
bytes = [0x41, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
stream = new MemoryStream(bytes);

View File

@@ -619,6 +619,12 @@ namespace SabreTools.IO.Test.Extensions
string? actual = bytes.ReadNullTerminatedString(ref offset, Encoding.ASCII);
Assert.Equal("ABC", actual);
// Encoding.Latin1
offset = 0;
bytes = [0x41, 0x42, 0x43, 0x00];
actual = bytes.ReadNullTerminatedString(ref offset, Encoding.Latin1);
Assert.Equal("ABC", actual);
// Encoding.UTF8
offset = 0;
bytes = [0x41, 0x42, 0x43, 0x00];
@@ -631,6 +637,12 @@ namespace SabreTools.IO.Test.Extensions
actual = bytes.ReadNullTerminatedString(ref offset, Encoding.Unicode);
Assert.Equal("ABC", actual);
// Encoding.BigEndianUnicode
offset = 0;
bytes = [0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x00];
actual = bytes.ReadNullTerminatedString(ref offset, Encoding.BigEndianUnicode);
Assert.Equal("ABC", actual);
// Encoding.UTF32
offset = 0;
bytes = [0x41, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

View File

@@ -623,6 +623,12 @@ namespace SabreTools.IO.Test.Extensions
string? actual = stream.ReadNullTerminatedString(Encoding.ASCII);
Assert.Equal("ABC", actual);
// Encoding.Latin1
bytes = [0x41, 0x42, 0x43, 0x00];
stream = new MemoryStream(bytes);
actual = stream.ReadNullTerminatedString(Encoding.Latin1);
Assert.Equal("ABC", actual);
// Encoding.UTF8
bytes = [0x41, 0x42, 0x43, 0x00];
stream = new MemoryStream(bytes);
@@ -635,6 +641,12 @@ namespace SabreTools.IO.Test.Extensions
actual = stream.ReadNullTerminatedString(Encoding.Unicode);
Assert.Equal("ABC", actual);
// Encoding.BigEndianUnicode
bytes = [0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x00];
stream = new MemoryStream(bytes);
actual = stream.ReadNullTerminatedString(Encoding.BigEndianUnicode);
Assert.Equal("ABC", actual);
// Encoding.UTF32
bytes = [0x41, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
stream = new MemoryStream(bytes);

View File

@@ -582,13 +582,19 @@ namespace SabreTools.IO.Extensions
public static string? ReadNullTerminatedString(this BinaryReader reader, Encoding encoding)
{
// Short-circuit to explicit implementations
if (encoding.Equals(Encoding.ASCII))
if (encoding.CodePage == Encoding.ASCII.CodePage)
return reader.ReadNullTerminatedAnsiString();
else if (encoding.Equals(Encoding.UTF8))
#if NET5_0_OR_GREATER
else if (encoding.CodePage == Encoding.Latin1.CodePage)
return reader.ReadNullTerminatedLatin1String();
#endif
else if (encoding.CodePage == Encoding.UTF8.CodePage)
return reader.ReadNullTerminatedUTF8String();
else if (encoding.Equals(Encoding.Unicode))
else if (encoding.CodePage == Encoding.Unicode.CodePage)
return reader.ReadNullTerminatedUnicodeString();
else if (encoding.Equals(Encoding.UTF32))
else if (encoding.CodePage == Encoding.BigEndianUnicode.CodePage)
return reader.ReadNullTerminatedBigEndianUnicodeString();
else if (encoding.CodePage == Encoding.UTF32.CodePage)
return reader.ReadNullTerminatedUTF32String();
if (reader.BaseStream.Position >= reader.BaseStream.Length)
@@ -619,6 +625,20 @@ namespace SabreTools.IO.Extensions
return Encoding.ASCII.GetString(buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Read a null-terminated Latin1 string from the underlying stream
/// </summary>
public static string? ReadNullTerminatedLatin1String(this BinaryReader reader)
{
if (reader.BaseStream.Position >= reader.BaseStream.Length)
return null;
byte[] buffer = ReadUntilNull1Byte(reader);
return Encoding.Latin1.GetString(buffer);
}
#endif
/// <summary>
/// Read a null-terminated UTF-8 string from the underlying stream
/// </summary>
@@ -643,6 +663,18 @@ namespace SabreTools.IO.Extensions
return Encoding.Unicode.GetString(buffer);
}
/// <summary>
/// Read a null-terminated UTF-16 (Unicode) string from the underlying stream
/// </summary>
public static string? ReadNullTerminatedBigEndianUnicodeString(this BinaryReader reader)
{
if (reader.BaseStream.Position >= reader.BaseStream.Length)
return null;
byte[] buffer = ReadUntilNull2Byte(reader);
return Encoding.BigEndianUnicode.GetString(buffer);
}
/// <summary>
/// Read a null-terminated UTF-32 string from the underlying stream
/// </summary>
@@ -687,6 +719,22 @@ namespace SabreTools.IO.Extensions
return Encoding.Unicode.GetString(buffer);
}
/// <summary>
/// Read a ushort-prefixed Unicode string from the underlying stream
/// </summary>
public static string? ReadPrefixedBigEndianUnicodeString(this BinaryReader reader)
{
if (reader.BaseStream.Position >= reader.BaseStream.Length)
return null;
ushort size = reader.ReadUInt16();
if (reader.BaseStream.Position + (size * 2) >= reader.BaseStream.Length)
return null;
byte[] buffer = reader.ReadBytes(size * 2);
return Encoding.BigEndianUnicode.GetString(buffer);
}
/// <summary>
/// Read a <typeparamref name="T"/> from the underlying stream
/// </summary>

View File

@@ -415,6 +415,14 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedAnsiString(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.ASCII);
#if NET5_0_OR_GREATER
/// <summary>
/// Write a null-terminated Latin1 string to the underlying stream
/// </summary>
public static bool WriteNullTerminatedLatin1String(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.Latin1);
#endif
/// <summary>
/// Write a null-terminated UTF-8 string to the underlying stream
/// </summary>
@@ -427,6 +435,12 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedUnicodeString(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.Unicode);
/// <summary>
/// Write a null-terminated UTF-16 (Unicode) string to the underlying stream
/// </summary>
public static bool WriteNullTerminatedBigEndianUnicodeString(this BinaryWriter writer, string? value)
=> writer.WriteNullTerminatedString(value, Encoding.BigEndianUnicode);
/// <summary>
/// Write a null-terminated UTF-32 string to the underlying stream
/// </summary>
@@ -452,6 +466,27 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Write a byte-prefixed Latin1 string to the underlying stream
/// </summary>
public static bool WritePrefixedLatin1String(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.Latin1.GetBytes(value);
// Write the length as a byte
writer.Write((byte)value.Length);
// Write the buffer
return WriteFromBuffer(writer, buffer);
}
#endif
/// <summary>
/// Write a ushort-prefixed Unicode string to the underlying stream
/// </summary>
@@ -471,6 +506,25 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(writer, buffer);
}
/// <summary>
/// Write a ushort-prefixed Unicode string to the underlying stream
/// </summary>
public static bool WritePrefixedBigEndianUnicodeString(this BinaryWriter writer, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.BigEndianUnicode.GetBytes(value);
// Write the length as a ushort
writer.Write((ushort)value.Length);
// Write the buffer
return WriteFromBuffer(writer, buffer);
}
/// <summary>
/// Write a <typeparamref name="T"/> to the underlying stream
/// </summary>

View File

@@ -790,13 +790,19 @@ namespace SabreTools.IO.Extensions
public static string? ReadNullTerminatedString(this byte[] content, ref int offset, Encoding encoding)
{
// Short-circuit to explicit implementations
if (encoding.Equals(Encoding.ASCII))
if (encoding.CodePage == Encoding.ASCII.CodePage)
return content.ReadNullTerminatedAnsiString(ref offset);
else if (encoding.Equals(Encoding.UTF8))
#if NET5_0_OR_GREATER
else if (encoding.CodePage == Encoding.Latin1.CodePage)
return content.ReadNullTerminatedAnsiString(ref offset);
#endif
else if (encoding.CodePage == Encoding.UTF8.CodePage)
return content.ReadNullTerminatedUTF8String(ref offset);
else if (encoding.Equals(Encoding.Unicode))
else if (encoding.CodePage == Encoding.Unicode.CodePage)
return content.ReadNullTerminatedUnicodeString(ref offset);
else if (encoding.Equals(Encoding.UTF32))
else if (encoding.CodePage == Encoding.BigEndianUnicode.CodePage)
return content.ReadNullTerminatedBigEndianUnicodeString(ref offset);
else if (encoding.CodePage == Encoding.UTF32.CodePage)
return content.ReadNullTerminatedUTF32String(ref offset);
if (offset >= content.Length)
@@ -827,6 +833,20 @@ namespace SabreTools.IO.Extensions
return Encoding.ASCII.GetString(buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Read a null-terminated Latin1 string from the byte array
/// </summary>
public static string? ReadNullTerminatedLatin1String(this byte[] content, ref int offset)
{
if (offset >= content.Length)
return null;
byte[] buffer = ReadUntilNull1Byte(content, ref offset);
return Encoding.Latin1.GetString(buffer);
}
#endif
/// <summary>
/// Read a null-terminated UTF-8 string from the byte array
/// </summary>
@@ -851,6 +871,18 @@ namespace SabreTools.IO.Extensions
return Encoding.Unicode.GetString(buffer);
}
/// <summary>
/// Read a null-terminated UTF-16 (Unicode) string from the byte array
/// </summary>
public static string? ReadNullTerminatedBigEndianUnicodeString(this byte[] content, ref int offset)
{
if (offset >= content.Length)
return null;
byte[] buffer = ReadUntilNull2Byte(content, ref offset);
return Encoding.BigEndianUnicode.GetString(buffer);
}
/// <summary>
/// Read a null-terminated UTF-32 string from the byte array
/// </summary>
@@ -879,6 +911,24 @@ namespace SabreTools.IO.Extensions
return Encoding.ASCII.GetString(buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Read a byte-prefixed Latin1 string from the byte array
/// </summary>
public static string? ReadPrefixedLatin1String(this byte[] content, ref int offset)
{
if (offset >= content.Length)
return null;
byte size = content.ReadByteValue(ref offset);
if (offset + size >= content.Length)
return null;
byte[] buffer = content.ReadBytes(ref offset, size);
return Encoding.Latin1.GetString(buffer);
}
#endif
/// <summary>
/// Read a ushort-prefixed Unicode string from the byte array
/// </summary>
@@ -895,6 +945,22 @@ namespace SabreTools.IO.Extensions
return Encoding.Unicode.GetString(buffer);
}
/// <summary>
/// Read a ushort-prefixed Unicode string from the byte array
/// </summary>
public static string? ReadPrefixedBigEndianUnicodeString(this byte[] content, ref int offset)
{
if (offset >= content.Length)
return null;
ushort size = content.ReadUInt16(ref offset);
if (offset + (size * 2) >= content.Length)
return null;
byte[] buffer = content.ReadBytes(ref offset, size * 2);
return Encoding.BigEndianUnicode.GetString(buffer);
}
/// <summary>
/// Read a <typeparamref name="T"/> and increment the pointer to an array
/// </summary>

View File

@@ -583,6 +583,14 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedAnsiString(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.ASCII);
#if NET5_0_OR_GREATER
/// <summary>
/// Write a null-terminated Latin1 string to the byte array
/// </summary>
public static bool WriteNullTerminatedLatin1String(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.Latin1);
#endif
/// <summary>
/// Write a null-terminated UTF-8 string to the byte array
/// </summary>
@@ -595,6 +603,12 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedUnicodeString(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.Unicode);
/// <summary>
/// Write a null-terminated UTF-16 (Unicode) string to the byte array
/// </summary>
public static bool WriteNullTerminatedBigEndianUnicodeString(this byte[] content, ref int offset, string? value)
=> content.WriteNullTerminatedString(ref offset, value, Encoding.BigEndianUnicode);
/// <summary>
/// Write a null-terminated UTF-32 string to the byte array
/// </summary>
@@ -621,6 +635,28 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Write a byte-prefixed Latin1 string to the byte array
/// </summary>
public static bool WritePrefixedLatin1String(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.Latin1.GetBytes(value);
// Write the length as a byte
if (!content.Write(ref offset, (byte)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(content, ref offset, buffer);
}
#endif
/// <summary>
/// Write a ushort-prefixed Unicode string to the byte array
/// </summary>
@@ -641,6 +677,26 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a ushort-prefixed Unicode string to the byte array
/// </summary>
public static bool WritePrefixedBigEndianUnicodeString(this byte[] content, ref int offset, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.BigEndianUnicode.GetBytes(value);
// Write the length as a ushort
if (!content.Write(ref offset, (ushort)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(content, ref offset, buffer);
}
/// <summary>
/// Write a <typeparamref name="T"/> to the byte array
/// </summary>

View File

@@ -785,13 +785,19 @@ namespace SabreTools.IO.Extensions
public static string? ReadNullTerminatedString(this Stream stream, Encoding encoding)
{
// Short-circuit to explicit implementations
if (encoding.Equals(Encoding.ASCII))
if (encoding.CodePage == Encoding.ASCII.CodePage)
return stream.ReadNullTerminatedAnsiString();
else if (encoding.Equals(Encoding.UTF8))
#if NET5_0_OR_GREATER
else if (encoding.CodePage == Encoding.Latin1.CodePage)
return stream.ReadNullTerminatedLatin1String();
#endif
else if (encoding.CodePage == Encoding.UTF8.CodePage)
return stream.ReadNullTerminatedUTF8String();
else if (encoding.Equals(Encoding.Unicode))
else if (encoding.CodePage == Encoding.Unicode.CodePage)
return stream.ReadNullTerminatedUnicodeString();
else if (encoding.Equals(Encoding.UTF32))
else if (encoding.CodePage == Encoding.BigEndianUnicode.CodePage)
return stream.ReadNullTerminatedBigEndianUnicodeString();
else if (encoding.CodePage == Encoding.UTF32.CodePage)
return stream.ReadNullTerminatedUTF32String();
if (stream.Position >= stream.Length)
@@ -822,6 +828,20 @@ namespace SabreTools.IO.Extensions
return Encoding.ASCII.GetString(buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Read a null-terminated Latin1 string from the stream
/// </summary>
public static string? ReadNullTerminatedLatin1String(this Stream stream)
{
if (stream.Position >= stream.Length)
return null;
byte[] buffer = ReadUntilNull1Byte(stream);
return Encoding.Latin1.GetString(buffer);
}
#endif
/// <summary>
/// Read a null-terminated UTF-8 string from the stream
/// </summary>
@@ -846,6 +866,18 @@ namespace SabreTools.IO.Extensions
return Encoding.Unicode.GetString(buffer);
}
/// <summary>
/// Read a null-terminated UTF-16 (Unicode) string from the stream
/// </summary>
public static string? ReadNullTerminatedBigEndianUnicodeString(this Stream stream)
{
if (stream.Position >= stream.Length)
return null;
byte[] buffer = ReadUntilNull2Byte(stream);
return Encoding.BigEndianUnicode.GetString(buffer);
}
/// <summary>
/// Read a null-terminated UTF-32 string from the stream
/// </summary>
@@ -874,6 +906,24 @@ namespace SabreTools.IO.Extensions
return Encoding.ASCII.GetString(buffer);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Read a byte-prefixed Latin1 string from the stream
/// </summary>
public static string? ReadPrefixedLatin1String(this Stream stream)
{
if (stream.Position >= stream.Length)
return null;
byte size = stream.ReadByteValue();
if (stream.Position + size >= stream.Length)
return null;
byte[] buffer = stream.ReadBytes(size);
return Encoding.Latin1.GetString(buffer);
}
#endif
/// <summary>
/// Read a ushort-prefixed Unicode string from the stream
/// </summary>
@@ -890,6 +940,22 @@ namespace SabreTools.IO.Extensions
return Encoding.Unicode.GetString(buffer);
}
/// <summary>
/// Read a ushort-prefixed Unicode string from the stream
/// </summary>
public static string? ReadPrefixedBigEndianUnicodeString(this Stream stream)
{
if (stream.Position >= stream.Length)
return null;
ushort size = stream.ReadUInt16();
if (stream.Position + (size * 2) >= stream.Length)
return null;
byte[] buffer = stream.ReadBytes(size * 2);
return Encoding.BigEndianUnicode.GetString(buffer);
}
/// <summary>
/// Read a <typeparamref name="T"/> from the stream
/// </summary>

View File

@@ -584,6 +584,14 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedAnsiString(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.ASCII);
#if NET5_0_OR_GREATER
/// <summary>
/// Write a null-terminated Latin1 string to the stream
/// </summary>
public static bool WriteNullTerminatedLatin1String(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.Latin1);
#endif
/// <summary>
/// Write a null-terminated UTF-8 string to the stream
/// </summary>
@@ -596,6 +604,12 @@ namespace SabreTools.IO.Extensions
public static bool WriteNullTerminatedUnicodeString(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.Unicode);
/// <summary>
/// Write a null-terminated UTF-16 (Unicode) string to the stream
/// </summary>
public static bool WriteNullTerminatedBigEndianUnicodeString(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.BigEndianUnicode);
/// <summary>
/// Write a null-terminated UTF-32 string to the stream
/// </summary>
@@ -622,6 +636,28 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(stream, buffer);
}
#if NET5_0_OR_GREATER
//// <summary>
/// Write a byte-prefixed Latin1 string to the stream
/// </summary>
public static bool WritePrefixedLatin1String(this Stream stream, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.Latin1.GetBytes(value);
// Write the length as a byte
if (!stream.Write((byte)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(stream, buffer);
}
#endif
/// <summary>
/// Write a ushort-prefixed Unicode string to the stream
/// </summary>
@@ -642,6 +678,26 @@ namespace SabreTools.IO.Extensions
return WriteFromBuffer(stream, buffer);
}
/// <summary>
/// Write a ushort-prefixed Unicode string to the stream
/// </summary>
public static bool WritePrefixedBigEndianUnicodeString(this Stream stream, string? value)
{
// If the value is null
if (value == null)
return false;
// Get the buffer
byte[] buffer = Encoding.BigEndianUnicode.GetBytes(value);
// Write the length as a ushort
if (!stream.Write((ushort)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(stream, buffer);
}
/// <summary>
/// Write a <typeparamref name="T"/> to the stream
/// </summary>