Skip to content

Commit df5db2c

Browse files
committed
adding C interface without dep to C++ standard lib
1 parent 221a492 commit df5db2c

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.14)
22

33

4-
project(fast_float VERSION 8.2.3 LANGUAGES CXX)
4+
project(fast_float VERSION 8.2.3 LANGUAGES CXX C)
55
set(FASTFLOAT_CXX_STANDARD 11 CACHE STRING "the C++ standard to use for fastfloat")
66
set(CMAKE_CXX_STANDARD ${FASTFLOAT_CXX_STANDARD})
77
option(FASTFLOAT_TEST "Enable tests" OFF)
@@ -32,6 +32,8 @@ endif()
3232

3333
add_library(fast_float INTERFACE)
3434

35+
add_subdirectory(src)
36+
3537

3638
option(FASTFLOAT_BENCHMARKS "Enable benchmarks" OFF)
3739
if(FASTFLOAT_BENCHMARKS)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef __FAST_FLOAT_STRTOD_H__
2+
#define __FAST_FLOAT_STRTOD_H__
3+
4+
#if defined(__cplusplus)
5+
extern "C"
6+
{
7+
#endif
8+
/**
9+
* @brief Convert a string to a double using the fast_float library. This is
10+
* a C-compatible wrapper around the fast_float parsing functionality, designed to
11+
* mimic the behavior of the standard strtod function.
12+
*
13+
* This function parses the initial portion of the null-terminated string `nptr`
14+
* and converts it to a `double`, similar to the standard `strtod` function but
15+
* utilizing the high-performance fast_float library for parsing.
16+
*
17+
* On successful conversion, the result is returned. If parsing fails, errno is
18+
* set to EINVAL and 0.0 is returned.
19+
*
20+
* @param nptr Pointer to the null-terminated string to be parsed.
21+
* @param endptr If not NULL, a pointer to store the address of the first
22+
* character after the parsed number. If parsing fails, it points
23+
* to the beginning of the string.
24+
* @return The converted double value on success, or 0.0 on failure.
25+
*/
26+
double fast_float_strtod(const char *in, char **out);
27+
28+
#if defined(__cplusplus)
29+
}
30+
#endif
31+
32+
#endif /* __FAST_FLOAT_STRTOD_H__ */

src/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_library(fast_float_strtod STATIC fast_float_strtod.cpp)
2+
target_link_libraries(fast_float_strtod PRIVATE fast_float)
3+
4+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
5+
target_link_options(fast_float_strtod PRIVATE -nostdlib++)
6+
endif()
7+
8+
add_library(FastFloat::fast_float_strtod ALIAS fast_float_strtod)
9+
target_include_directories(
10+
fast_float_strtod
11+
INTERFACE
12+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
13+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
14+
)

src/fast_float_strtod.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "fast_float/fast_float_strtod.h"
2+
#include "fast_float/fast_float.h"
3+
#include <cerrno>
4+
#include <cstring>
5+
#include <system_error>
6+
7+
extern "C" double fast_float_strtod(const char *nptr, char **endptr) {
8+
double result = 0.0;
9+
10+
// Parse the string using fast_float's from_chars function
11+
auto parse_result = fast_float::from_chars(nptr, nptr + strlen(nptr), result);
12+
13+
// Check if parsing encountered an error
14+
if (parse_result.ec != std::errc{}) {
15+
errno = EINVAL;
16+
}
17+
18+
// Update endptr if provided
19+
if (endptr != nullptr) {
20+
*endptr = const_cast<char*>(parse_result.ptr);
21+
}
22+
23+
return result;
24+
}

tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ endif(FASTFLOAT_EXHAUSTIVE)
108108

109109
add_subdirectory(build_tests)
110110
add_subdirectory(bloat_analysis)
111+
112+
add_executable(strtod_test strtod_test.c)
113+
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
114+
target_link_options(strtod_test PUBLIC -nostdlib++)
115+
endif()
116+
target_link_libraries(strtod_test PUBLIC fast_float_strtod)
117+
add_test(NAME strtod_test COMMAND strtod_test)

0 commit comments

Comments
 (0)