Add some FspPathPrintf functionality and change a constant name

This commit is contained in:
Alex Barney 2020-04-02 21:54:26 -07:00
parent a5699182b0
commit 405bbeff9e
4 changed files with 42 additions and 12 deletions

View file

@ -12,8 +12,9 @@ namespace LibHac.Common
private int _length;
public bool Overflowed { get; private set; }
public int Length => _length;
public int Capacity => _buffer.Length - NullTerminatorLength;
public readonly int Length => _length;
public readonly int Capacity => _buffer.Length - NullTerminatorLength;
public readonly Span<byte> Buffer => _buffer;
public U8StringBuilder(Span<byte> buffer)
{
@ -62,12 +63,12 @@ namespace LibHac.Common
return this;
}
private bool HasCapacity(int requiredCapacity)
private readonly bool HasCapacity(int requiredCapacity)
{
return requiredCapacity <= Capacity;
}
private bool HasAdditionalCapacity(int requiredAdditionalCapacity)
private readonly bool HasAdditionalCapacity(int requiredAdditionalCapacity)
{
return HasCapacity(_length + requiredAdditionalCapacity);
}
@ -77,11 +78,11 @@ namespace LibHac.Common
_buffer[_length] = 0;
}
private void ThrowIfBufferLengthIsZero()
private readonly void ThrowIfBufferLengthIsZero()
{
if (_buffer.Length == 0) throw new ArgumentException("Buffer length must be greater than 0.");
}
public override string ToString() => StringUtils.Utf8ZToString(_buffer);
public override readonly string ToString() => StringUtils.Utf8ZToString(_buffer);
}
}

View file

@ -6,7 +6,7 @@ namespace LibHac.Fs
{
public class FileStorage2 : StorageBase
{
protected const long InvalidSize = -1;
protected const long SizeNotInitialized = -1;
private IFile BaseFile { get; set; }
protected long FileSize { get; set; }
@ -14,7 +14,7 @@ namespace LibHac.Fs
public FileStorage2(IFile baseFile)
{
BaseFile = baseFile;
FileSize = InvalidSize;
FileSize = SizeNotInitialized;
}
protected FileStorage2() { }
@ -29,7 +29,7 @@ namespace LibHac.Fs
private Result UpdateSize()
{
if (FileSize != InvalidSize)
if (FileSize != SizeNotInitialized)
return Result.Success;
Result rc = BaseFile.GetSize(out long fileSize);
@ -87,7 +87,7 @@ namespace LibHac.Fs
protected override Result SetSizeImpl(long size)
{
FileSize = InvalidSize;
FileSize = SizeNotInitialized;
return BaseFile.SetSize(size);
}

View file

@ -11,7 +11,7 @@ namespace LibHac.Fs
private FileStorageBasedFileSystem()
{
FileSize = InvalidSize;
FileSize = SizeNotInitialized;
}
public static Result CreateNew(out FileStorageBasedFileSystem created, IFileSystem baseFileSystem, U8Span path,

View file

@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using LibHac.Common;
using static LibHac.Fs.PathTool;
@ -77,5 +78,33 @@ namespace LibHac.Fs
return ResultFs.TooLongPath.Log();
}
public static void Replace(Span<byte> buffer, byte oldChar, byte newChar)
{
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] == oldChar)
{
buffer[i] = newChar;
}
}
}
/// <summary>
/// Performs the extra functions that nn::fs::FspPathPrintf does on the string buffer.
/// </summary>
/// <param name="builder">The string builder to process.</param>
/// <returns>The <see cref="Result"/> of the operation.</returns>
public static Result ToSfPath(in this U8StringBuilder builder)
{
if (builder.Overflowed)
return ResultFs.TooLongPath.Log();
Replace(builder.Buffer.Slice(builder.Capacity),
StringTraits.AltDirectorySeparator,
StringTraits.DirectorySeparator);
return Result.Success;
}
}
}