Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isspace #451

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cesium.Compiler/stdlib/ctype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

__cli_import("Cesium.Runtime.CTypeFunctions::IsSpace")
int isspace(int ch);
12 changes: 12 additions & 0 deletions Cesium.IntegrationTests/stdlib/string/byte/ctype.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <ctype.h>
#include <limits.h>
#include <stdio.h>

int main(void)
{
for (int ndx = 0; ndx <= UCHAR_MAX; ndx++)
if (isspace(ndx))
printf("0x%02x ", ndx);

return 42;
}
40 changes: 40 additions & 0 deletions Cesium.Runtime.Tests/StdIoFunctionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text;

namespace Cesium.Runtime.Tests;

public class StdIoFunctionTests
{
[Theory]
[InlineData(9, "0x09")]
[InlineData(32, "0x20")]
public unsafe void FPrintFHex(long input, string expectedResult)
{
var format = Encoding.UTF8.GetBytes("0x%02x");

using var buffer = new MemoryStream();
var handleIndex = StdIoFunctions.Handles.Count;
try
{
using var writer = new StreamWriter(buffer);
var handle = new StdIoFunctions.StreamHandle
{
FileMode = "w",
// ReSharper disable once AccessToDisposedClosure
Writer = () => writer
};

StdIoFunctions.Handles.Add(handle);
fixed (byte* formatPtr = format)
{
Assert.Equal(4, StdIoFunctions.FPrintF((void*)handleIndex, formatPtr, &input));
}
}
finally
{
StdIoFunctions.Handles.RemoveAt(handleIndex);
}

var result = Encoding.UTF8.GetString(buffer.ToArray());
Assert.Equal(expectedResult, result);
}
}
21 changes: 21 additions & 0 deletions Cesium.Runtime/CTypeFunctions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Cesium.Runtime;

/// <summary>
/// Functions declared in the ctype.h
/// </summary>
public unsafe static class CTypeFunctions
{
public static int IsSpace(int value)
{
return value switch
{
0x20 => 1,
0x0c => 1,
0x0a => 1,
0x0d => 1,
0x09 => 1,
0x0b => 1,
_ => 0,
};
}
}
4 changes: 4 additions & 0 deletions Cesium.Runtime/Cesium.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Cesium.Runtime.Tests" />
</ItemGroup>

</Project>
38 changes: 31 additions & 7 deletions Cesium.Runtime/StdIoFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System.Runtime.InteropServices;
#if NETSTANDARD
using System.Text;
Expand All @@ -11,14 +10,14 @@ namespace Cesium.Runtime;
/// </summary>
public unsafe static class StdIoFunctions
{
record class StreamHandle
internal record StreamHandle
{
public required string FileMode { get; set; }
public Func<TextReader>? Reader { get; set; }
public Func<TextWriter>? Writer { get; set; }
}

private static List<StreamHandle> handles = new();
internal static List<StreamHandle> Handles = new();

private const int StdIn = 0;

Expand All @@ -28,17 +27,17 @@ record class StreamHandle

static StdIoFunctions()
{
handles.Add(new StreamHandle()
Handles.Add(new StreamHandle()
{
FileMode = "r",
Reader = () => Console.In,
});
handles.Add(new StreamHandle()
Handles.Add(new StreamHandle()
{
FileMode = "w",
Writer = () => Console.Out,
});
handles.Add(new StreamHandle()
Handles.Add(new StreamHandle()
{
FileMode = "w",
Writer = () => Console.Error,
Expand Down Expand Up @@ -129,6 +128,18 @@ public static int FPrintF(void* stream, byte* str, void* varargs)
streamWriter.Write(formatString.Substring(currentPosition, lengthTillPercent));
consumedBytes += lengthTillPercent;
int addition = 1;
int width = 0;
if (formatString[formatStartPosition + addition] == '0')
{
addition++;
}

while (formatString[formatStartPosition + addition] >= '0' && formatString[formatStartPosition + addition] <= '9')
{
width = width * 10 + (formatString[formatStartPosition + addition] - '0');
addition++;
}

string formatSpecifier = formatString[formatStartPosition + addition].ToString();
if (formatString[formatStartPosition + addition] == 'l')
{
Expand Down Expand Up @@ -180,6 +191,19 @@ public static int FPrintF(void* stream, byte* str, void* varargs)
consumedBytes += pointerValueString.Length;
consumedArgs++;
break;
case "x":
case "X":
nuint hexadecimalValue = ((nuint*)varargs)[consumedArgs];
if (hexadecimalValue != 0)
{
var targetFormat = "{0:" + formatSpecifier + (width == 0 ? "" : width) + "}";
// NOTE: without converting nuint to long, this was broken on .NET Framework
var hexadecimalValueString = string.Format(targetFormat, (long)hexadecimalValue);
streamWriter.Write(hexadecimalValueString);
consumedBytes += hexadecimalValueString.Length;
consumedArgs++;
}
break;
case "%":
streamWriter.Write('%');
consumedBytes += 1;
Expand Down Expand Up @@ -225,7 +249,7 @@ public static int FPrintF(void* stream, byte* str, void* varargs)
private static StreamHandle? GetStreamHandle(void* stream)
{
var handleIndex = (int)(IntPtr)stream;
var result = handles.ElementAtOrDefault(handleIndex);
var result = Handles.ElementAtOrDefault(handleIndex);
return result;
}
}
Loading