Skip to content

Commit

Permalink
Rename Encoding enum (conflicts with System.Text.Encoding)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Dec 7, 2023
1 parent 50320cc commit 296c15d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
9 changes: 1 addition & 8 deletions src/MessageStudio/Formats/BinaryText/Msbt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
using MessageStudio.Formats.BinaryText.Writers;
using MessageStudio.IO;
using System.Runtime.CompilerServices;
using System.Text;

namespace MessageStudio.Formats.BinaryText;

public enum Encoding : byte
{
UTF8 = 0,
Unicode = 1,
}

public class Msbt : Dictionary<string, MsbtEntry>
{
internal const ulong MSBT_MAGIC = 0x6E4264745367734D;
Expand Down Expand Up @@ -63,7 +56,7 @@ public static Msbt FromYaml(in ReadOnlySpan<char> src)
throw new NotImplementedException();
}

public void ToBinary(in Stream stream, Encoding encoding = Encoding.Unicode, Endian endianness = Endian.Little)
public void ToBinary(in Stream stream, TextEncoding encoding = TextEncoding.Unicode, Endian endianness = Endian.Little)
{
InternalWriter writer = new(stream, endianness);
ushort sectionCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public readonly ref struct AttributeSectionReader
{
private readonly Span<byte> _buffer;
private readonly Span<int> _offsets;
private readonly Encoding _encoding;
private readonly TextEncoding _encoding;
private readonly int _attributeSize;
private readonly int _count;

Expand All @@ -31,7 +31,7 @@ public readonly AttributeMarshal this[int index] {
}
}

public AttributeSectionReader(ref SpanReader reader, ref MsbtSectionHeader header, Encoding encoding)
public AttributeSectionReader(ref SpanReader reader, ref MsbtSectionHeader header, TextEncoding encoding)
{
_encoding = encoding;

Expand All @@ -50,7 +50,7 @@ public AttributeSectionReader(ref SpanReader reader, ref MsbtSectionHeader heade
_offsets = reader.ReadSpan<int>(_count);

if (reader.IsNotSystemByteOrder()) {
if (encoding == Encoding.Unicode) {
if (encoding == TextEncoding.Unicode) {
int eos = sectionOffset + header.SectionSize;
while (reader.Position < eos) {
reader.Reverse<ushort>();
Expand All @@ -61,10 +61,10 @@ public AttributeSectionReader(ref SpanReader reader, ref MsbtSectionHeader heade
_buffer = reader.Read(header.SectionSize, sectionOffset);
}

public readonly ref struct AttributeMarshal(Span<byte> buffer, Encoding encoding)
public readonly ref struct AttributeMarshal(Span<byte> buffer, TextEncoding encoding)
{
public readonly Span<byte> Buffer = buffer;
public readonly Encoding Encoding = encoding;
public readonly TextEncoding Encoding = encoding;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<char> GetUnicode()
Expand All @@ -78,7 +78,7 @@ public ReadOnlySpan<char> GetUnicode()
return null;
}

if (Encoding == Encoding.UTF8) {
if (Encoding == TextEncoding.UTF8) {
fixed (byte* ptr = Buffer) {
return Utf8StringMarshaller.ConvertToManaged(ptr);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public readonly ref struct TextSectionReader
{
private readonly Span<byte> _buffer;
private readonly Span<int> _offsets;
private readonly Encoding _encoding;
private readonly TextEncoding _encoding;
private readonly int _count;

public readonly TextMarshal this[int index] {
Expand All @@ -27,7 +27,7 @@ public readonly TextMarshal this[int index] {
}
}

public TextSectionReader(ref SpanReader reader, ref MsbtSectionHeader header, Encoding encoding)
public TextSectionReader(ref SpanReader reader, ref MsbtSectionHeader header, TextEncoding encoding)
{
_encoding = encoding;

Expand All @@ -36,7 +36,7 @@ public TextSectionReader(ref SpanReader reader, ref MsbtSectionHeader header, En
_offsets = reader.ReadSpan<int>(_count);

if (reader.IsNotSystemByteOrder()) {
if (encoding == Encoding.Unicode) {
if (encoding == TextEncoding.Unicode) {
int eos = sectionOffset + header.SectionSize;
while (reader.Position < eos) {
reader.Reverse<ushort>();
Expand All @@ -47,10 +47,10 @@ public TextSectionReader(ref SpanReader reader, ref MsbtSectionHeader header, En
_buffer = reader.Read(header.SectionSize, sectionOffset);
}

public readonly ref struct TextMarshal(Span<byte> buffer, Encoding encoding)
public readonly ref struct TextMarshal(Span<byte> buffer, TextEncoding encoding)
{
public readonly Span<byte> Buffer = buffer;
public readonly Encoding Encoding = encoding;
public readonly TextEncoding Encoding = encoding;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<char> GetUnicode()
Expand All @@ -61,7 +61,7 @@ public ReadOnlySpan<char> GetUnicode()
public readonly string? GetManaged()
{
StringBuilder sb = new();
if (Encoding == Encoding.UTF8) {
if (Encoding == TextEncoding.UTF8) {
WriteUtf8(sb);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/MessageStudio/Formats/BinaryText/Structures/MsbtHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public readonly struct MsbtHeader
public readonly Endian ByteOrderMark;

[FieldOffset(0x0C)]
public readonly Encoding Encoding;
public readonly TextEncoding Encoding;

[FieldOffset(0x0D)]
public readonly byte Version;
Expand All @@ -26,7 +26,7 @@ public readonly struct MsbtHeader
public readonly uint FileSize;

public MsbtHeader() { }
public MsbtHeader(ulong magic, Endian byteOrderMark, Encoding encoding, byte version, ushort sectionCount, uint fileSize)
public MsbtHeader(ulong magic, Endian byteOrderMark, TextEncoding encoding, byte version, ushort sectionCount, uint fileSize)
{
Magic = magic;
ByteOrderMark = byteOrderMark;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace MessageStudio.Formats.BinaryText.Writers;

internal static class AttributeSectionWriter
{
public static void Write(in InternalWriter writer, Encoding encoding, string?[] attributes)
public static void Write(in InternalWriter writer, TextEncoding encoding, string?[] attributes)
{
writer.Write(attributes.Length);
writer.Write(sizeof(uint));

int firstOffset = attributes.Length * sizeof(uint) + sizeof(uint) + sizeof(uint);

if (encoding is Encoding.UTF8) {
if (encoding is TextEncoding.UTF8) {
WriteUtf8(writer, attributes, firstOffset);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace MessageStudio.Formats.BinaryText.Writers;

internal static class TextSectionWriter
{
public static void Write(in InternalWriter writer, Encoding encoding, string?[] entries)
public static void Write(in InternalWriter writer, TextEncoding encoding, string?[] entries)
{
long sectionOffset = writer.Position;

Expand All @@ -15,7 +15,7 @@ public static void Write(in InternalWriter writer, Encoding encoding, string?[]
int firstOffset = entries.Length * sizeof(uint) + sizeof(uint);
long sectionEndPosition;

if (encoding == Encoding.UTF8) {
if (encoding == TextEncoding.UTF8) {
sectionEndPosition = WriteUtf8(writer, entries, firstOffset, sectionOffset);
}
else {
Expand Down Expand Up @@ -66,10 +66,10 @@ private static void WriteUtf16Entry(InternalWriter writer, ReadOnlySpan<char> te
if (value == '<' && (endTagIndex = text[i..].IndexOf('>')) > -1) {
ReadOnlySpan<char> tagStr = text[i..((i += endTagIndex) + 1)];
if (tagStr.Length > 1 && tagStr[1] == '[') {
writer.WriteEndTag(tagStr, Encoding.Unicode);
writer.WriteEndTag(tagStr, TextEncoding.Unicode);
}
else {
writer.WriteTag(tagStr, Encoding.Unicode);
writer.WriteTag(tagStr, TextEncoding.Unicode);
}
}
else {
Expand Down
13 changes: 13 additions & 0 deletions src/MessageStudio/IO/Primitives.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MessageStudio.IO;

public enum TextEncoding : byte
{
UTF8 = 0,
Unicode = 1,
}

public enum Endian : ushort
{
Big = 0xFEFF,
Little = 0xFFFE,
}
6 changes: 0 additions & 6 deletions src/MessageStudio/IO/SpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

namespace MessageStudio.IO;

public enum Endian : ushort
{
Big = 0xFEFF,
Little = 0xFFFE,
}

public ref struct SpanReader(Span<byte> buffer, Endian endianness = Endian.Big)
{
private readonly Span<byte> _buffer = buffer;
Expand Down

0 comments on commit 296c15d

Please sign in to comment.