diff --git a/include/yaml-cpp/node/parse.h b/include/yaml-cpp/node/parse.h index 4e4ce6b44..41f769ff2 100644 --- a/include/yaml-cpp/node/parse.h +++ b/include/yaml-cpp/node/parse.h @@ -21,7 +21,7 @@ YAML_CPP_API Node Load(const std::string& input); * * @throws {@link ParserException} if it is malformed. */ -YAML_CPP_API Node Load(const char* input); +YAML_CPP_API Node Load(const char* input, size_t length); /** * Loads the input stream as a single YAML document. diff --git a/include/yaml-cpp/parser.h b/include/yaml-cpp/parser.h index 7e7ab0312..4973f97eb 100644 --- a/include/yaml-cpp/parser.h +++ b/include/yaml-cpp/parser.h @@ -27,7 +27,7 @@ class YAML_CPP_API Parser : private noncopyable { * live as long as the parser. */ explicit Parser(std::istream& in); - explicit Parser(const std::string& in); + explicit Parser(const char* in, size_t length); ~Parser(); @@ -39,7 +39,7 @@ class YAML_CPP_API Parser : private noncopyable { * erased. */ void Load(std::istream& in); - void Load(const std::string& in); + void Load(const char* in, size_t length); /** * Handles the next document by calling events on the {@code eventHandler}. diff --git a/src/parse.cpp b/src/parse.cpp index 4ac9c152e..2e9ded116 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -10,7 +10,11 @@ namespace YAML { Node Load(const std::string& input) { - Parser parser(input); + return Load(input.data(), input.length()); +} + +Node Load(const char* input, size_t length) { + Parser parser(input, length); NodeBuilder builder; if (!parser.HandleNextDocument(builder)) { return Node(); diff --git a/src/parser.cpp b/src/parser.cpp index 0a0f254d6..68428fa98 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -14,7 +14,7 @@ class EventHandler; Parser::Parser() {} Parser::Parser(std::istream& in) { Load(in); } -Parser::Parser(const std::string& in) { Load(in); } +Parser::Parser(const char* in, size_t length) { Load(in, length); } Parser::~Parser() {} @@ -27,8 +27,8 @@ void Parser::Load(std::istream& in) { m_pDirectives.reset(new Directives); } -void Parser::Load(const std::string& in) { - m_pScanner.reset(new Scanner(in)); +void Parser::Load(const char* in, size_t length) { + m_pScanner.reset(new Scanner(in, length)); m_pDirectives.reset(new Directives); } diff --git a/src/scanner.cpp b/src/scanner.cpp index 7ea348d2b..da02f1e0d 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -16,8 +16,8 @@ Scanner::Scanner(std::istream& in) InitTokens(); } -Scanner::Scanner(const std::string& in) - : INPUT(in), +Scanner::Scanner(const char* in, size_t length) + : INPUT(in, length), m_startedStream(false), m_endedStream(false), m_simpleKeyAllowed(false), diff --git a/src/scanner.h b/src/scanner.h index edd120292..85a89d0d5 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -28,7 +28,7 @@ class RegEx; class Scanner { public: explicit Scanner(std::istream &in); - explicit Scanner(const std::string &in); + explicit Scanner(const char* in, size_t length); ~Scanner(); /** Returns true if there are no more tokens to be read. */ diff --git a/src/stream.cpp b/src/stream.cpp index d19253805..05cf9816d 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -244,25 +244,26 @@ Stream::CharacterSet Stream::determineCharachterSet(std::istream& input, int& sk return utf8; } -Stream::Stream(const std::string& input) + +Stream::Stream(const char* input, size_t length) : m_input(nullptr), m_pPrefetched(new unsigned char[YAML_PREFETCH_SIZE]), m_nPrefetchedAvailable(0), m_nPrefetchedUsed(0) { - std::stringstream ss(input.substr(0, 5)); int skip = 0; + std::stringstream ss(input); m_charSet = determineCharachterSet(ss, skip); if (m_charSet == utf8) { // Skip UTF-8 BOM - m_readaheadSize = input.size() - skip; - m_buffer = input.data() + skip; + m_readaheadSize = length - skip; + m_buffer = input + skip; } else { m_readaheadSize = 0; - m_input = new std::stringstream({input.begin() + skip, input.end()}); + m_input = new std::stringstream(input + skip); m_ownInput = true; } diff --git a/src/stream.h b/src/stream.h index 1e36fc5f5..d2d817121 100644 --- a/src/stream.h +++ b/src/stream.h @@ -20,8 +20,9 @@ class Stream : private noncopyable { public: friend class StreamCharSource; - Stream(std::istream& input); - Stream(const std::string& input); + explicit Stream(std::istream& input); + explicit Stream(const std::string& input); + explicit Stream(const char* input, size_t length); ~Stream(); operator bool() const {