Change eolformat and BOM options to be enums instead of strings

This commit is contained in:
Maxime Coste 2015-12-06 12:51:55 +00:00
parent 39fffec104
commit 966ac90fe7
4 changed files with 47 additions and 15 deletions

View File

@ -17,43 +17,47 @@
namespace Kakoune namespace Kakoune
{ {
struct ParsedLines { BufferLines lines; bool bom, crlf; }; struct ParsedLines
{
BufferLines lines;
ByteOrderMark bom = ByteOrderMark::None;
EolFormat eolformat = EolFormat::Lf;
};
static ParsedLines parse_lines(StringView data) static ParsedLines parse_lines(StringView data)
{ {
bool bom = false, crlf = false; ParsedLines res;
const char* pos = data.begin(); const char* pos = data.begin();
if (data.substr(0, 3_byte) == "\xEF\xBB\xBF") if (data.substr(0, 3_byte) == "\xEF\xBB\xBF")
{ {
bom = true; res.bom = ByteOrderMark::Utf8;
pos = data.begin() + 3; pos = data.begin() + 3;
} }
BufferLines lines;
while (pos < data.end()) while (pos < data.end())
{ {
const char* line_end = pos; const char* line_end = pos;
while (line_end < data.end() and *line_end != '\r' and *line_end != '\n') while (line_end < data.end() and *line_end != '\r' and *line_end != '\n')
++line_end; ++line_end;
lines.emplace_back(StringData::create({pos, line_end}, '\n')); res.lines.emplace_back(StringData::create({pos, line_end}, '\n'));
if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n') if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n')
{ {
crlf = true; res.eolformat = EolFormat::Crlf;
pos = line_end + 2; pos = line_end + 2;
} }
else else
pos = line_end + 1; pos = line_end + 1;
} }
return { std::move(lines), bom, crlf }; return res;
} }
static void apply_options(OptionManager& options, const ParsedLines& parsed_lines) static void apply_options(OptionManager& options, const ParsedLines& parsed_lines)
{ {
options.get_local_option("eolformat").set<String>(parsed_lines.crlf ? "crlf" : "lf"); options.get_local_option("eolformat").set(parsed_lines.eolformat);
options.get_local_option("BOM").set<String>(parsed_lines.bom ? "utf-8" : "no"); options.get_local_option("BOM").set(parsed_lines.bom);
} }
Buffer::Buffer(String name, Flags flags, StringView data, Buffer::Buffer(String name, Flags flags, StringView data,

View File

@ -12,6 +12,34 @@
namespace Kakoune namespace Kakoune
{ {
enum class EolFormat
{
Lf,
Crlf
};
constexpr Array<EnumDesc<EolFormat>, 2> enum_desc(EolFormat)
{
return { {
{ EolFormat::Lf, "lf" },
{ EolFormat::Crlf, "crlf" },
} };
}
enum class ByteOrderMark
{
None,
Utf8
};
constexpr Array<EnumDesc<ByteOrderMark>, 2> enum_desc(ByteOrderMark)
{
return { {
{ ByteOrderMark::None, "none" },
{ ByteOrderMark::Utf8, "utf8" },
} };
}
class Buffer; class Buffer;
constexpr timespec InvalidTime = { -1, -1 }; constexpr timespec InvalidTime = { -1, -1 };

View File

@ -215,14 +215,14 @@ void write(int fd, StringView data)
void write_buffer_to_fd(Buffer& buffer, int fd) void write_buffer_to_fd(Buffer& buffer, int fd)
{ {
const String& eolformat = buffer.options()["eolformat"].get<String>(); auto eolformat = buffer.options()["eolformat"].get<EolFormat>();
StringView eoldata; StringView eoldata;
if (eolformat == "crlf") if (eolformat == EolFormat::Crlf)
eoldata = "\r\n"; eoldata = "\r\n";
else else
eoldata = "\n"; eoldata = "\n";
if (buffer.options()["BOM"].get<String>() == "utf-8") if (buffer.options()["BOM"].get<ByteOrderMark>() == ByteOrderMark::Utf8)
::write(fd, "\xEF\xBB\xBF", 3); ::write(fd, "\xEF\xBB\xBF", 3);
for (LineCount i = 0; i < buffer.line_count(); ++i) for (LineCount i = 0; i < buffer.line_count(); ++i)

View File

@ -188,9 +188,9 @@ void register_options()
reg.declare_option("scrolloff", reg.declare_option("scrolloff",
"number of lines and columns to keep visible main cursor when scrolling", "number of lines and columns to keep visible main cursor when scrolling",
CharCoord{0,0}); CharCoord{0,0});
reg.declare_option("eolformat", "end of line format: 'crlf' or 'lf'", "lf"_str); reg.declare_option("eolformat", "end of line format: crlf or lf", EolFormat::Lf);
reg.declare_option("BOM", "insert a byte order mark when writing buffer", reg.declare_option("BOM", "insert a byte order mark when writing buffer (none or utf8)",
"no"_str); ByteOrderMark::None);
reg.declare_option("complete_prefix", reg.declare_option("complete_prefix",
"complete up to common prefix in tab completion", "complete up to common prefix in tab completion",
true); true);