Skip to content

Commit 08972f9

Browse files
committed
v2.1 release
1 parent aa9c995 commit 08972f9

File tree

4 files changed

+23
-47
lines changed

4 files changed

+23
-47
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Full specification:
22
[SPECIFICATION](SPEC.md)
3+
# New in v2.1:
4+
Note that this is not a stable implementation.
5+
```c
6+
bool cp_xterm_is_dark_mode();
7+
```
38
# New in v2.0:
49
Say hello to `T()` `F()` macro with _Generic() support!
510
Usage:
@@ -19,7 +24,6 @@ cprintf("{}{}{}{}","xxxxx"); // Also boom!!!!!!
1924
printf("%s","xxxxx"); // Good!
2025
cprintf("{}","xxxxxx"); // Also good :)
2126
```
22-
Update: cprintf can now recognise extra {} when running program when args of cprintf <= 15.
2327
# About GNU C:
2428
cprintf uses GNU C11, port/cprintf.c is in pure C std.
2529
# Let's make a colorful world!

cprintf.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void cprintf_free_buf__(void)
5757
cprintf_buffer = NULL;
5858
cprintf_buf_count = 0;
5959
}
60-
char *cprintf_regen_format__(const char *f, int limit)
60+
char *cprintf_regen_format__(const char *f)
6161
{
6262
/*
6363
* This function will regenerate the format string
@@ -66,32 +66,19 @@ char *cprintf_regen_format__(const char *f, int limit)
6666
*/
6767
char *ret = strdup(cprintf_avoid_null__(f));
6868
int j = 0;
69-
// For the case that limit is 0, that means no limit.
70-
// This is caused when args > 15, and we can't count it with CPRINTF_COUNT_ARGS.
71-
if (limit == 0) {
72-
limit = INT16_MAX;
73-
}
7469
int count = 0;
7570
size_t len = cprintf_strlen__(f);
76-
if (len == 0 || limit == 1) {
71+
if (len == 0) {
7772
cprintf_mark_buf__(ret);
7873
return ret;
7974
}
80-
limit = limit - 1;
8175
for (size_t i = 0; i < len - 1; i++) {
8276
if (f[i] == '{' && f[i + 1] == '}') {
8377
ret[j] = '%';
8478
ret[j + 1] = 's';
8579
j += 2;
8680
i++;
8781
count++;
88-
if (count == limit) {
89-
for (size_t k = i + 1; k < len - 1; k++) {
90-
ret[j] = f[k];
91-
j++;
92-
}
93-
break;
94-
}
9582
} else {
9683
ret[j] = f[i];
9784
j++;

include/cprintf.h

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#define false ((_Bool) + 0u)
5757
#endif
5858
#define CPRINTF_MAJOR 2
59-
#define CPRINTF_MINOR 0
59+
#define CPRINTF_MINOR 1
6060
bool cp_xterm_is_dark_mode(void);
6161
int cprintf__(const char *_Nonnull buf);
6262
int cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf);
@@ -129,30 +129,19 @@ extern bool cprintf_print_color_only_tty;
129129
#define F(cp_f_data__, cp_f_format__) \
130130
cprintf_to_char__(cp_f_data__, cprintf_get_fmt__(cp_f_data__, cp_f_format__))
131131
#define T(cp_t_data__) F(cp_t_data__, NULL)
132-
#define cprintf_len__(cpl_format__, ...) \
133-
(snprintf(NULL, 0, \
134-
cprintf_regen_format__(cpl_format__, CPRINTF_COUNT_ARGS(cpl_format__, ##__VA_ARGS__)), \
135-
##__VA_ARGS__) + \
136-
8)
137-
// Count the number of arguments passed to the csprintf().
138-
#define CPRINTF_COUNT_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, COUNT, ...) \
139-
_Generic((COUNT), int: COUNT, default: 0)
140-
#define CPRINTF_COUNT_ARGS(...) \
141-
CPRINTF_COUNT_ARGS_IMPL(__VA_ARGS__, ((int)15), ((int)14), ((int)13), ((int)12), ((int)11), ((int)10), \
142-
((int)9), ((int)8), ((int)7), ((int)6), ((int)5), ((int)4), ((int)3), ((int)2), \
143-
((int)1), ((int)0))
144-
#define csprintf(string, format, ...) \
145-
({ \
146-
int csp_ret__ = 0; \
147-
if (format == NULL) { \
148-
csp_ret__ = sprintf(string, "%s", "(null)"); \
149-
} else { \
150-
char *csp_fmt__ = \
151-
cprintf_regen_format__(format, CPRINTF_COUNT_ARGS(format, ##__VA_ARGS__)); \
152-
csp_ret__ = sprintf(string, csp_fmt__, ##__VA_ARGS__); \
153-
cprintf_free_buf__(); \
154-
} \
155-
csp_ret__; \
132+
#define cprintf_len__(cpl_format__, ...) \
133+
(snprintf(NULL, 0, cprintf_regen_format__(cpl_format__), ##__VA_ARGS__) + 8)
134+
#define csprintf(string, format, ...) \
135+
({ \
136+
int csp_ret__ = 0; \
137+
if (format == NULL) { \
138+
csp_ret__ = sprintf(string, "%s", "(null)"); \
139+
} else { \
140+
char *csp_fmt__ = cprintf_regen_format__(format); \
141+
csp_ret__ = sprintf(string, csp_fmt__, ##__VA_ARGS__); \
142+
cprintf_free_buf__(); \
143+
} \
144+
csp_ret__; \
156145
})
157146
#define cprintf(format, ...) \
158147
({ \
@@ -187,7 +176,7 @@ extern bool cprintf_print_color_only_tty;
187176
free(cfp_buf__); \
188177
}
189178
// For generic support.
190-
char *cprintf_regen_format__(const char *f, int limit);
179+
char *cprintf_regen_format__(const char *f);
191180
void cprintf_free_buf__(void);
192181
void cprintf_mark_buf__(char *b);
193182
// NOLINTEND

test.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "include/cprintf.h"
3131
int main(void)
3232
{
33-
/*
3433
cprintf("%s %s %d %s %d{clear}\n\n\n",
3534
"{black}ehi{114;5;14}bfwvjbkuiew{red}fgLCUEUIGvli{white}uEGFVLI{green}ILWR{yellow}Hvbi{blue}HVIBU{purple}WBHC{cyan}wvlrjvkjnvkjdcbvljb{clear}cjbskdjlvbjasf",
3635
"xxxxxxxxxxxxxxxxxxx[underline]xxxxxxxxx[clear]xx{cyan}xxxxx[highlight]xx{cyan}xxxx{base}xxxxxxxxxxxxx",
@@ -100,15 +99,12 @@ int main(void)
10099
cprintf("{}\n", T(i));
101100
cprintf("{}\n", "{}{}{}{}");
102101
cprintf("END\n");
103-
cprintf("{}{}{}\n");
104-
cprintf("{}{}{}{}{}{}{}\n", "x", T(1));
105102
cprintf("{red}red{green}green{blue}blue{yellow}yellow{purple}purple{cyan}cyan{white}white{black}black{clear}\n");
106103
cprintf("[red]red[green]green[blue]blue[yellow]yellow[purple]purple[cyan]cyan[white]white[black]black[clear]\n");
107-
*/
108104
if (cp_xterm_is_dark_mode()) {
109105
cprintf("{green}Dark mode is enabled\n");
110106
} else {
111107
cprintf("{red}Dark mode is not enabled{clear}\n");
112108
}
113-
// cprintf("cprintf version: %d.%d\n", CPRINTF_MAJOR, CPRINTF_MINOR);
109+
cprintf("cprintf version: %d.%d\n", CPRINTF_MAJOR, CPRINTF_MINOR);
114110
}

0 commit comments

Comments
 (0)