-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite interpolated string handler again
- Loading branch information
Showing
2 changed files
with
73 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/ImGuiGlfw.Sample/Utils/InlineInterpolatedStringHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,55 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
|
||
namespace ImGuiGlfw.Sample.Utils; | ||
|
||
#pragma warning disable CA1822, RCS1163 | ||
[InterpolatedStringHandler] | ||
internal ref struct InlineInterpolatedStringHandler | ||
{ | ||
private int _charsWritten; | ||
|
||
public InlineInterpolatedStringHandler(int literalLength, int formattedCount) | ||
{ | ||
} | ||
|
||
public static implicit operator ReadOnlySpan<byte>(InlineInterpolatedStringHandler handler) | ||
{ | ||
return Inline.Buffer[..handler._charsWritten]; | ||
} | ||
|
||
public void AppendLiteral(ReadOnlySpan<byte> s) | ||
{ | ||
if (s.TryCopyTo(Inline.Buffer[_charsWritten..])) | ||
_charsWritten = s.Length; | ||
} | ||
|
||
public unsafe void AppendLiteral(string s) | ||
{ | ||
fixed (char* utf16Ptr = s) | ||
{ | ||
int utf8ByteCount = Encoding.UTF8.GetByteCount(utf16Ptr, s.Length); | ||
if (utf8ByteCount == 0) | ||
return; | ||
|
||
if (utf8ByteCount > Inline.Buffer.Length - _charsWritten) | ||
throw new InvalidOperationException("The formatted string is too long."); | ||
|
||
fixed (byte* bufferPtr = Inline.Buffer) | ||
_ = Encoding.UTF8.GetBytes(utf16Ptr, s.Length, bufferPtr _charsWritten, utf8ByteCount); | ||
} | ||
} | ||
|
||
public void AppendFormatted(ReadOnlySpan<byte> s) | ||
{ | ||
if (s.TryCopyTo(Inline.Buffer[_charsWritten..])) | ||
_charsWritten = s.Length; | ||
} | ||
|
||
public void AppendFormatted<T>(T t, ReadOnlySpan<char> format = default, IFormatProvider? provider = null) | ||
where T : IUtf8SpanFormattable | ||
{ | ||
t.TryFormat(Inline.Buffer[_charsWritten..], out int charsWritten, format, provider); | ||
_charsWritten = charsWritten; | ||
} | ||
} |