Skip to content

Commit

Permalink
Merge pull request #409 from NoahStolk/use-readonlyspan-in-manual-img…
Browse files Browse the repository at this point in the history
…ui-functions

Use ReadOnlySpan<char> in manual ImGui functions
  • Loading branch information
zaafar authored Jun 12, 2023
2 parents 42fdb7d + 156279e commit b5256ac
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/ImGui.NET/ImDrawList.Manual.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System.Numerics;
using System;
using System.Numerics;
using System.Text;

namespace ImGuiNET
{
public unsafe partial struct ImDrawListPtr
{
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public void AddText(Vector2 pos, uint col, ReadOnlySpan<char> text_begin)
#else
public void AddText(Vector2 pos, uint col, string text_begin)
#endif
{
int text_begin_byteCount = Encoding.UTF8.GetByteCount(text_begin);
byte* native_text_begin = stackalloc byte[text_begin_byteCount + 1];
Expand All @@ -18,7 +23,11 @@ public void AddText(Vector2 pos, uint col, string text_begin)
ImGuiNative.ImDrawList_AddText_Vec2(NativePtr, pos, col, native_text_begin, native_text_end);
}

public void AddText(ImFontPtr font, float font_size, Vector2 pos, uint col, string text_begin)
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public void AddText(ImFontPtr font, float font_size, Vector2 pos, uint col, ReadOnlySpan<char> text_begin)
#else
public void AddText(ImFontPtr font, float font_size, Vector2 pos, uint col, string text_begin)
#endif
{
ImFont* native_font = font.NativePtr;
int text_begin_byteCount = Encoding.UTF8.GetByteCount(text_begin);
Expand Down
131 changes: 131 additions & 0 deletions src/ImGui.NET/ImGui.Manual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ namespace ImGuiNET
public static unsafe partial class ImGui
{
public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
byte[] buf,
uint buf_size)
{
return InputText(label, buf, buf_size, 0, null, IntPtr.Zero);
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
byte[] buf,
uint buf_size,
ImGuiInputTextFlags flags)
Expand All @@ -26,7 +34,11 @@ public static bool InputText(
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
byte[] buf,
uint buf_size,
ImGuiInputTextFlags flags,
Expand All @@ -36,7 +48,11 @@ public static bool InputText(
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
byte[] buf,
uint buf_size,
ImGuiInputTextFlags flags,
Expand Down Expand Up @@ -71,25 +87,41 @@ public static bool InputText(
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength) => InputText(label, ref input, maxLength, 0, null, IntPtr.Zero);

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
ImGuiInputTextFlags flags) => InputText(label, ref input, maxLength, flags, null, IntPtr.Zero);

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback) => InputText(label, ref input, maxLength, flags, callback, IntPtr.Zero);

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
Expand Down Expand Up @@ -157,28 +189,44 @@ public static bool InputText(
}

public static bool InputTextMultiline(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
Vector2 size) => InputTextMultiline(label, ref input, maxLength, size, 0, null, IntPtr.Zero);

public static bool InputTextMultiline(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
Vector2 size,
ImGuiInputTextFlags flags) => InputTextMultiline(label, ref input, maxLength, size, flags, null, IntPtr.Zero);

public static bool InputTextMultiline(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
Vector2 size,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback) => InputTextMultiline(label, ref input, maxLength, size, flags, callback, IntPtr.Zero);

public static bool InputTextMultiline(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
ref string input,
uint maxLength,
Vector2 size,
Expand Down Expand Up @@ -248,29 +296,49 @@ public static bool InputTextMultiline(
}

public static bool InputTextWithHint(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
ReadOnlySpan<char> hint,
#else
string label,
string hint,
#endif
ref string input,
uint maxLength) => InputTextWithHint(label, hint, ref input, maxLength, 0, null, IntPtr.Zero);

public static bool InputTextWithHint(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
ReadOnlySpan<char> hint,
#else
string label,
string hint,
#endif
ref string input,
uint maxLength,
ImGuiInputTextFlags flags) => InputTextWithHint(label, hint, ref input, maxLength, flags, null, IntPtr.Zero);

public static bool InputTextWithHint(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
ReadOnlySpan<char> hint,
#else
string label,
string hint,
#endif
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback) => InputTextWithHint(label, hint, ref input, maxLength, flags, callback, IntPtr.Zero);

public static bool InputTextWithHint(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
ReadOnlySpan<char> hint,
#else
string label,
string hint,
#endif
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
Expand Down Expand Up @@ -355,6 +423,40 @@ public static bool InputTextWithHint(
return result != 0;
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public static Vector2 CalcTextSize(ReadOnlySpan<char> text)
=> CalcTextSizeImpl(text);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start)
=> CalcTextSizeImpl(text, start);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, float wrapWidth)
=> CalcTextSizeImpl(text, wrapWidth: wrapWidth);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, bool hideTextAfterDoubleHash)
=> CalcTextSizeImpl(text, hideTextAfterDoubleHash: hideTextAfterDoubleHash);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start, int length)
=> CalcTextSizeImpl(text, start, length);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start, bool hideTextAfterDoubleHash)
=> CalcTextSizeImpl(text, start, hideTextAfterDoubleHash: hideTextAfterDoubleHash);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start, float wrapWidth)
=> CalcTextSizeImpl(text, start, wrapWidth: wrapWidth);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, bool hideTextAfterDoubleHash, float wrapWidth)
=> CalcTextSizeImpl(text, hideTextAfterDoubleHash: hideTextAfterDoubleHash, wrapWidth: wrapWidth);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start, int length, bool hideTextAfterDoubleHash)
=> CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start, int length, float wrapWidth)
=> CalcTextSizeImpl(text, start, length, wrapWidth: wrapWidth);

public static Vector2 CalcTextSize(ReadOnlySpan<char> text, int start, int length, bool hideTextAfterDoubleHash, float wrapWidth)
=> CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash, wrapWidth);
#else
public static Vector2 CalcTextSize(string text)
=> CalcTextSizeImpl(text);

Expand Down Expand Up @@ -387,9 +489,14 @@ public static Vector2 CalcTextSize(string text, int start, int length, float wra

public static Vector2 CalcTextSize(string text, int start, int length, bool hideTextAfterDoubleHash, float wrapWidth)
=> CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash, wrapWidth);
#endif

private static Vector2 CalcTextSizeImpl(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> text,
#else
string text,
#endif
int start = 0,
int? length = null,
bool hideTextAfterDoubleHash = false,
Expand Down Expand Up @@ -429,15 +536,23 @@ private static Vector2 CalcTextSizeImpl(
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
IntPtr buf,
uint buf_size)
{
return InputText(label, buf, buf_size, 0, null, IntPtr.Zero);
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
IntPtr buf,
uint buf_size,
ImGuiInputTextFlags flags)
Expand All @@ -446,7 +561,11 @@ public static bool InputText(
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
IntPtr buf,
uint buf_size,
ImGuiInputTextFlags flags,
Expand All @@ -456,7 +575,11 @@ public static bool InputText(
}

public static bool InputText(
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
ReadOnlySpan<char> label,
#else
string label,
#endif
IntPtr buf,
uint buf_size,
ImGuiInputTextFlags flags,
Expand Down Expand Up @@ -486,7 +609,11 @@ public static bool InputText(
return ret;
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public static bool Begin(ReadOnlySpan<char> name, ImGuiWindowFlags flags)
#else
public static bool Begin(string name, ImGuiWindowFlags flags)
#endif
{
int utf8NameByteCount = Encoding.UTF8.GetByteCount(name);
byte* utf8NameBytes;
Expand All @@ -512,7 +639,11 @@ public static bool Begin(string name, ImGuiWindowFlags flags)
return ret != 0;
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
public static bool MenuItem(ReadOnlySpan<char> label, bool enabled)
#else
public static bool MenuItem(string label, bool enabled)
#endif
{
return MenuItem(label, string.Empty, false, enabled);
}
Expand Down
8 changes: 8 additions & 0 deletions src/ImGui.NET/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ internal static bool AreStringsEqual(byte* a, int aLength, byte* b)

internal static void Free(byte* ptr) => Marshal.FreeHGlobal((IntPtr)ptr);

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
internal static int CalcSizeInUtf8(ReadOnlySpan<char> s, int start, int length)
#else
internal static int CalcSizeInUtf8(string s, int start, int length)
#endif
{
if (start < 0 || length < 0 || start + length > s.Length)
{
Expand Down Expand Up @@ -71,7 +75,11 @@ internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount)
}
}

#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER
internal static int GetUtf8(ReadOnlySpan<char> s, int start, int length, byte* utf8Bytes, int utf8ByteCount)
#else
internal static int GetUtf8(string s, int start, int length, byte* utf8Bytes, int utf8ByteCount)
#endif
{
if (start < 0 || length < 0 || start + length > s.Length)
{
Expand Down

0 comments on commit b5256ac

Please sign in to comment.