buf->modified = false;
buf->expand_tabs = ExpandTabs;
buf->copy_indent = CopyIndent;
- buf->charset = UTF_8;
+ buf->charset = BINARY;
buf->crlf = 0;
buf->bufsize = 8192;
buf->bufstart = (Rune*)malloc(buf->bufsize * sizeof(Rune));
/* load the file and determine the character set */
FMap file = mmap_readonly(buf->path);
- filetype(buf, file);
-
- /* read the file contents into the buffer */
buf_resize(buf, next_size(file.len));
- for (size_t i = 0; i < file.len;) {
- Rune r;
- if (buf->charset == BINARY) {
- r = file.buf[i++];
- } else {
- size_t len = 0;
- while (!utf8decode(&r, &len, file.buf[i++]));
- }
- buf_insert(buf, false, buf_end(buf), r);
- }
+ for (size_t i = 0; i < file.len;)
+ buf_insert(buf, false, buf_end(buf), file.buf[i++]);
/* jump to address if we got one */
if (addr)
/* text files should always end in a new line. If we detected a
binary file or at least a non-utf8 file, skip this part. */
- if (!buf_iseol(buf, buf_end(buf)-1) && (buf->charset != BINARY))
+ if (!buf_iseol(buf, buf_end(buf)-1))
buf_insert(buf, false, buf_end(buf), '\n');
size_t wrlen = 0;
+++ /dev/null
-#include <stdc.h>
-#include <utf.h>
-#include <edit.h>
-
-static const char Utf8Valid[256] = {
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
-};
-
-void filetype(Buf* buf, FMap file) {
- size_t crs = 0, lfs = 0, tabs = 0;
- /* look for bytes that are invalid in utf-8 and count tabs, carriage
- returns, and line feeds */
- int type = buf->charset;
- for (size_t i = 0; i < file.len; i++) {
- if (type == UTF_8)
- type &= Utf8Valid[(int)file.buf[i]];
- switch(file.buf[i]) {
- case '\r': crs++; break;
- case '\n': lfs++; break;
- case '\t': tabs++; break;
- }
- }
-
- /* setup filetype attributes in the buffer */
- if (crs || lfs)
- buf->crlf = (crs == lfs);
- buf->charset = type;
- buf->expand_tabs = (tabs == 0);
-}