Run-length encoding/C: Difference between revisions

m
→‎Unbuffered - Generative: minor reformat of code and comment
(→‎[[Run-length encoding/C#Unbuffered - generative: The following code sample is experimental as it implements python style iterators.)
m (→‎Unbuffered - Generative: minor reformat of code and comment)
Line 29:
#define GOTO(label) longjmp(label, TRUE)
#endif
 
/* the following line is the only time I have ever required "auto" */
#define FOR(i,iterator) auto BOOL lambda(i); yield_init = (void *)λ iterator; BOOL lambda(i)
Line 37:
#define CONTINUE return TRUE
#define OD CONTINUE; }
/* Warning: _Most_ FOR(,){ } loops _must_ have a CONTINUE, as the last statement. BREAK
* or OD as the terminating statement. Otherwise the lambda will
* Otherwise the lambda will return random value from stack, and may terminate early */
 
typedef BOOL ITERATOR;
static volatile void *yield_init; /* not thread safe */
#define YIELDS(type) BOOL (*yield)(type) = yield_init
 
ITERATOR gen_char_seq (char *s){
YIELDS(char);
fflush(stdout);
int upb_s = strlen(s);
int i; for(i = 0; i <= upb_s; i++) YIELD(s[i]);
}
 
Line 67 ⟶ 68:
count = 1;
YIELD(prev); prev = c;
} else {
count += 1;
}
OD;
Line 86 ⟶ 87:
if(strchr(zero2nine, c)){
repeat = repeat*10 + c - '0';
} else {
int i; for(i = 1; i <= repeat; i++ ){ YIELD(c); }
repeat = 0;
}
Line 100 ⟶ 101:
OD;
printf("\n");
 
/* iterate through output string */
printf("Decode output: ");
Line 113 ⟶ 114:
Decode output: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
</pre>
 
=== Buffered ===
These functions have no check for the size of the output buffers.