1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Corlib additions

This commit is contained in:
Brian Fiete 2019-11-22 12:26:51 -08:00
parent b756fe50eb
commit 788351e178
4 changed files with 251 additions and 9 deletions

View file

@ -165,7 +165,7 @@ namespace Beefy
// Retry for a while if the other side is still writing out the file
for (int i = 0; i < 100; i++)
{
if (sr.Open(fileName) case .Err(let fileOpenErr))
if (sr.Open(fileName, .Read, .Read) case .Err(let fileOpenErr))
{
bool retry = false;
if (autoRetry)

View file

@ -272,5 +272,124 @@ namespace System
}
}
#if BF_RUNTIME_CHECKS
#define BF_OPTSPAN_LENGTH
#endif
struct OptSpan<T>
{
protected T* mPtr;
#if BF_OPTSPAN_LENGTH
protected int mLength;
#endif
public this()
{
mPtr = null;
#if BF_OPTSPAN_LENGTH
mLength = 0;
#endif
}
public this(T[] array)
{
mPtr = &array.getRef(0);
#if BF_OPTSPAN_LENGTH
mLength = array.[Friend]mLength;
#endif
}
public this(T[] array, int index)
{
mPtr = &array[index];
#if BF_OPTSPAN_LENGTH
mLength = array.[Friend]mLength - index;
#endif
}
public this(T[] array, int index, int length)
{
if (length == 0)
mPtr = null;
else
mPtr = &array[index];
#if BF_OPTSPAN_LENGTH
mLength = length;
#endif
}
public this(T* memory, int length)
{
mPtr = memory;
#if BF_OPTSPAN_LENGTH
mLength = length;
#endif
}
public static implicit operator OptSpan<T> (T[] array)
{
return OptSpan<T>(array);
}
[Inline]
public T* Ptr
{
get
{
return mPtr;
}
}
public ref T this[int index]
{
[Inline, Checked]
get
{
#if BF_OPTSPAN_LENGTH
Debug.Assert((uint)index < (uint)mLength);
#endif
return ref mPtr[index];
}
[Inline, Unchecked]
get
{
return ref mPtr[index];
}
}
public OptSpan<T> Slice(int index, int length)
{
OptSpan<T> span;
span.mPtr = mPtr + index;
#if BF_OPTSPAN_LENGTH
Debug.Assert((uint)index + (uint)length <= (uint)mLength);
span.mLength = length;
#else
Debug.Assert(index >= 0);
#endif
return span;
}
public void Adjust(int ofs) mut
{
mPtr += ofs;
#if BF_OPTSPAN_LENGTH
Debug.Assert((uint)ofs <= (uint)mLength);
mLength -= ofs;
#endif
}
public OptSpan<uint8> ToRawData()
{
#if BF_OPTSPAN_LENGTH
return OptSpan<uint8>((uint8*)mPtr, mLength * alignof(T));
#else
return OptSpan<uint8>((uint8*)mPtr, 0);
#endif
}
}
//TODO: Make a ReadOnlySpan
}

View file

@ -20,20 +20,18 @@ ProductVersion = "0000000000000000"
[Configs.Debug.Win32]
TargetName = ""
OtherLinkFlags = ""
OptimizationLevel = "O0"
[Configs.Debug.Win64]
TargetDirectory = "$(WorkspaceDir)/dist"
TargetName = "BeefIDE_d"
OtherLinkFlags = "$(LinkFlags) Comdlg32.lib kernel32.lib user32.lib advapi32.lib shell32.lib IDEHelper64_d.lib"
DebugCommandArguments = "-workspace=c:\\Beef\\IDE"
DebugCommandArguments = "-proddir=C:\\Beef\\IDE"
DebugWorkingDirectory = "c:\\Beef\\IDE\\Tests\\EmptyTest"
EnvironmentVars = ["_NO_DEBUG_HEAP=1"]
[Configs.Release.Win32]
TargetName = ""
OtherLinkFlags = ""
OptimizationLevel = "O0"
[Configs.Release.Win64]
TargetDirectory = "$(WorkspaceDir)/dist"
@ -46,7 +44,6 @@ EnvironmentVars = ["_NO_DEBUG_HEAP=1"]
[Configs.Debug2.Win32]
TargetName = ""
OtherLinkFlags = ""
OptimizationLevel = "O0"
[Configs.Debug2.Win64]
TargetDirectory = "$(WorkspaceDir)/dist"

View file

@ -78,14 +78,21 @@ namespace System
}
}
[Inline]
public ref T this[int index]
{
get
{
[Inline, Checked]
get
{
Debug.Assert((uint)index < (uint)mLength);
return ref mPtr[index];
}
[Inline, Unchecked]
get
{
return ref mPtr[index];
}
}
}
public Span<T> Slice(int index)
{
@ -214,5 +221,124 @@ namespace System
}
}
#if BF_RUNTIME_CHECKS
#define BF_OPTSPAN_LENGTH
#endif
struct OptSpan<T>
{
protected T* mPtr;
#if BF_OPTSPAN_LENGTH
protected int mLength;
#endif
public this()
{
mPtr = null;
#if BF_OPTSPAN_LENGTH
mLength = 0;
#endif
}
public this(T[] array)
{
mPtr = &array.getRef(0);
#if BF_OPTSPAN_LENGTH
mLength = array.[Friend]mLength;
#endif
}
public this(T[] array, int index)
{
mPtr = &array[index];
#if BF_OPTSPAN_LENGTH
mLength = array.[Friend]mLength - index;
#endif
}
public this(T[] array, int index, int length)
{
if (length == 0)
mPtr = null;
else
mPtr = &array[index];
#if BF_OPTSPAN_LENGTH
mLength = length;
#endif
}
public this(T* memory, int length)
{
mPtr = memory;
#if BF_OPTSPAN_LENGTH
mLength = length;
#endif
}
public static implicit operator OptSpan<T> (T[] array)
{
return OptSpan<T>(array);
}
[Inline]
public T* Ptr
{
get
{
return mPtr;
}
}
public ref T this[int index]
{
[Inline, Checked]
get
{
#if BF_OPTSPAN_LENGTH
Debug.Assert((uint)index < (uint)mLength);
#endif
return ref mPtr[index];
}
[Inline, Unchecked]
get
{
return ref mPtr[index];
}
}
public OptSpan<T> Slice(int index, int length)
{
OptSpan<T> span;
span.mPtr = mPtr + index;
#if BF_OPTSPAN_LENGTH
Debug.Assert((uint)index + (uint)length <= (uint)mLength);
span.mLength = length;
#else
Debug.Assert(index >= 0);
#endif
return span;
}
public void Adjust(int ofs) mut
{
mPtr += ofs;
#if BF_OPTSPAN_LENGTH
Debug.Assert((uint)ofs <= (uint)mLength);
mLength -= ofs;
#endif
}
public OptSpan<uint8> ToRawData()
{
#if BF_OPTSPAN_LENGTH
return OptSpan<uint8>((uint8*)mPtr, mLength * alignof(T));
#else
return OptSpan<uint8>((uint8*)mPtr, 0);
#endif
}
}
//TODO: Make a ReadOnlySpan
}