1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-07-05 07:45:59 +02:00

Fixed some hash checking errors

This commit is contained in:
Brian Fiete 2019-10-05 10:24:58 -07:00
parent f258b4a25b
commit b934378758
3 changed files with 89 additions and 18 deletions

View file

@ -159,28 +159,54 @@ namespace Beefy
public static Result<void, FileError> LoadTextFile(String fileName, String outBuffer, bool autoRetry = true, delegate void() onPreFilter = null)
{
FileStream sr = scope .();
// Retry for a while if the other side is still writing out the file
for (int i = 0; i < 100; i++)
{
if (File.ReadAllText(fileName, outBuffer, true) case .Err(let err))
if (sr.Open(fileName) case .Err(let fileOpenErr))
{
bool retry = false;
if ((autoRetry) && (err case .FileOpenError(let fileOpenErr)))
if (autoRetry)
{
if (fileOpenErr == .SharingViolation)
retry = true;
}
if (!retry)
return .Err(err);
return .Err(.FileOpenError(fileOpenErr));
}
else
break;
Thread.Sleep(20);
}
int fileLen = sr.Length;
if (sr.TryRead(.((.)outBuffer.PrepareBuffer(fileLen), fileLen)) case .Err(let readErr))
return .Err(.FileReadError(readErr));
if (onPreFilter != null)
onPreFilter();
int startLen = Math.Min(fileLen, 4);
Span<uint8> bomSpan = .((.)outBuffer.Ptr, startLen);
var encoding = Encoding.DetectEncoding(bomSpan, var bomSize);
if (bomSize > 0)
{
if (encoding == .UTF8WithBOM)
{
outBuffer.Remove(0, bomSize);
}
else
{
String srcBuffer = scope .();
outBuffer.MoveTo(srcBuffer);
Span<uint8> inSpan = .((.)srcBuffer.Ptr, srcBuffer.Length);
inSpan.RemoveFromStart(bomSize);
encoding.DecodeToUTF8(inSpan, outBuffer).IgnoreError();
}
}
/*if (hashPtr != null)
*hashPtr = MD5.Hash(Span<uint8>((uint8*)outBuffer.Ptr, outBuffer.Length));*/

View file

@ -59,6 +59,27 @@ namespace System.Text
/// Decodes from bytes to UTF8
public abstract Result<int, DecodeError> DecodeToUTF8(Span<uint8> inBytes, StringView outChars);
/// Decodes from bytes to UTF8
public virtual Result<int, DecodeError> DecodeToUTF8(Span<uint8> inBytes, String outStr)
{
int utf8Len = GetDecodedUTF8Size(inBytes);
int prevSize = outStr.Length;
switch (DecodeToUTF8(inBytes, StringView(outStr.PrepareBuffer(utf8Len))))
{
case .Ok(let val):
return .Ok(val);
case .Err(let err):
switch (err)
{
case .PartialDecode(let decodedBytes, let outChars):
outStr.[Friend]mLength = (.)(prevSize + outChars);
case .FormatError:
}
return .Err(err);
}
}
public static Encoding DetectEncoding(Span<uint8> data, out int bomSize)
{
bomSize = 0;
@ -261,7 +282,7 @@ namespace System.Text
public override int GetDecodedUTF8Size(Span<uint8> bytes)
{
return Text.UTF16.GetLengthAsUTF8(Span<char16>((.)bytes.Ptr, bytes.Length));
return Text.UTF16.GetLengthAsUTF8(Span<char16>((.)bytes.Ptr, bytes.Length / 2));
}
public override Result<int, DecodeError> DecodeToUTF8(Span<uint8> inBytes, StringView outChars)