Skip to content

Commit 1ae3e95

Browse files
committed
Move std::string -> String from value type
Now starting to change over to the new memory allocation, care has been taken to leave exceptions in the original allocator
1 parent 027ee3d commit 1ae3e95

File tree

8 files changed

+169
-152
lines changed

8 files changed

+169
-152
lines changed

include/json/reader.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace detail {
3434
template<class _Value>
3535
class JSON_API Reader {
3636
public:
37+
typedef typename _Value::String String;
3738
typedef char Char;
3839
typedef const Char* Location;
3940

@@ -74,7 +75,7 @@ class JSON_API Reader {
7475
* error occurred.
7576
*/
7677
bool
77-
parse(const std::string& document, _Value& root, bool collectComments = true);
78+
parse(const String& document, _Value& root, bool collectComments = true);
7879

7980
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
8081
document.
@@ -205,7 +206,7 @@ class JSON_API Reader {
205206
bool decodeNumber(Token& token);
206207
bool decodeNumber(Token& token, _Value& decoded);
207208
bool decodeString(Token& token);
208-
bool decodeString(Token& token, std::string& decoded);
209+
bool decodeString(Token& token, String& decoded);
209210
bool decodeDouble(Token& token);
210211
bool decodeDouble(Token& token, _Value& decoded);
211212
bool decodeUnicodeCodePoint(Token& token,
@@ -226,20 +227,20 @@ class JSON_API Reader {
226227
Char getNextChar();
227228
void
228229
getLocationLineAndColumn(Location location, int& line, int& column) const;
229-
std::string getLocationLineAndColumn(Location location) const;
230+
String getLocationLineAndColumn(Location location) const;
230231
void addComment(Location begin, Location end, CommentPlacement placement);
231232
void skipCommentTokens(Token& token);
232233

233234
typedef std::stack<_Value*> Nodes;
234235
Nodes nodes_;
235236
Errors errors_;
236-
std::string document_;
237+
String document_;
237238
Location begin_;
238239
Location end_;
239240
Location current_;
240241
Location lastValueEnd_;
241242
_Value* lastValue_;
242-
std::string commentsBefore_;
243+
String commentsBefore_;
243244
Features features_;
244245
bool collectComments_;
245246
}; // Reader
@@ -296,6 +297,7 @@ class JSON_API CharReader {
296297
template<class _Value>
297298
class JSON_API CharReaderBuilder : public CharReader<_Value>::Factory {
298299
public:
300+
typedef typename _Value::String String;
299301
// Note: We use a Json::Value so that we can add data-members to this class
300302
// without a major version bump.
301303
/** Configuration of this builder.
@@ -348,7 +350,7 @@ class JSON_API CharReaderBuilder : public CharReader<_Value>::Factory {
348350

349351
/** A simple way to update a specific setting.
350352
*/
351-
_Value& operator[](std::string key);
353+
_Value& operator[](String key);
352354

353355
/** Called by ctor, but you can use this to reset settings_.
354356
* \pre 'settings' != NULL (but Json::null is fine)

include/json/reader.inl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Reader<_Value>::Reader(const Features& features)
7676

7777
template<class _Value>
7878
bool
79-
Reader<_Value>::parse(const std::string& document, _Value& root, bool collectComments) {
79+
Reader<_Value>::parse(const String& document, _Value& root, bool collectComments) {
8080
document_ = document;
8181
const char* begin = document_.c_str();
8282
const char* end = begin + document_.length();
@@ -92,7 +92,7 @@ bool Reader<_Value>::parse(std::istream& sin, _Value& root, bool collectComments
9292

9393
// Since std::string is reference-counted, this at least does not
9494
// create an extra copy.
95-
std::string doc;
95+
String doc;
9696
std::getline(sin, doc, (char)EOF);
9797
return parse(doc, root, collectComments);
9898
}
@@ -356,8 +356,8 @@ bool Reader<_Value>::readComment() {
356356
}
357357

358358
template<class _Value>
359-
static std::string normalizeEOL(typename Reader<_Value>::Location begin, typename Reader<_Value>::Location end) {
360-
std::string normalized;
359+
static typename _Value::String normalizeEOL(typename Reader<_Value>::Location begin, typename Reader<_Value>::Location end) {
360+
typename _Value::String normalized;
361361
normalized.reserve(end - begin);
362362
typename Reader<_Value>::Location current = begin;
363363
while (current != end) {
@@ -379,7 +379,7 @@ template<class _Value>
379379
void
380380
Reader<_Value>::addComment(Location begin, Location end, CommentPlacement placement) {
381381
assert(collectComments_);
382-
const std::string& normalized = normalizeEOL<_Value>(begin, end);
382+
const String& normalized = normalizeEOL<_Value>(begin, end);
383383
if (placement == commentAfterOnSameLine) {
384384
assert(lastValue_ != 0);
385385
lastValue_->setComment(normalized, placement);
@@ -454,7 +454,7 @@ bool Reader<_Value>::readString() {
454454
template<class _Value>
455455
bool Reader<_Value>::readObject(Token& tokenStart) {
456456
Token tokenName;
457-
std::string name;
457+
String name;
458458
_Value init(objectValue);
459459
currentValue().swapPayload(init);
460460
currentValue().setOffsetStart(tokenStart.start_ - begin_);
@@ -615,10 +615,10 @@ bool Reader<_Value>::decodeDouble(Token& token) {
615615
template<class _Value>
616616
bool Reader<_Value>::decodeDouble(Token& token, _Value& decoded) {
617617
double value = 0;
618-
std::string buffer(token.start_, token.end_);
618+
String buffer(token.start_, token.end_);
619619
std::istringstream is(buffer);
620620
if (!(is >> value))
621-
return addError("'" + std::string(token.start_, token.end_) +
621+
return addError("'" + String(token.start_, token.end_) +
622622
"' is not a number.",
623623
token);
624624
decoded = value;
@@ -627,7 +627,7 @@ bool Reader<_Value>::decodeDouble(Token& token, _Value& decoded) {
627627

628628
template<class _Value>
629629
bool Reader<_Value>::decodeString(Token& token) {
630-
std::string decoded_string;
630+
String decoded_string;
631631
if (!decodeString(token, decoded_string))
632632
return false;
633633
_Value decoded(decoded_string);
@@ -638,7 +638,7 @@ bool Reader<_Value>::decodeString(Token& token) {
638638
}
639639

640640
template<class _Value>
641-
bool Reader<_Value>::decodeString(Token& token, std::string& decoded) {
641+
bool Reader<_Value>::decodeString(Token& token, String& decoded) {
642642
decoded.reserve(token.end_ - token.start_ - 2);
643643
Location current = token.start_ + 1; // skip '"'
644644
Location end = token.end_ - 1; // do not include '"'
@@ -679,7 +679,7 @@ bool Reader<_Value>::decodeString(Token& token, std::string& decoded) {
679679
unsigned int unicode;
680680
if (!decodeUnicodeCodePoint(token, current, end, unicode))
681681
return false;
682-
decoded += codePointToUTF8(unicode);
682+
decoded += codePointToUTF8<String>(unicode);
683683
} break;
684684
default:
685685
return addError("Bad escape sequence in string", token, current);
@@ -818,7 +818,7 @@ void Reader<_Value>::getLocationLineAndColumn(Location location,
818818
}
819819

820820
template<class _Value>
821-
std::string Reader<_Value>::getLocationLineAndColumn(Location location) const {
821+
typename _Value::String Reader<_Value>::getLocationLineAndColumn(Location location) const {
822822
int line, column;
823823
getLocationLineAndColumn(location, line, column);
824824
char buffer[18 + 16 + 16 + 1];
@@ -929,6 +929,7 @@ public:
929929
template<class _Value>
930930
class OurReader {
931931
public:
932+
typedef typename _Value::String String;
932933
typedef char Char;
933934
typedef const Char* Location;
934935
struct StructuredError {
@@ -1716,7 +1717,7 @@ bool OurReader<_Value>::decodeString(Token& token, std::string& decoded) {
17161717
unsigned int unicode;
17171718
if (!decodeUnicodeCodePoint(token, current, end, unicode))
17181719
return false;
1719-
decoded += codePointToUTF8(unicode);
1720+
decoded += codePointToUTF8<String>(unicode);
17201721
} break;
17211722
default:
17221723
return addError("Bad escape sequence in string", token, current);
@@ -2019,7 +2020,7 @@ bool CharReaderBuilder<_Value>::validate(_Value* invalid) const
20192020
return 0u == inv.size();
20202021
}
20212022
template<class _Value>
2022-
_Value& CharReaderBuilder<_Value>::operator[](std::string key)
2023+
_Value& CharReaderBuilder<_Value>::operator[](String key)
20232024
{
20242025
return settings_[key];
20252026
}

include/json/tool.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
namespace Json {
1616

1717
/// Converts a unicode code-point to UTF-8.
18-
static inline std::string codePointToUTF8(unsigned int cp) {
19-
std::string result;
18+
template<class _String>
19+
static inline _String codePointToUTF8(unsigned int cp) {
20+
_String result;
2021

2122
// based on description from http://en.wikipedia.org/wiki/UTF-8
2223

include/json/value.h

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ class JSON_API Value {
167167
template<typename T>
168168
friend class ValueIteratorBase;
169169
public:
170+
typedef _String String;
171+
typedef _Alloc Allocator;
170172
typedef std::vector<std::string> Members;
171173
typedef ValueIterator<Value> iterator;
172174
typedef ValueConstIterator<Value> const_iterator;
@@ -295,7 +297,7 @@ Json::Value obj_value(Json::objectValue); // {}
295297
* \endcode
296298
*/
297299
Value(const Json::StaticString& value);
298-
Value(const std::string& value); ///< Copy data() til size(). Embedded zeroes too.
300+
Value(const String& value); ///< Copy data() til size(). Embedded zeroes too.
299301
#ifdef JSON_USE_CPPTL
300302
Value(const CppTL::ConstString& value);
301303
#endif
@@ -328,7 +330,7 @@ Json::Value obj_value(Json::objectValue); // {}
328330
int compare(const Value& other) const;
329331

330332
const char* asCString() const; ///< Embedded zeroes could cause you trouble!
331-
std::string asString() const; ///< Embedded zeroes are possible.
333+
String asString() const; ///< Embedded zeroes are possible.
332334
/** Get raw char* of string-value.
333335
* \return false if !string. (Seg-fault if str or end are NULL.)
334336
*/
@@ -432,11 +434,11 @@ Json::Value obj_value(Json::objectValue); // {}
432434
const Value& operator[](const char* key) const;
433435
/// Access an object value by name, create a null member if it does not exist.
434436
/// \param key may contain embedded nulls.
435-
Value& operator[](const std::string& key);
437+
Value& operator[](const String& key);
436438
/// Access an object value by name, returns null if there is no member with
437439
/// that name.
438440
/// \param key may contain embedded nulls.
439-
const Value& operator[](const std::string& key) const;
441+
const Value& operator[](const String& key) const;
440442
/** \brief Access an object value by name, create a null member if it does not
441443
exist.
442444
@@ -467,7 +469,7 @@ Json::Value obj_value(Json::objectValue); // {}
467469
/// Return the member named key if it exist, defaultValue otherwise.
468470
/// \note deep copy
469471
/// \param key may contain embedded nulls.
470-
Value get(const std::string& key, const Value& defaultValue) const;
472+
Value get(const String& key, const Value& defaultValue) const;
471473
#ifdef JSON_USE_CPPTL
472474
/// Return the member named key if it exist, defaultValue otherwise.
473475
/// \note deep copy
@@ -492,7 +494,7 @@ Json::Value obj_value(Json::objectValue); // {}
492494
/// Same as removeMember(const char*)
493495
/// \param key may contain embedded nulls.
494496
/// \deprecated
495-
Value removeMember(const std::string& key);
497+
Value removeMember(const String& key);
496498
/// Same as removeMember(const char* begin, const char* end, Value* removed),
497499
/// but 'key' is null-terminated.
498500
bool removeMember(const char* key, Value* removed);
@@ -502,8 +504,8 @@ Json::Value obj_value(Json::objectValue); // {}
502504
\param key may contain embedded nulls.
503505
\return true iff removed (no exceptions)
504506
*/
505-
bool removeMember(std::string const& key, Value* removed);
506-
/// Same as removeMember(std::string const& key, Value* removed)
507+
bool removeMember(String const& key, Value* removed);
508+
/// Same as removeMember(String const& key, Value* removed)
507509
bool removeMember(const char* begin, const char* end, Value* removed);
508510
/** \brief Remove the indexed array element.
509511
@@ -518,8 +520,8 @@ Json::Value obj_value(Json::objectValue); // {}
518520
bool isMember(const char* key) const;
519521
/// Return true if the object has a member named key.
520522
/// \param key may contain embedded nulls.
521-
bool isMember(const std::string& key) const;
522-
/// Same as isMember(std::string const& key)const
523+
bool isMember(const String& key) const;
524+
/// Same as isMember(String const& key)const
523525
bool isMember(const char* begin, const char* end) const;
524526
#ifdef JSON_USE_CPPTL
525527
/// Return true if the object has a member named key.
@@ -539,17 +541,17 @@ Json::Value obj_value(Json::objectValue); // {}
539541
//# endif
540542

541543
/// \deprecated Always pass len.
542-
JSONCPP_DEPRECATED("Use setComment(std::string const&) instead.")
544+
JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
543545
void setComment(const char* comment, CommentPlacement placement);
544546
/// Comments must be //... or /* ... */
545547
void setComment(const char* comment, size_t len, CommentPlacement placement);
546548
/// Comments must be //... or /* ... */
547-
void setComment(const std::string& comment, CommentPlacement placement);
549+
void setComment(const String& comment, CommentPlacement placement);
548550
bool hasComment(CommentPlacement placement) const;
549551
/// Include delimiters and embedded newlines.
550-
std::string getComment(CommentPlacement placement) const;
552+
String getComment(CommentPlacement placement) const;
551553

552-
std::string toStyledString() const;
554+
String toStyledString() const;
553555

554556
const_iterator begin() const;
555557
const_iterator end() const;
@@ -613,21 +615,22 @@ Json::Value obj_value(Json::objectValue); // {}
613615
template<class _Value>
614616
class JSON_API PathArgument {
615617
public:
618+
typedef typename _Value::String String;
616619
template<typename T>
617620
friend class Path;
618621

619622
PathArgument();
620623
PathArgument(ArrayIndex index);
621624
PathArgument(const char* key);
622-
PathArgument(const std::string& key);
625+
PathArgument(const String& key);
623626

624627
private:
625628
enum Kind {
626629
kindNone = 0,
627630
kindIndex,
628631
kindKey
629632
};
630-
std::string key_;
633+
String key_;
631634
ArrayIndex index_;
632635
Kind kind_;
633636
};
@@ -646,7 +649,8 @@ class JSON_API PathArgument {
646649
template<class _Value>
647650
class JSON_API Path {
648651
public:
649-
Path(const std::string& path,
652+
typedef typename _Value::String String;
653+
Path(const String& path,
650654
const PathArgument<_Value>& a1 = PathArgument<_Value>(),
651655
const PathArgument<_Value>& a2 = PathArgument<_Value>(),
652656
const PathArgument<_Value>& a3 = PathArgument<_Value>(),
@@ -663,12 +667,12 @@ class JSON_API Path {
663667
typedef std::vector<const PathArgument<_Value>*> InArgs;
664668
typedef std::vector<PathArgument<_Value>> Args;
665669

666-
void makePath(const std::string& path, const InArgs& in);
667-
void addPathInArg(const std::string& path,
670+
void makePath(const String& path, const InArgs& in);
671+
void addPathInArg(const String& path,
668672
const InArgs& in,
669673
typename InArgs::const_iterator& itInArg,
670674
typename PathArgument<_Value>::Kind kind);
671-
void invalidPath(const std::string& path, int location);
675+
void invalidPath(const String& path, int location);
672676

673677
Args args_;
674678
};
@@ -679,6 +683,7 @@ class JSON_API Path {
679683
template<class _Value>
680684
class JSON_API ValueIteratorBase {
681685
public:
686+
typedef typename _Value::String String;
682687
typedef std::bidirectional_iterator_tag iterator_category;
683688
typedef unsigned int size_t;
684689
typedef int difference_type;
@@ -702,7 +707,7 @@ class JSON_API ValueIteratorBase {
702707
/// Return the member name of the referenced Value, or "" if it is not an
703708
/// objectValue.
704709
/// \note Avoid `c_str()` on result, as embedded zeroes are possible.
705-
std::string name() const;
710+
String name() const;
706711

707712
/// Return the member name of the referenced Value. "" if it is not an
708713
/// objectValue.

0 commit comments

Comments
 (0)