@@ -24,91 +24,39 @@ const Int Value::minInt = Int( ~(UInt(-1)/2) );
2424const Int Value::maxInt = Int( UInt(-1 )/2 );
2525const UInt Value::maxUInt = UInt(-1 );
2626
27- // A "safe" implementation of strdup. Allow null pointer to be passed.
28- // Also avoid warning on msvc80.
29- //
30- // inline char *safeStringDup( const char *czstring )
31- // {
32- // if ( czstring )
33- // {
34- // const size_t length = (unsigned int)( strlen(czstring) + 1 );
35- // char *newString = static_cast<char *>( malloc( length ) );
36- // memcpy( newString, czstring, length );
37- // return newString;
38- // }
39- // return 0;
40- // }
41- //
42- // inline char *safeStringDup( const std::string &str )
43- // {
44- // if ( !str.empty() )
45- // {
46- // const size_t length = str.length();
47- // char *newString = static_cast<char *>( malloc( length + 1 ) );
48- // memcpy( newString, str.c_str(), length );
49- // newString[length] = 0;
50- // return newString;
51- // }
52- // return 0;
53- // }
27+ // / Unknown size marker
28+ enum { unknown = (unsigned )-1 };
5429
55- ValueAllocator::~ValueAllocator ()
56- {
57- }
5830
59- class DefaultValueAllocator : public ValueAllocator
31+ /* * Duplicates the specified string value.
32+ * @param value Pointer to the string to duplicate. Must be zero-terminated if
33+ * length is "unknown".
34+ * @param length Length of the value. if equals to unknown, then it will be
35+ * computed using strlen(value).
36+ * @return Pointer on the duplicate instance of string.
37+ */
38+ static inline char *
39+ duplicateStringValue ( const char *value,
40+ unsigned int length = unknown )
6041{
61- public:
62- virtual ~DefaultValueAllocator ()
63- {
64- }
65-
66- virtual char *makeMemberName ( const char *memberName )
67- {
68- return duplicateStringValue ( memberName );
69- }
70-
71- virtual void releaseMemberName ( char *memberName )
72- {
73- releaseStringValue ( memberName );
74- }
75-
76- virtual char *duplicateStringValue ( const char *value,
77- unsigned int length = unknown )
78- {
79- // @todo invesgate this old optimization
80- // if ( !value || value[0] == 0 )
81- // return 0;
82-
83- if ( length == unknown )
84- length = (unsigned int )strlen (value);
85- char *newString = static_cast <char *>( malloc ( length + 1 ) );
86- memcpy ( newString, value, length );
87- newString[length] = 0 ;
88- return newString;
89- }
42+ if ( length == unknown )
43+ length = (unsigned int )strlen (value);
44+ char *newString = static_cast <char *>( malloc ( length + 1 ) );
45+ memcpy ( newString, value, length );
46+ newString[length] = 0 ;
47+ return newString;
48+ }
9049
91- virtual void releaseStringValue ( char *value )
92- {
93- if ( value )
94- free ( value );
95- }
96- };
9750
98- static ValueAllocator *&valueAllocator ()
51+ /* * Free the string duplicated by duplicateStringValue().
52+ */
53+ static inline void
54+ releaseStringValue ( char *value )
9955{
100- static DefaultValueAllocator defaultAllocator;
101- static ValueAllocator *valueAllocator = &defaultAllocator;
102- return valueAllocator;
56+ if ( value )
57+ free ( value );
10358}
10459
105- static struct DummyValueAllocatorInitializer {
106- DummyValueAllocatorInitializer ()
107- {
108- valueAllocator (); // ensure valueAllocator() statics are initialized before main().
109- }
110- } dummyValueAllocatorInitializer;
111-
11260
11361
11462// //////////////////////////////////////////////////////////////////
@@ -143,19 +91,19 @@ Value::CommentInfo::CommentInfo()
14391Value::CommentInfo::~CommentInfo ()
14492{
14593 if ( comment_ )
146- valueAllocator ()-> releaseStringValue ( comment_ );
94+ releaseStringValue ( comment_ );
14795}
14896
14997
15098void
15199Value::CommentInfo::setComment ( const char *text )
152100{
153101 if ( comment_ )
154- valueAllocator ()-> releaseStringValue ( comment_ );
102+ releaseStringValue ( comment_ );
155103 JSON_ASSERT ( text );
156104 JSON_ASSERT_MESSAGE ( text[0 ]==' \0 ' || text[0 ]==' /' , " Comments must start with /" );
157105 // It seems that /**/ style comments are acceptable as well.
158- comment_ = valueAllocator ()-> duplicateStringValue ( text );
106+ comment_ = duplicateStringValue ( text );
159107}
160108
161109
@@ -178,15 +126,15 @@ Value::CZString::CZString( int index )
178126}
179127
180128Value::CZString::CZString ( const char *cstr, DuplicationPolicy allocate )
181- : cstr_( allocate == duplicate ? valueAllocator()->makeMemberName (cstr)
129+ : cstr_( allocate == duplicate ? duplicateStringValue (cstr)
182130 : cstr )
183131 , index_( allocate )
184132{
185133}
186134
187135Value::CZString::CZString ( const CZString &other )
188136: cstr_( other.index_ != noDuplication && other.cstr_ != 0
189- ? valueAllocator()->makeMemberName ( other.cstr_ )
137+ ? duplicateStringValue ( other.cstr_ )
190138 : other.cstr_ )
191139 , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate)
192140 : other.index_ )
@@ -196,7 +144,7 @@ Value::CZString::CZString( const CZString &other )
196144Value::CZString::~CZString ()
197145{
198146 if ( cstr_ && index_ == duplicate )
199- valueAllocator ()-> releaseMemberName ( const_cast <char *>( cstr_ ) );
147+ releaseStringValue ( const_cast <char *>( cstr_ ) );
200148}
201149
202150void
@@ -348,7 +296,7 @@ Value::Value( const char *value )
348296 , itemIsUsed_( 0 )
349297#endif
350298{
351- value_.string_ = valueAllocator ()-> duplicateStringValue ( value );
299+ value_.string_ = duplicateStringValue ( value );
352300}
353301
354302
@@ -361,8 +309,8 @@ Value::Value( const char *beginValue,
361309 , itemIsUsed_( 0 )
362310#endif
363311{
364- value_.string_ = valueAllocator ()-> duplicateStringValue ( beginValue,
365- UInt (endValue - beginValue) );
312+ value_.string_ = duplicateStringValue ( beginValue,
313+ UInt (endValue - beginValue) );
366314}
367315
368316
@@ -374,8 +322,8 @@ Value::Value( const std::string &value )
374322 , itemIsUsed_( 0 )
375323#endif
376324{
377- value_.string_ = valueAllocator ()-> duplicateStringValue ( value.c_str (),
378- (unsigned int )value.length () );
325+ value_.string_ = duplicateStringValue ( value.c_str (),
326+ (unsigned int )value.length () );
379327
380328}
381329
@@ -400,7 +348,7 @@ Value::Value( const CppTL::ConstString &value )
400348 , itemIsUsed_( 0 )
401349#endif
402350{
403- value_.string_ = valueAllocator ()-> duplicateStringValue ( value, value.length () );
351+ value_.string_ = duplicateStringValue ( value, value.length () );
404352}
405353# endif
406354
@@ -434,7 +382,7 @@ Value::Value( const Value &other )
434382 case stringValue:
435383 if ( other.value_ .string_ )
436384 {
437- value_.string_ = valueAllocator ()-> duplicateStringValue ( other.value_ .string_ );
385+ value_.string_ = duplicateStringValue ( other.value_ .string_ );
438386 allocated_ = true ;
439387 }
440388 else
@@ -481,7 +429,7 @@ Value::~Value()
481429 break ;
482430 case stringValue:
483431 if ( allocated_ )
484- valueAllocator ()-> releaseStringValue ( value_.string_ );
432+ releaseStringValue ( value_.string_ );
485433 break ;
486434#ifndef JSON_VALUE_USE_INTERNAL_MAP
487435 case arrayValue:
0 commit comments