Skip to content

Commit

Permalink
minor span extension refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cengelha committed Sep 10, 2023
1 parent 12fc1d5 commit a8f0009
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
26 changes: 10 additions & 16 deletions libNOM.io/Extensions/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ internal static Guid GetGuid(this ReadOnlySpan<byte> self, int start = 0)
return null;
}

/// <summary>
/// Gets all bytes until the first \0 as string.
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
internal static string GetSaveRenamingString(this ReadOnlySpan<byte> self)
{
return GetString(self.Slice(0, self.IndexOf((byte)(0))));
}

/// <summary>
/// Decodes all the bytes in UTF-8 format into a string.
/// </summary>
Expand Down Expand Up @@ -150,22 +160,6 @@ internal static int ReadString(this ReadOnlySpan<byte> self, out ReadOnlySpan<ch
}

#endregion

#region typeof(uint)

internal static string GetSaveRenamingString(this ReadOnlySpan<uint> self)
{
var bytes = self.AsBytes();
var index = bytes.IndexOf((byte)(0));

#if NETSTANDARD2_0
return Encoding.UTF8.GetString(bytes.Slice(0, index).ToArray());
#else
return Encoding.UTF8.GetString(bytes.Slice(0, index));
#endif
}

#endregion
}

internal static class SpanExtensions
Expand Down
4 changes: 2 additions & 2 deletions libNOM.io/PlatformMicrosoft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ protected override void UpdateContainerWithMetaInformation(Container container,
{
container.Extra = container.Extra with
{
SaveName = decompressed.Slice(5, 32).GetSaveRenamingString(),
SaveSummary = decompressed.Slice(37, 32).GetSaveRenamingString(),
SaveName = disk.Slice(20, 128).GetSaveRenamingString(),
SaveSummary = disk.Slice(148, 128).GetSaveRenamingString(),
DifficultyPreset = disk[276],
};
}
Expand Down
4 changes: 2 additions & 2 deletions libNOM.io/PlatformSteam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ protected override void UpdateContainerWithMetaInformation(Container container,
{
container.Extra = container.Extra with
{
SaveName = decompressed.Slice(22, 32).GetSaveRenamingString(),
SaveSummary = decompressed.Slice(54, 32).GetSaveRenamingString(),
SaveName = disk.Slice(88, 128).GetSaveRenamingString(),
SaveSummary = disk .Slice(216, 128).GetSaveRenamingString(),
DifficultyPreset = disk[344],
};
}
Expand Down
4 changes: 2 additions & 2 deletions libNOM.io/PlatformSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ protected override void UpdateContainerWithMetaInformation(Container container,
{
container.Extra = container.Extra with
{
SaveName = decompressed.Slice(10, 32).GetSaveRenamingString(),
SaveSummary = decompressed.Slice(42, 32).GetSaveRenamingString(),
SaveName = disk.Slice(40, 128).GetSaveRenamingString(),
SaveSummary = disk.Slice(168, 128).GetSaveRenamingString(),
DifficultyPreset = disk[296],
};
}
Expand Down

0 comments on commit a8f0009

Please sign in to comment.