-
Notifications
You must be signed in to change notification settings - Fork 181
adding C interface without dep to C++ standard lib #370
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
Open
lemire
wants to merge
5
commits into
main
Choose a base branch
from
c_interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,31 @@ | ||
| #ifndef __FAST_FLOAT_STRTOD_H__ | ||
| #define __FAST_FLOAT_STRTOD_H__ | ||
|
|
||
| #if defined(__cplusplus) | ||
| extern "C" { | ||
| #endif | ||
| /** | ||
| * @brief Convert a string to a double using the fast_float library. This is | ||
| * a C-compatible wrapper around the fast_float parsing functionality, designed | ||
| * to mimic the behavior of the standard strtod function. | ||
| * | ||
| * This function parses the initial portion of the null-terminated string `nptr` | ||
| * and converts it to a `double`, similar to the standard `strtod` function but | ||
| * utilizing the high-performance fast_float library for parsing. | ||
| * | ||
| * On successful conversion, the result is returned. If parsing fails, errno is | ||
| * set to EINVAL and 0.0 is returned. | ||
| * | ||
| * @param nptr Pointer to the null-terminated string to be parsed. | ||
| * @param endptr If not NULL, a pointer to store the address of the first | ||
| * character after the parsed number. If parsing fails, it points | ||
| * to the beginning of the string. | ||
| * @return The converted double value on success, or 0.0 on failure. | ||
| */ | ||
| double fast_float_strtod(const char *in, char **out); | ||
|
|
||
| #if defined(__cplusplus) | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* __FAST_FLOAT_STRTOD_H__ */ | ||
This file contains hidden or 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,14 @@ | ||
| add_library(fast_float_strtod STATIC fast_float_strtod.cpp) | ||
| target_link_libraries(fast_float_strtod PRIVATE fast_float) | ||
|
|
||
| if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU") | ||
| target_link_options(fast_float_strtod PRIVATE -nostdlib++) | ||
| endif() | ||
|
|
||
| add_library(FastFloat::fast_float_strtod ALIAS fast_float_strtod) | ||
| target_include_directories( | ||
| fast_float_strtod | ||
| INTERFACE | ||
| $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> | ||
| $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> | ||
| ) |
This file contains hidden or 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,24 @@ | ||
| #include "fast_float/fast_float_strtod.h" | ||
| #include "fast_float/fast_float.h" | ||
| #include <cerrno> | ||
| #include <cstring> | ||
| #include <system_error> | ||
|
|
||
| extern "C" double fast_float_strtod(const char *nptr, char **endptr) { | ||
| double result = 0.0; | ||
|
|
||
| // Parse the string using fast_float's from_chars function | ||
| auto parse_result = fast_float::from_chars(nptr, nptr + strlen(nptr), result); | ||
|
|
||
| // Check if parsing encountered an error | ||
| if (parse_result.ec != std::errc{}) { | ||
| errno = EINVAL; | ||
| } | ||
|
|
||
| // Update endptr if provided | ||
| if (endptr != nullptr) { | ||
| *endptr = const_cast<char *>(parse_result.ptr); | ||
| } | ||
|
|
||
| return result; | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,28 @@ | ||
| #include "fast_float/fast_float_strtod.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <errno.h> | ||
|
|
||
| int main() { | ||
| // Test successful conversion | ||
| const char *str1 = "3.14159"; | ||
| char *end1; | ||
| errno = 0; | ||
| double d1 = fast_float_strtod(str1, &end1); | ||
| printf("Input: %s\n", str1); | ||
| printf("Converted: %f\n", d1); | ||
| printf("End pointer: %s\n", end1); | ||
| printf("errno: %d\n", errno); | ||
|
|
||
| // Test invalid input | ||
| const char *str2 = "invalid"; | ||
| char *end2; | ||
| errno = 0; | ||
| double d2 = fast_float_strtod(str2, &end2); | ||
| printf("\nInput: %s\n", str2); | ||
| printf("Converted: %f\n", d2); | ||
| printf("End pointer: %s\n", end2); | ||
| printf("errno: %d\n", errno); | ||
|
|
||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include guards with double underscores are bad habits, this is a reserved name so has undefined behaviour!
FAST_FLOAT_STRTOD_Hwould make a fine include guard :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I will fix this. Note that this is experimental and I will not be merging this PR immediately before it has received considerable review from others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To give some more constructive feedback, this does seem like a useful addition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!!! Much appreciated. You have increased the probability that we will move forward ! ah ah