181 points · by wooby · 7y ago · 59 comments
- [0] https://byuu.org/library/libco/
- [1] https://fluentbit.io
Nevertheless, even in C, I'd find it conceptually simpler to understand if the functions were written as pure functions, returning a tuple with (c, state) and passing the state back into the caller. Using non-idiomatic tricks and macros in this way is harder to read and understand.
static int
u_getopt_r(int argc, char *const argv[], const char *shortopts, struct u_getopt_r *K)
{
K->optarg = NULL;
K->optopt = 0;
GETOPT_ENTER;
while (K->optind < argc) {
K->cp = argv[K->optind];
if (!K->cp || *(K->cp) != '-' || !strcmp(K->cp, "-")) {
break;
} else if (!strcmp(K->cp, "--")) {
K->optind++;
break;
}
for (;;) {
char *shortopt;
if (!(K->optopt = *++K->cp)) {
K->optind++;
break;
} else if (!(shortopt = strchr(shortopts, K->optopt))) {
getopt_err(argc, argv, shortopts, K, "illegal option -- %c\n", K->optopt);
GETOPT_YIELD('?');
} else if (shortopt[1] != ':') {
GETOPT_YIELD(K->optopt);
} else if (K->cp[1]) {
K->optarg = &K->cp[1];
K->optind++;
GETOPT_YIELD(K->optopt);
break;
} else if (K->optind + 1 < argc) {
K->optarg = argv[K->optind + 1];
K->optind += 2;
GETOPT_YIELD(K->optopt);
break;
} else {
getopt_err(argc, argv, shortopts, K, "option requires an argument -- %c\n", K->optopt);
K->optind++;
GETOPT_YIELD((*shortopts == ':')? ':' : '?');
break;
}
}
}
GETOPT_LEAVE;
return -1;
}
Here's macro magic, the complexity of which is more than made up for by the readability of the core code: #define GETOPT_ENTER \
do { \
static const int pc0 = __LINE__; \
switch (pc0 + K->pc) { \
case __LINE__: (void)0
#define GETOPT_SAVE_AND_DO(do_statement) \
do { \
K->pc = __LINE__ - pc0; \
do_statement; \
case __LINE__: (void)0; \
} while (0)
#define GETOPT_YIELD(rv) \
GETOPT_SAVE_AND_DO(return (rv))
#define GETOPT_LEAVE \
GETOPT_SAVE_AND_DO(break); \
} \
} while (0)CoroutineTS uses coroutines that are implemented on the LLVM-IR level, as a result they get optimized away in many cases.
Unfortunately the CoroutineTS has also some design flaws around allocations and ownership
There are implementations that use the GCC goto and label pointer, which then avoid the need for a switch statement.
In lining the answer.
> Protothreads is a library implementation of this trick. In fact, the protothreads creator even references the linked site in their explanation of protothreads (very last paragraph)
if (c == 0xFF) {
len = getchar();
c = getchar();
while (len--)
emit(c);
}The code snippet itself is run length encoding with 0xff as an escape.
If you add {max-width: 60em; margin: auto;} to the body tag it looks much better.
Although I love using a tiling window manager now, I kinda miss my old System 8 Macintosh setup, with Netscape open in one window & Think C (or was it CodeWarrior?) open in another.
I haven't found something similar for Windows, but I'm sure there is one.
Not on 27" with browser in full screen.
CMD and + to zoom in on Mac
or CTRL/CMD and scroll wheel to zoom in/out depending on direction scrolled.
Or are you using an ancient browser without zoom?
def function():
for i in range(10):
yield i