From eb85069b669e754836b9d4587ba03d3af1a5e975 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 26 Mar 2023 00:27:45 +0100 Subject: development branch for 4.2 Removed uses of c_auto and c_with in documentation examples and code examples. Still using c_defer a few places. Renamed c11/fmt.h to c11/print.h. Some additions in ccommon.h, e.g. c_const_cast(T, x). Improved docs. --- include/c11/print.h | 272 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 include/c11/print.h (limited to 'include/c11/print.h') diff --git a/include/c11/print.h b/include/c11/print.h new file mode 100644 index 00000000..1302d9cc --- /dev/null +++ b/include/c11/print.h @@ -0,0 +1,272 @@ +#ifndef FMT_H_INCLUDED +#define FMT_H_INCLUDED +/* +VER 2.0: NEW API: + +void fmt_print(fmt, ...); +void fmt_println(fmt, ...); +void fmt_printd(dest, fmt, ...); +void fmt_destroy(fmt_buffer* buf); + + dest - destination, one of: + FILE* fp Write to a file + char* strbuf Write to a pre-allocated string buffer + fmt_buffer* buf Auto realloc the needed memory (safe). + Set buf->stream=1 for stream-mode. + Call fmt_destroy(buf) after usage. + + fmt - format string + {} Auto-detected format. If :MOD is not specified, + float will use ".8g" format, and double ".16g". + {:MOD} Format modifiers: < left align (replaces -), default for char*, char. + > right align, default for numbers. + Other than that MOD can be normal printf format modifiers. + {{, }} Print chars {, and }. (note: a single % prints %). + +* C11 or higher required. +* MAX 255 chars fmt string by default. MAX 12 arguments after fmt string. +* Static linking by default, shared symbols by defining FMT_HEADER / FMT_IMPLEMENT. +* (c) operamint, 2022, MIT License. +----------------------------------------------------------------------------------- +#include "c11/print.h" + +int main() { + const double pi = 3.141592653589793; + const size_t x = 1234567890; + const char* string = "Hello world"; + const wchar_t* wstr = L"The whole"; + const char z = 'z'; + _Bool flag = 1; + unsigned char r = 123, g = 214, b = 90, w = 110; + char buffer[64]; + + fmt_print("Color: ({} {} {}), {}\n", r, g, b, flag); + fmt_println("Wide: {}, {}", wstr, L"wide world"); + fmt_println("{:10} {:10} {:10.2f}", 42ull, 43, pi); + fmt_println("{:>10} {:>10} {:>10}", z, z, w); + fmt_printd(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World"); + fmt_printd(stderr, "100%: {:<20} {:.*} {}\n", string, 4, pi, x); + fmt_printd(buffer, "Precision: {} {:.10} {}", string, pi, x); + fmt_println("{}", buffer); + fmt_println("Vector: ({}, {}, {})", 3.2, 3.3, pi); + + fmt_buffer out[1] = {{.stream=1}}; + fmt_printd(out, "{} {}", "Pi is:", pi); + fmt_print("{}, len={}, cap={}\n", out->data, out->len, out->cap); + fmt_printd(out, "{} {}", ", Pi squared is:", pi*pi); + fmt_print("{}, len={}, cap={}\n", out->data, out->len, out->cap); + fmt_destroy(out); +} +*/ +#include +#include +#include + +#define fmt_OVERLOAD(name, ...) \ + fmt_JOIN(name, fmt_NUMARGS(__VA_ARGS__))(__VA_ARGS__) +#define fmt_CONCAT(a, b) a ## b +#define fmt_JOIN(a, b) fmt_CONCAT(a, b) +#define fmt_EXPAND(...) __VA_ARGS__ +#define fmt_NUMARGS(...) _fmt_APPLY_ARG_N((__VA_ARGS__, _fmt_RSEQ_N)) + +#define _fmt_APPLY_ARG_N(args) fmt_EXPAND(_fmt_ARG_N args) +#define _fmt_RSEQ_N 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#define _fmt_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, \ + _14, _15, _16, N, ...) N + +#if defined FMT_HEADER || defined FMT_IMPLEMENT +# define FMT_API +#else +# define FMT_API static inline +#endif +#if defined FMT_NDEBUG || defined NDEBUG +# define fmt_OK(exp) (void)(exp) +#else +# define fmt_OK(exp) assert(exp) +#endif + +typedef struct { + char* data; + intptr_t cap, len; + _Bool stream; +} fmt_buffer; + +FMT_API void fmt_destroy(fmt_buffer* buf); +FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...); +FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); + +#ifndef FMT_MAX +#define FMT_MAX 256 +#endif + +#ifndef FMT_NOSHORTS +#define print(...) fmt_printd(stdout, __VA_ARGS__) +#define println(...) fmt_printd((fmt_buffer*)0, __VA_ARGS__) +#define printd fmt_printd +#endif + +#define fmt_print(...) fmt_printd(stdout, __VA_ARGS__) +#define fmt_println(...) fmt_printd((fmt_buffer*)0, __VA_ARGS__) +#define fmt_printd(...) fmt_OVERLOAD(fmt_printd, __VA_ARGS__) + +/* Primary function. */ +#define fmt_printd2(to, fmt) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 0, fmt); \ + fmt_OK(_n == 0); _fmt_fn(to)(to, fmt); } while (0) +#define fmt_printd3(to, fmt, c) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 1, fmt, _fc(c)); \ + fmt_OK(_n == 1); _fmt_fn(to)(to, _fs, c); } while (0) +#define fmt_printd4(to, fmt, c, d) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 2, fmt, _fc(c), _fc(d)); \ + fmt_OK(_n == 2); _fmt_fn(to)(to, _fs, c, d); } while (0) +#define fmt_printd5(to, fmt, c, d, e) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 3, fmt, _fc(c), _fc(d), _fc(e)); \ + fmt_OK(_n == 3); _fmt_fn(to)(to, _fs, c, d, e); } while (0) +#define fmt_printd6(to, fmt, c, d, e, f) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 4, fmt, _fc(c), _fc(d), _fc(e), _fc(f)); \ + fmt_OK(_n == 4); _fmt_fn(to)(to, _fs, c, d, e, f); } while (0) +#define fmt_printd7(to, fmt, c, d, e, f, g) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 5, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g)); \ + fmt_OK(_n == 5); _fmt_fn(to)(to, _fs, c, d, e, f, g); } while (0) +#define fmt_printd8(to, fmt, c, d, e, f, g, h) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 6, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h)); \ + fmt_OK(_n == 6); _fmt_fn(to)(to, _fs, c, d, e, f, g, h); } while (0) +#define fmt_printd9(to, fmt, c, d, e, f, g, h, i) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 7, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), _fc(i)); \ + fmt_OK(_n == 7); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i); } while (0) +#define fmt_printd10(to, fmt, c, d, e, f, g, h, i, j) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 8, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \ + _fc(i), _fc(j)); \ + fmt_OK(_n == 8); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j); } while (0) +#define fmt_printd11(to, fmt, c, d, e, f, g, h, i, j, k) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 9, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \ + _fc(i), _fc(j), _fc(k)); \ + fmt_OK(_n == 9); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k); } while (0) +#define fmt_printd12(to, fmt, c, d, e, f, g, h, i, j, k, m) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 10, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \ + _fc(i), _fc(j), _fc(k), _fc(m)); \ + fmt_OK(_n == 10); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m); } while (0) +#define fmt_printd13(to, fmt, c, d, e, f, g, h, i, j, k, m, n) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 11, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \ + _fc(i), _fc(j), _fc(k), _fc(m), _fc(n)); \ + fmt_OK(_n == 11); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n); } while (0) +#define fmt_printd14(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o) \ + do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 12, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \ + _fc(i), _fc(j), _fc(k), _fc(m), _fc(n), _fc(o)); \ + fmt_OK(_n == 12); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n, o); } while (0) + +#define _fmt_fn(x) _Generic ((x), \ + FILE*: fprintf, \ + char*: sprintf, \ + fmt_buffer*: _fmt_bprint) + +#if defined(_MSC_VER) && !defined(__clang__) +# define _signed_char_hhd +#else +# define _signed_char_hhd signed char: "hhd", +#endif + +#define _fc(x) _Generic (x, \ + _Bool: "d", \ + unsigned char: "hhu", \ + _signed_char_hhd \ + char: "c", \ + short: "hd", \ + unsigned short: "hu", \ + int: "d", \ + unsigned: "u", \ + long: "ld", \ + unsigned long: "lu", \ + long long: "lld", \ + unsigned long long: "llu", \ + float: "g", \ + double: "@g", \ + long double: "@Lg", \ + char*: "s", \ + wchar_t*: "ls", \ + void*: "p", \ + const char*: "s", \ + const wchar_t*: "ls", \ + const void*: "p") + +#if defined FMT_IMPLEMENT || !(defined FMT_HEADER || defined FMT_IMPLEMENT) + +#include +#include +#include + +FMT_API void fmt_destroy(fmt_buffer* buf) { + free(buf->data); +} + +FMT_API void _fmt_bprint(fmt_buffer* buf, const char* fmt, ...) { + va_list args, args2; + va_start(args, fmt); + if (buf == NULL) { + vprintf(fmt, args); putchar('\n'); + goto done1; + } + va_copy(args2, args); + const int n = vsnprintf(NULL, 0U, fmt, args); + if (n < 0) goto done2; + const intptr_t pos = buf->stream ? buf->len : 0; + buf->len = pos + n; + if (buf->len > buf->cap) { + buf->cap = buf->len + buf->cap/2; + buf->data = (char*)realloc(buf->data, (size_t)buf->cap + 1U); + } + vsprintf(buf->data + pos, fmt, args2); + done2: va_end(args2); + done1: va_end(args); +} + +FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) { + char *arg, *p0, ch; + int n = 0, empty; + va_list args; + va_start(args, fmt); + do { + switch ((ch = *fmt)) { + case '%': + *p++ = '%'; + break; + case '}': + if (*++fmt == '}') break; /* ok */ + n = 99; + continue; + case '{': + if (*++fmt == '{') break; /* ok */ + if (++n > nargs) continue; + if (*fmt != ':' && *fmt != '}') n = 99; + fmt += (*fmt == ':'); + empty = *fmt == '}'; + arg = va_arg(args, char *); + *p++ = '%', p0 = p; + while (1) switch (*fmt) { + case '\0': n = 99; /* nobreak */ + case '}': goto done; + case '<': *p++ = '-', ++fmt; break; + case '>': p0 = NULL; /* nobreak */ + case '-': ++fmt; break; + case '*': if (++n <= nargs) arg = va_arg(args, char *); /* nobreak */ + default: *p++ = *fmt++; + } + done: + switch (*arg) { + case 'g': if (empty) memcpy(p, ".8", 2), p += 2; break; + case '@': ++arg; if (empty) memcpy(p, ".16", 3), p += 3; break; + } + if (!strchr("csdioxXufFeEaAgGnp", fmt[-1])) + while (*arg) *p++ = *arg++; + if (p0 && (p[-1] == 's' || p[-1] == 'c')) /* left-align str */ + memmove(p0 + 1, p0, (size_t)(p++ - p0)), *p0 = '-'; + fmt += *fmt == '}'; + continue; + } + *p++ = *fmt++; + } while (ch); + va_end(args); + return n; +} +#endif +#endif -- cgit v1.2.3 From 2ad41420a973a3f1bd1ca47ab0f61b8f59ab9e66 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 5 Apr 2023 14:44:31 +0200 Subject: Internal: renamed untemplate.h to template2.h --- include/c11/print.h | 31 +++++++++-------- include/stc/algo/csort.h | 2 +- include/stc/carc.h | 2 +- include/stc/cbox.h | 2 +- include/stc/cdeq.h | 2 +- include/stc/clist.h | 2 +- include/stc/cmap.h | 2 +- include/stc/cpque.h | 2 +- include/stc/csmap.h | 2 +- include/stc/cstack.h | 2 +- include/stc/cvec.h | 2 +- include/stc/priv/template2.h | 78 +++++++++++++++++++++++++++++++++++++++++++ include/stc/priv/untemplate.h | 78 ------------------------------------------- misc/examples/triples.c | 10 +++--- 14 files changed, 110 insertions(+), 107 deletions(-) create mode 100644 include/stc/priv/template2.h delete mode 100644 include/stc/priv/untemplate.h (limited to 'include/c11/print.h') diff --git a/include/c11/print.h b/include/c11/print.h index 1302d9cc..7c155875 100644 --- a/include/c11/print.h +++ b/include/c11/print.h @@ -1,7 +1,10 @@ #ifndef FMT_H_INCLUDED #define FMT_H_INCLUDED /* -VER 2.0: NEW API: +VER 2.1: NEW API: +void print(fmt, ...); +void println(fmt, ...); +void printd(dest, fmt, ...); void fmt_print(fmt, ...); void fmt_println(fmt, ...); @@ -40,21 +43,21 @@ int main() { unsigned char r = 123, g = 214, b = 90, w = 110; char buffer[64]; - fmt_print("Color: ({} {} {}), {}\n", r, g, b, flag); - fmt_println("Wide: {}, {}", wstr, L"wide world"); - fmt_println("{:10} {:10} {:10.2f}", 42ull, 43, pi); - fmt_println("{:>10} {:>10} {:>10}", z, z, w); - fmt_printd(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World"); - fmt_printd(stderr, "100%: {:<20} {:.*} {}\n", string, 4, pi, x); - fmt_printd(buffer, "Precision: {} {:.10} {}", string, pi, x); - fmt_println("{}", buffer); - fmt_println("Vector: ({}, {}, {})", 3.2, 3.3, pi); + print("Color: ({} {} {}), {}\n", r, g, b, flag); + println("Wide: {}, {}", wstr, L"wide world"); + println("{:10} {:10} {:10.2f}", 42ull, 43, pi); + println("{:>10} {:>10} {:>10}", z, z, w); + printd(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World"); + printd(stderr, "100%: {:<20} {:.*} {}\n", string, 4, pi, x); + printd(buffer, "Precision: {} {:.10} {}", string, pi, x); + println("{}", buffer); + println("Vector: ({}, {}, {})", 3.2, 3.3, pi); fmt_buffer out[1] = {{.stream=1}}; - fmt_printd(out, "{} {}", "Pi is:", pi); - fmt_print("{}, len={}, cap={}\n", out->data, out->len, out->cap); - fmt_printd(out, "{} {}", ", Pi squared is:", pi*pi); - fmt_print("{}, len={}, cap={}\n", out->data, out->len, out->cap); + printd(out, "{} {}", "Pi is:", pi); + print("{}, len={}, cap={}\n", out->data, out->len, out->cap); + printd(out, "{} {}", ", Pi squared is:", pi*pi); + print("{}, len={}, cap={}\n", out->data, out->len, out->cap); fmt_destroy(out); } */ diff --git a/include/stc/algo/csort.h b/include/stc/algo/csort.h index 02ac4e34..53fe9fcc 100644 --- a/include/stc/algo/csort.h +++ b/include/stc/algo/csort.h @@ -86,4 +86,4 @@ static inline void c_PASTE(cqsort_, i_tag)(i_val arr[], intptr_t lo, intptr_t hi static inline void c_PASTE(csort_, i_tag)(i_val arr[], intptr_t n) { c_PASTE(cqsort_, i_tag)(arr, 0, n - 1); } -#include "../priv/untemplate.h" +#include "../priv/template2.h" diff --git a/include/stc/carc.h b/include/stc/carc.h index 16d3a2d4..8ef80b12 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -210,4 +210,4 @@ STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self) #undef _i_eq #undef _i_atomic_inc #undef _i_atomic_dec_and_test -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 4d2cb1f1..ca88fc66 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -194,4 +194,4 @@ STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self) #endif #undef _i_eq -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index ef1700f5..ff6e744f 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -443,4 +443,4 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #endif // !_i_queue #endif // IMPLEMENTATION #define CDEQ_H_INCLUDED -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/clist.h b/include/stc/clist.h index 6e6b6d45..f7fb4152 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -404,4 +404,4 @@ STC_DEF bool _cx_memb(_sort_with)(_cx_self* self, int(*cmp)(const _cx_value*, co #endif // !c_no_cmp #endif // i_implement #define CLIST_H_INCLUDED -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 1976738d..9a503367 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -481,4 +481,4 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { #undef _i_MAP_ONLY #undef _i_SET_ONLY #define CMAP_H_INCLUDED -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 023c9d27..b95b5020 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -160,4 +160,4 @@ _cx_memb(_push)(_cx_self* self, _cx_value value) { #endif #define CPQUE_H_INCLUDED -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 716c1bfe..22607196 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -595,4 +595,4 @@ _cx_memb(_drop)(_cx_self* self) { #undef _i_MAP_ONLY #undef _i_SET_ONLY #define CSMAP_H_INCLUDED -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 1fc3f377..c2792358 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -189,4 +189,4 @@ STC_INLINE void _cx_memb(_next)(_cx_iter* it) STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) { if ((it.ref += n) >= it.end) it.ref = NULL ; return it; } -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/cvec.h b/include/stc/cvec.h index e257f85a..a1aa74b2 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -438,4 +438,4 @@ STC_DEF int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #endif // !c_no_cmp #endif // i_implement #define CVEC_H_INCLUDED -#include "priv/untemplate.h" +#include "priv/template2.h" diff --git a/include/stc/priv/template2.h b/include/stc/priv/template2.h new file mode 100644 index 00000000..27c6a890 --- /dev/null +++ b/include/stc/priv/template2.h @@ -0,0 +1,78 @@ +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#undef i_type +#undef i_tag +#undef i_imp +#undef i_opt +#undef i_less +#undef i_cmp +#undef i_eq +#undef i_hash +#undef i_rawclass +#undef i_capacity +#undef i_ssize + +#undef i_val +#undef i_val_str +#undef i_val_ssv +#undef i_valboxed +#undef i_valclass +#undef i_valraw +#undef i_valclone +#undef i_valfrom +#undef i_valto +#undef i_valdrop + +#undef i_key +#undef i_key_str +#undef i_key_ssv +#undef i_keyboxed +#undef i_keyclass +#undef i_keyraw +#undef i_keyclone +#undef i_keyfrom +#undef i_keyto +#undef i_keydrop + +#undef i_header +#undef i_implement +#undef i_static +#undef i_extern + +#undef i_allocator +#undef i_malloc +#undef i_calloc +#undef i_realloc +#undef i_free + +#undef i_no_cmp +#undef i_no_hash +#undef i_no_clone +#undef i_no_emplace +#undef i_is_forward + +#undef _i_prefix +#undef _i_expandby +#undef _i_has_from +#undef _i_has_eq +#undef _i_template diff --git a/include/stc/priv/untemplate.h b/include/stc/priv/untemplate.h deleted file mode 100644 index 27c6a890..00000000 --- a/include/stc/priv/untemplate.h +++ /dev/null @@ -1,78 +0,0 @@ -/* MIT License - * - * Copyright (c) 2023 Tyge Løvset - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#undef i_type -#undef i_tag -#undef i_imp -#undef i_opt -#undef i_less -#undef i_cmp -#undef i_eq -#undef i_hash -#undef i_rawclass -#undef i_capacity -#undef i_ssize - -#undef i_val -#undef i_val_str -#undef i_val_ssv -#undef i_valboxed -#undef i_valclass -#undef i_valraw -#undef i_valclone -#undef i_valfrom -#undef i_valto -#undef i_valdrop - -#undef i_key -#undef i_key_str -#undef i_key_ssv -#undef i_keyboxed -#undef i_keyclass -#undef i_keyraw -#undef i_keyclone -#undef i_keyfrom -#undef i_keyto -#undef i_keydrop - -#undef i_header -#undef i_implement -#undef i_static -#undef i_extern - -#undef i_allocator -#undef i_malloc -#undef i_calloc -#undef i_realloc -#undef i_free - -#undef i_no_cmp -#undef i_no_hash -#undef i_no_clone -#undef i_no_emplace -#undef i_is_forward - -#undef _i_prefix -#undef _i_expandby -#undef _i_has_from -#undef _i_has_eq -#undef _i_template diff --git a/misc/examples/triples.c b/misc/examples/triples.c index aac7967a..520bf012 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -4,12 +4,12 @@ #include void triples_vanilla(int n) { - for (int i = 5, c = 1;; ++c) { + for (int c = 5; n; ++c) { for (int a = 1; a < c; ++a) { for (int b = a + 1; b < c; ++b) { if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c) { - if (i++ == n) goto done; printf("{%d, %d, %d}\n", a, b, c); + if (--n == 0) goto done; } } } @@ -25,12 +25,12 @@ struct triples { bool triples_next(struct triples* I) { cco_begin(I); - for (I->c = 5;; ++I->c) { + for (I->c = 5; I->n; ++I->c) { for (I->a = 1; I->a < I->c; ++I->a) { for (I->b = I->a + 1; I->b < I->c; ++I->b) { if ((int64_t)I->a*I->a + (int64_t)I->b*I->b == (int64_t)I->c*I->c) { - if (I->n-- == 0) cco_return; cco_yield(true); + if (--I->n == 0) cco_return; } } } @@ -61,7 +61,7 @@ int main() while (triples_next(&t)) { if (gcd(t.a, t.b) > 1) continue; - if (t.c < 1000) + if (t.c < 100) printf("%d: {%d, %d, %d}\n", ++n, t.a, t.b, t.c); else cco_stop(&t); -- cgit v1.2.3