{
buf->insert_mode = true;
unsigned i = 0;
+ Rune r;
FILE* in = (!strcmp(path,"-") ? stdin : fopen(path, "rb"));
- while (EOF != fpeekc(in)) {
- size_t len = 0;
- Rune r = 0;
- while (!utf8decode(&r, &len, fgetc(in)));
+ while (RUNE_EOF != (r = fgetrune(in)))
buf_ins(buf, i++, r);
- }
fclose(in);
buf->insert_mode = false;
}
/* UTF-8 Handling
*****************************************************************************/
enum {
- UTF_MAX = 6u, /* maximum number of bytes that make up a rune */
- RUNE_SELF = 0x80, /* byte values larger than this are *not* ascii */
- RUNE_ERR = 0xFFFD, /* rune value representing an error */
- RUNE_MAX = 0x10FFFF, /* Maximum decodable rune value */
- RUNE_EOF = EOF /* ruen value representing end of file */
+ UTF_MAX = 6u, /* maximum number of bytes that make up a rune */
+ RUNE_SELF = 0x80, /* byte values larger than this are *not* ascii */
+ RUNE_ERR = 0xFFFD, /* rune value representing an error */
+ RUNE_MAX = 0x10FFFF, /* Maximum decodable rune value */
+ RUNE_EOF = UINT32_MAX /* ruen value representing end of file */
};
/* Represents a unicode code point */
size_t utf8encode(char str[UTF_MAX], Rune rune);
bool utf8decode(Rune* rune, size_t* length, int byte);
+Rune fgetrune(FILE* f);
+void fputrune(Rune rune, FILE* f);
/* Input Handling
*****************************************************************************/
const uint8_t UTF8_SeqMask[] = { 0x00u, 0xFFu, 0x1Fu, 0x0Fu, 0x07u, 0x03u, 0x01u, 0x00u };
const uint8_t UTF8_SeqLens[] = { 0x01u, 0x00u, 0x02u, 0x03u, 0x04u, 0x05u, 0x06u, 0x00u };
-bool runevalid(Rune val) {
+static bool runevalid(Rune val) {
return (val <= RUNE_MAX)
&& ((val & 0xFFFEu) != 0xFFFEu)
&& ((val < 0xD800u) || (val > 0xDFFFu))
&& ((val < 0xFDD0u) || (val > 0xFDEFu));
}
-size_t runelen(Rune rune) {
+static size_t runelen(Rune rune) {
if(!runevalid(rune))
return 0;
else if(rune <= 0x7F)
return 4;
}
-uint8_t utfseq(uint8_t byte) {
+static uint8_t utfseq(uint8_t byte) {
for (int i = 1; i < 8; i++)
if ((byte & UTF8_SeqBits[i]) == UTF8_SeqBits[i-1])
return UTF8_SeqLens[i-1];
return ((*length == 0) || (*rune == RUNE_ERR));
}
-size_t utflen(const char* s) {
- size_t len = 0;
- Rune rune = 0;
- while (*s && !utf8decode(&rune, &len, *(s++)))
- len++;
- return len;
-}
-
Rune fgetrune(FILE* f) {
Rune rune = 0;
size_t length = 0;