further optimizations
parent
e7eacc893f
commit
163235bf0c
|
@ -1,7 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
#define LNBUFF_LEN 10
|
#include <assert.h>
|
||||||
|
|
||||||
int grep(char pattern[]);
|
int grep(char pattern[]);
|
||||||
|
|
||||||
|
@ -67,54 +67,52 @@ void println()
|
||||||
|
|
||||||
/* ------------------------ line buffer ---------------------- */
|
/* ------------------------ line buffer ---------------------- */
|
||||||
|
|
||||||
|
#define LNBUFF_LEN 8
|
||||||
|
|
||||||
|
/* the line buffer lenght must be a power of two */
|
||||||
|
static_assert(!(LNBUFF_LEN % 2), "Line buffer length is not a power of two!");
|
||||||
|
|
||||||
/* line_buffer: global overflowing line buffer */
|
/* line_buffer: global overflowing line buffer */
|
||||||
struct {
|
struct {
|
||||||
char self[LNBUFF_LEN];
|
char self[LNBUFF_LEN];
|
||||||
unsigned i;
|
unsigned len;
|
||||||
unsigned trunc;
|
} lnbuff = {.len= 0};
|
||||||
} lnbuff = {.i = 0, .trunc = 0};
|
|
||||||
|
|
||||||
/* reset_lnbuff: reset buffer */
|
/* LNBUFF_I: current index in line buffer */
|
||||||
#define reset_lnbuff() lnbuff.i = lnbuff.trunc = 0
|
#define LNBUFF_I (lnbuff.len & (LNBUFF_LEN - 1))
|
||||||
|
|
||||||
/* getch_lnbuff: getchar and add it to buffer */
|
/* getch_lnbuff: getchar and add it to buffer */
|
||||||
int getch_lnbuff()
|
int getch_lnbuff()
|
||||||
{
|
{
|
||||||
int c = getchar();
|
int c = getchar();
|
||||||
|
|
||||||
if (c == EOF || c == '\n') { // new line
|
if (c == EOF || c == '\n') // new line
|
||||||
reset_lnbuff();
|
lnbuff.len = 0;
|
||||||
return c;
|
else {
|
||||||
|
lnbuff.self[LNBUFF_I] = c;
|
||||||
|
lnbuff.len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lnbuff.trunc)
|
|
||||||
lnbuff.trunc++;
|
|
||||||
|
|
||||||
if (lnbuff.i == LNBUFF_LEN) {
|
|
||||||
lnbuff.i = 0;
|
|
||||||
if (!lnbuff.trunc)
|
|
||||||
lnbuff.trunc = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
lnbuff.self[lnbuff.i++] = c;
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* flush_lnbuff: print contents of line buffer, return length */
|
/* flush_lnbuff: print contents of line buffer, return length */
|
||||||
int flush_lnbuff()
|
int flush_lnbuff()
|
||||||
{
|
{
|
||||||
int i, len = lnbuff.i;
|
int i;
|
||||||
|
|
||||||
if (lnbuff.trunc) {
|
/* handle overflowing chars */
|
||||||
len = LNBUFF_LEN + lnbuff.trunc;
|
if (lnbuff.len > LNBUFF_LEN) {
|
||||||
for (i = 0; i < lnbuff.trunc; i++)
|
for (i = LNBUFF_LEN; i < lnbuff.len; i++)
|
||||||
putchar('.');
|
putchar('.');
|
||||||
for (i = lnbuff.i; i < LNBUFF_LEN; i++)
|
for (i = LNBUFF_I; i < LNBUFF_LEN; i++)
|
||||||
putchar(lnbuff.self[i]);
|
putchar(lnbuff.self[i]);
|
||||||
}
|
}
|
||||||
for (i = 0; i < lnbuff.i; i++)
|
/* handle rest of chars */
|
||||||
|
for (i = 0; i < LNBUFF_I; i++)
|
||||||
putchar(lnbuff.self[i]);
|
putchar(lnbuff.self[i]);
|
||||||
reset_lnbuff();
|
|
||||||
return len;
|
i = lnbuff.len;
|
||||||
|
lnbuff.len = 0;
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue