Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api to parse string with specified length #417

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,37 @@ class GenericDocument : public GenericValue<Encoding, Allocator> {
GenericDocument& Parse(const Ch* str) {
return Parse<kParseDefaultFlags>(str);
}

//! Parse JSON text from a read-only string (with Encoding conversion)
/*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
\tparam SourceEncoding Transcoding from input Encoding
\param str Read-only string to be parsed.
\param len The length of the string.
*/
template <unsigned parseFlags, typename SourceEncoding>
GenericDocument& Parse(const Ch* str, size_t len) {
RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag));
GenericBufferStream<SourceEncoding> s(str, len);
return ParseStream<parseFlags, SourceEncoding>(s);
}

//! Parse JSON text from a read-only string with length
/*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
\param str Read-only string to be parsed.
\param len The length of the string.
*/
template <unsigned parseFlags>
GenericDocument& Parse(const Ch* str, size_t len) {
return Parse<parseFlags, Encoding>(str, len);
}

//! Parse JSON text from a read-only string with length (with \ref kParseDefaultFlags)
/*! \param str Read-only string to be parsed.
\param len The length of the string.
*/
GenericDocument& Parse(const Ch* str, size_t len) {
return Parse<kParseDefaultFlags>(str, len);
}
//!@}

//!@name Handling parse errors
Expand Down
39 changes: 39 additions & 0 deletions include/rapidjson/rapidjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,45 @@ struct StreamTraits<GenericStringStream<Encoding> > {
//! String stream with UTF8 encoding.
typedef GenericStringStream<UTF8<> > StringStream;

///////////////////////////////////////////////////////////////////////////////
// BufferStream

//! Read-only buffer stream.
/*! \note implements Stream concept
*/
template <typename Encoding>
struct GenericBufferStream {
typedef typename Encoding::Ch Ch;

GenericBufferStream(const Ch *src, size_t len) :
src_(src), length_(len), idx_(0) {}

Ch Peek() const { return src_[idx_]; }
Ch Take() {
if (idx_ != length_) return src_[idx_++];
else return '\0';
}
size_t Tell() const { return idx_; }

Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
void Put(Ch) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); }
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }

const Ch* src_; //!< Original buffer pointer.
size_t length_; //!< The length of buffer.
size_t idx_; //!< Current read position.
};

template <typename Encoding>
struct StreamTraits<GenericBufferStream<Encoding> > {
enum { copyOptimization = 1 };
};

//! Buffer stream with UTF8 encoding.
typedef GenericBufferStream<UTF8<> > BufferStream;


///////////////////////////////////////////////////////////////////////////////
// InsituStringStream

Expand Down