Skip to content
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
6 changes: 6 additions & 0 deletions Cesium.Compiler/stdlib/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ long strtol(const char* str, char** str_end, int base);
__cli_import("Cesium.Runtime.StdLibFunctions::StrToUL")
unsigned long strtoul(const char* str, char** str_end, int base);

__cli_import("Cesium.Runtime.StdLibFunctions::StrToF")
float strtof(const char* str, char** str_end);

__cli_import("Cesium.Runtime.StdLibFunctions::StrToD")
double strtod(const char* str, char** str_end);

__cli_import("Cesium.Runtime.StdLibFunctions::GetErrNo")
int* _errno(void);

Expand Down
4 changes: 2 additions & 2 deletions Cesium.IntegrationTests/stdlib/io/fprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ int main(void)
printf("\tScientific:\t%E %e\n", 1.5, 1.5);
// Not in the mood for this.
//printf("\tHexadecimal:\t%a %A\n", 1.5, 1.5);
//float divzero = 0.0F;
//printf("\tSpecial values:\t0/0=%g 1/0=%g\n", 0.0 / divzero, 1.0 / divzero);
float divzero = 0.0F;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really should be part of #962

printf("\tSpecial values:\t0/0=%g 1/0=%g\n", 0.0 / divzero, 1.0 / divzero);

//printf("Fixed-width types:\n");
//printf("\tLargest 32-bit value is %" PRIu32 " or %#" PRIx32 "\n",
Expand Down
37 changes: 37 additions & 0 deletions Cesium.IntegrationTests/stdlib/string/byte/strtof.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
*
* SPDX-License-Identifier: MIT
*/

#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
// parsing with error handling
const char* p = "111.11 -2.22 Nan nan(2) inF 0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz";
printf("Parsing '%s':\n", p);
char* end = NULL;
for (double f = strtod(p, &end); p != end; f = strtod(p, &end))
{
printf("'%.*s' -> ", (int)(end - p), p);
p = end;
if (errno == ERANGE)
{
printf("range error, got ");
errno = 0;
}
printf("%f\n", f);
}

// parsing without error handling
printf("\" -0.0000000123junk\" --> %g\n", strtod(" -0.0000000123junk", NULL));
printf("\"junk\" --> %g\n", strtod("junk", NULL));


return 42;
}
28 changes: 28 additions & 0 deletions Cesium.Runtime.Tests/StdLibFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class StdLibFunctionTests
[InlineData("012", 0, 10)]
[InlineData("0xA", 0, 10)]
[InlineData("junk", 0, 0)]
[InlineData("111.", 10, 111)]
public unsafe void StrToL(string input, int @base, long expectedResult)
{
var stringBytes = Encoding.UTF8.GetBytes(input);
Expand Down Expand Up @@ -59,4 +60,31 @@ public unsafe void StrToLOutOfRange()
Assert.Equal(long.MinValue, actual);
}
}
[Theory]
[InlineData("111.11", 111.11, 0)]
[InlineData("-2.22", -2.22, 0)]
[InlineData(" -2.22", -2.22, 0)]
[InlineData("NaN", double.NaN, 0)]
[InlineData("nan(2)", double.NaN, 0)]
[InlineData("inF", double.PositiveInfinity, 0)]
[InlineData("0X1.BC70A3D70A3D7P+6", 111.11, 0)]
[InlineData("junk", 0, (int)(byte)'j')]
public unsafe void StrToD(string input, double expectedResult, int expectedLastByte)
{
var stringBytes = Encoding.UTF8.GetBytes(input);
fixed (byte* str = stringBytes)
{
var actual = StdLibFunctions.StrToD(str, null);

Assert.Equal(expectedResult, actual);
}

byte* strEnd;
fixed (byte* str = stringBytes)
{
var actual = StdLibFunctions.StrToD(str, &strEnd);
Assert.Equal(expectedResult, actual);
Assert.Equal(expectedLastByte, *strEnd);
}
}
}
106 changes: 106 additions & 0 deletions Cesium.Runtime/StdLibFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,112 @@ public static ulong StrToUL(byte* str, byte** str_end, int @base)
return (ulong)StrToL(str, str_end, @base);
}

public static float StrToF(byte* str, byte** str_end)
{
return (float)StrToD(str, str_end);
}

public static double StrToD(byte* str, byte** str_end)
{
byte* current = str;
byte* next = str;
byte currentChar;
int @base = 10;
errNo = 0;
do
{
currentChar = *current++;
}
while (CTypeFunctions.IsSpace(currentChar) != 0);

bool negate = false;
if (currentChar == '-')
{
negate = true;
currentChar = *current++;
}
else if (currentChar == '+')
currentChar = *current++;

if (currentChar == 'I' || currentChar == 'i')
{
if ((current[0] == 'n' || current[0] == 'N') && (current[1] == 'f' || current[1] == 'F'))
{
if (str_end != null)
*str_end = current + 2;
return negate ? double.NegativeInfinity : double.PositiveInfinity;
}
errNo = 34 /*ERANGE*/;
if (str_end != null)
*str_end = str;
}

if (currentChar == 'n' || currentChar == 'N')
{
if ((current[0] == 'a' || current[0] == 'A') && (current[1] == 'n' || current[1] == 'N'))
{
current += 2;
if (current[0] == '(')
{
current++;
while (*current != ')' && (CTypeFunctions.IsAlnum(*current) != 0 || *current == '_'))
{
current++;
}
if (*current == ')')
{
current++;
}
}
if (str_end != null)
*str_end = current + 2;
return double.NaN;
}
errNo = 34 /*ERANGE*/;
}

if (currentChar == '0' && (*current == 'x' || *current == 'X'))
{
currentChar = current[1];
current += 2;
@base = 16;
}

double result = StrToL(current - 1, &next, @base);
if (errNo == 0)
{
current = next;
}

if ((char)(*next) == '.')
{
current++;
long exponenta = StrToL(current, &next, @base);
current = next;
if (exponenta != 0)
{
var expPower = Math.Log(exponenta, @base);
expPower = Math.Ceiling(expPower);
result += exponenta / Math.Pow(@base, expPower);
}
}

if ((*current == 'E' && @base == 10) || (*current == 'P' && @base == 16))
{
current++;
var exp = StrToL(current, &next, @base);
current = next;
result = result * Math.Pow(@base == 16 ? 2 : @base, exp);
}

if (str_end != null)
{
*str_end = current;
}

return (negate ? -result : result);
}

private static EnvVarsStorage InitEnvVarsStorage()
{
var processEnvs = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process);
Expand Down
Loading