Catch BOM at the beginning of file.

This commit is contained in:
Alek Mosingiewicz 2018-05-13 12:24:34 +02:00
parent efbebee9da
commit a024db040d
2 changed files with 23 additions and 3 deletions

View File

@ -52,6 +52,7 @@
#include "../dispatchkit/exception_specification.hpp"
#include "chaiscript_parser.hpp"
namespace chaiscript
{
@ -204,6 +205,23 @@ namespace chaiscript
m_engine.add(fun([this](const std::string& t_namespace_name) { import(t_namespace_name); }), "import");
}
/// Skip BOM at the beginning of file
static bool skip_bom(std::ifstream &infile) {
char buffer[4];
memset(buffer, '\0', 4);
infile.readsome(buffer, 3);
std::string buffer_string(buffer);
if (chaiscript::parser::detail::Char_Parser_Helper<std::string>::has_utf8_bom(buffer_string)) {
infile.seekg(3);
return true;
}
infile.seekg(0);
return false;
}
/// Helper function for loading a file
static std::string load_file(const std::string &t_filename) {
@ -218,6 +236,8 @@ namespace chaiscript
assert(size >= 0);
skip_bom(infile);
if (size == std::streampos(0))
{
return std::string();

View File

@ -114,11 +114,11 @@ namespace chaiscript
// little SFINAE trick to avoid base class
return Char_Parser_Helper<std::true_type>::u8str_from_ll(val);
}
static bool has_utf8_bom(const std::string &t_input)
{
//skip UTF-8 BOM
return ((t_input.size() > 3) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf'));
return ((t_input.size() > 2) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf'));
}
};
}
@ -2568,7 +2568,7 @@ namespace chaiscript
AST_NodePtr parse_internal(const std::string &t_input, std::string t_fname) {
m_position = Position(t_input.begin(), t_input.end());
m_filename = std::make_shared<std::string>(std::move(t_fname));
if (detail::Char_Parser_Helper<std::string>::has_utf8_bom(t_input)) {
throw exception::eval_error("UTF-8 in user provided input!");
}