Skip to content

Commit 1335192

Browse files
committed
Revisioned changes
1 parent 2e471a0 commit 1335192

File tree

4 files changed

+93
-138
lines changed

4 files changed

+93
-138
lines changed

CMakeLists.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ endif ()
537537
add_library(fmt_c STATIC src/fmt-c.cc)
538538

539539
target_compile_features(fmt_c PUBLIC cxx_std_11)
540-
target_compile_definitions(fmt_c PUBLIC FMT_C_STATIC)
541540
target_link_libraries(fmt_c PUBLIC fmt::fmt)
542541

543542
target_include_directories(fmt_c PUBLIC
@@ -570,16 +569,13 @@ if(FMT_TEST)
570569

571570
add_executable(test-c-api test/test_c.c)
572571
target_link_libraries(test-c-api PRIVATE fmt::fmt_c)
573-
#needed for c11(_generic)
572+
set_target_properties(test-c-api PROPERTIES
573+
C_STANDARD 11
574+
C_STANDARD_REQUIRED ON
575+
)
574576
if(MSVC)
575-
target_compile_options(test-c-api PRIVATE /std:c11 /Zc:preprocessor)
576-
else()
577-
set_target_properties(test-c-api PROPERTIES
578-
C_STANDARD 11
579-
C_STANDARD_REQUIRED ON
580-
)
577+
target_compile_options(test-c-api PRIVATE /Zc:preprocessor)
581578
endif()
582579

583580
add_test(NAME c-api-test COMMAND test-c-api)
584581
endif()
585-

include/fmt/fmt-c.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#ifndef FMT_C_H
22
#define FMT_C_H
33

4-
#include <stdbool.h>
5-
#include <stddef.h>
6-
#include <stdint.h>
4+
#ifdef __cplusplus
5+
# define _Bool bool
6+
#endif
77
#include <stdio.h>
88

9-
#define FMT_C_MAX_ARGS 16
9+
void fmt_error_unsupported_type_detected(void);
10+
11+
enum { fmt_c_max_args = 16 };
1012

1113
typedef enum {
12-
fmt_ok = 0,
1314
fmt_err_exception = -1,
1415
fmt_err_memory = -2,
1516
fmt_err_invalid_arg = -3
@@ -34,29 +35,29 @@ typedef enum {
3435
typedef struct {
3536
fmt_type type;
3637
union {
37-
int64_t i64;
38-
uint64_t u64;
38+
long long i64;
39+
unsigned long long u64;
3940
float f32;
4041
double f64;
4142
long double f128;
4243
const char* str;
4344
const void* ptr; // Used for FMT_PTR and custom data
44-
int bool_val;
45-
int char_val;
45+
_Bool bool_val;
46+
char char_val;
4647
} value;
4748
} fmt_arg;
4849

4950
int fmt_vformat(char* buffer, size_t capacity, const char* format_str,
5051
const fmt_arg* args, size_t arg_count);
5152

52-
static inline fmt_arg fmt_from_int(int64_t x) {
53+
static inline fmt_arg fmt_from_int(long long x) {
5354
fmt_arg arg;
5455
arg.type = fmt_int;
5556
arg.value.i64 = x;
5657
return arg;
5758
}
5859

59-
static inline fmt_arg fmt_from_uint(uint64_t x) {
60+
static inline fmt_arg fmt_from_uint(unsigned long long x) {
6061
fmt_arg arg;
6162
arg.type = fmt_uint;
6263
arg.value.u64 = x;
@@ -98,7 +99,7 @@ static inline fmt_arg fmt_from_ptr(const void* x) {
9899
return arg;
99100
}
100101

101-
static inline fmt_arg fmt_from_bool(bool x) {
102+
static inline fmt_arg fmt_from_bool(_Bool x) {
102103
fmt_arg arg;
103104
arg.type = fmt_bool;
104105
arg.value.bool_val = x;
@@ -143,7 +144,7 @@ static inline fmt_arg fmt_from_char(int x) {
143144
const char*: fmt_from_str, \
144145
void*: fmt_from_ptr, \
145146
const void*: fmt_from_ptr, \
146-
default: fmt_from_ptr)(x)
147+
default: fmt_error_unsupported_type_detected)(x)
147148

148149
# define FMT_CAT(a, b) FMT_CAT_(a, b)
149150
# define FMT_CAT_(a, b) a##b

src/fmt-c.cc

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,54 @@
22

33
#include <fmt/core.h>
44

5-
#include <array>
65
#include <cassert>
7-
#include <exception>
8-
#include <string>
9-
#include <vector>
106

117
extern "C" {
128

13-
using Context = fmt::format_context;
9+
using format_arg = fmt::basic_format_arg<fmt::format_context>;
1410

15-
static bool populate_store(fmt::basic_format_arg<Context>* out,
16-
const fmt_arg* c_args, size_t arg_count) {
17-
if (arg_count > FMT_C_MAX_ARGS) {
11+
static bool populate_store(format_arg* out, const fmt_arg* c_args,
12+
size_t arg_count) {
13+
if (arg_count > fmt_c_max_args) {
1814
return false;
1915
}
2016

2117
for (size_t i = 0; i < arg_count; ++i) {
2218
switch (c_args[i].type) {
23-
case fmt_int:
24-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.i64);
25-
break;
26-
case fmt_uint:
27-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.u64);
28-
break;
29-
case fmt_float:
30-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.f32);
31-
break;
32-
case fmt_double:
33-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.f64);
34-
break;
35-
case fmt_long_double:
36-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.f128);
37-
break;
38-
case fmt_ptr:
39-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.ptr);
40-
break;
41-
case fmt_char:
42-
out[i] = fmt::basic_format_arg<Context>(
43-
static_cast<char>(c_args[i].value.char_val));
44-
break;
45-
case fmt_bool:
46-
out[i] = fmt::basic_format_arg<Context>(c_args[i].value.bool_val != 0);
47-
break;
48-
case fmt_string: {
49-
const char* s = c_args[i].value.str ? c_args[i].value.str : "(null)";
50-
out[i] = fmt::basic_format_arg<Context>(fmt::string_view(s));
51-
break;
52-
}
53-
54-
default: return false;
19+
case fmt_int: out[i] = c_args[i].value.i64; break;
20+
case fmt_uint: out[i] = c_args[i].value.u64; break;
21+
case fmt_float: out[i] = c_args[i].value.f32; break;
22+
case fmt_double: out[i] = c_args[i].value.f64; break;
23+
case fmt_long_double: out[i] = c_args[i].value.f128; break;
24+
case fmt_ptr: out[i] = c_args[i].value.ptr; break;
25+
case fmt_char: out[i] = c_args[i].value.char_val; break;
26+
case fmt_bool: out[i] = c_args[i].value.bool_val; break;
27+
case fmt_string: out[i] = c_args[i].value.str; break;
28+
default: return false;
5529
}
5630
}
5731
return true;
5832
}
33+
5934
int fmt_vformat(char* buffer, size_t capacity, const char* format_str,
6035
const fmt_arg* args, size_t arg_count) {
6136
assert(format_str);
6237

63-
fmt::basic_format_arg<Context> format_args[FMT_C_MAX_ARGS];
38+
format_arg format_args[fmt_c_max_args];
6439

65-
try {
66-
if (arg_count > 0) {
67-
assert(args);
68-
if (!populate_store(format_args, args, arg_count)) {
69-
return fmt_err_exception;
70-
}
40+
if (arg_count > 0) {
41+
assert(args);
42+
if (!populate_store(format_args, args, arg_count)) {
43+
return fmt_err_invalid_arg;
7144
}
45+
}
7246

73-
auto format_args_view = fmt::basic_format_args<Context>(
74-
format_args, static_cast<int>(arg_count));
75-
76-
size_t write_capacity = (capacity > 0) ? capacity - 1 : 0;
77-
auto result =
78-
fmt::vformat_to_n(buffer, write_capacity, format_str, format_args_view);
79-
80-
if (capacity > 0) {
81-
buffer[result.size] = '\0';
82-
}
83-
return static_cast<int>(result.size);
47+
auto format_args_view = fmt::basic_format_args<fmt::format_context>(
48+
format_args, static_cast<int>(arg_count));
8449

85-
} catch (const std::bad_alloc&) {
86-
return fmt_err_memory;
87-
} catch (...) {
88-
return fmt_err_exception;
89-
}
50+
auto result =
51+
fmt::vformat_to_n(buffer, capacity, format_str, format_args_view);
52+
return static_cast<int>(result.size);
9053
}
9154

92-
} // extern "C"
55+
} // extern "C"

0 commit comments

Comments
 (0)