mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Make major improvements to stdio
Buffering now has optimal performance, bugs have been fixed, and some missing apis have been introduced. This implementation is also now more production worthy since it's less brittle now in terms of system errors. That's going to help redbean since lua i/o is all based on stdio. See #97
This commit is contained in:
parent
09bcfa23d5
commit
da36e7e256
69 changed files with 1595 additions and 735 deletions
|
@ -17,16 +17,35 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/thompike.h"
|
||||
#include "libc/str/tpdecodecb.internal.h"
|
||||
|
||||
/**
|
||||
* Reads UTF-8 character from stream.
|
||||
*
|
||||
* @return wide character or -1 on EOF or error
|
||||
*/
|
||||
wint_t fgetwc(FILE *f) {
|
||||
wint_t res;
|
||||
res = -1;
|
||||
tpdecodecb(&res, fgetc(f), (void *)fgetc, f);
|
||||
return res;
|
||||
int c, n;
|
||||
wint_t b, x, y;
|
||||
if (f->beg < f->end) {
|
||||
b = f->buf[f->beg++] & 0xff;
|
||||
} else if ((c = fgetc(f)) != -1) {
|
||||
b = c;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
if (b < 0300) return b;
|
||||
n = ThomPikeLen(b);
|
||||
x = ThomPikeByte(b);
|
||||
while (--n) {
|
||||
if ((c = fgetc(f)) == -1) return -1;
|
||||
y = c;
|
||||
if (ThomPikeCont(y)) {
|
||||
x = ThomPikeMerge(x, y);
|
||||
} else {
|
||||
ungetc(y, f);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue