mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-07 17:26:55 +08:00
Catch BOM at the beginning of file.
This commit is contained in:
parent
efbebee9da
commit
a024db040d
@ -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();
|
||||
|
||||
@ -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!");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user