Skip to content

Commit

Permalink
Add Load(const char* data, size_t length)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek committed Nov 29, 2018
1 parent 70d9104 commit 7cc8319
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/yaml-cpp/node/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions include/yaml-cpp/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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}.
Expand Down
6 changes: 5 additions & 1 deletion src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
11 changes: 6 additions & 5 deletions src/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7cc8319

Please sign in to comment.