@@ -716,6 +716,8 @@ void convert_append_unicode_code_unit(JSON_Parser<char>::Token &token, utf16char
716
716
template <typename CharType>
717
717
inline bool JSON_Parser<CharType>::handle_unescape_char(Token &token)
718
718
{
719
+ token.has_unescape_symbol = true ;
720
+
719
721
// This function converts unescaped character pairs (e.g. "\t") into their ASCII or Unicode representations (e.g. tab sign)
720
722
// Also it handles \u + 4 hexadecimal digits
721
723
auto ch = NextCharacter ();
@@ -784,6 +786,7 @@ inline bool JSON_Parser<CharType>::handle_unescape_char(Token &token)
784
786
template <typename CharType>
785
787
bool JSON_Parser<CharType>::CompleteStringLiteral(Token &token)
786
788
{
789
+ token.has_unescape_symbol = false ;
787
790
auto ch = NextCharacter ();
788
791
while ( ch != ' "' )
789
792
{
@@ -836,13 +839,18 @@ bool JSON_StringParser<CharType>::CompleteStringLiteral(typename JSON_Parser<Cha
836
839
837
840
if (ch == ' \\ ' )
838
841
{
839
- token.string_val .resize (m_position - start - 1 );
840
- if (token.string_val .size () > 0 )
841
- memcpy (&token.string_val [0 ], start, (m_position - start - 1 )*sizeof (CharType));
842
+ const size_t numChars = m_position - start - 1 ;
843
+ const size_t prevSize = token.string_val .size ();
844
+ token.string_val .resize (prevSize + numChars);
845
+ memcpy (const_cast <CharType *>(token.string_val .c_str () + prevSize), start, numChars * sizeof (CharType));
842
846
843
- token.has_unescape_symbol = true ;
847
+ if (!JSON_StringParser<CharType>::handle_unescape_char (token))
848
+ {
849
+ return false ;
850
+ }
844
851
845
- return finish_parsing_string_with_unescape_char (token);
852
+ // Reset start position and continue.
853
+ start = m_position;
846
854
}
847
855
else if (ch >= CharType (0x0 ) && ch < CharType (0x20 ))
848
856
{
@@ -852,47 +860,16 @@ bool JSON_StringParser<CharType>::CompleteStringLiteral(typename JSON_Parser<Cha
852
860
ch = JSON_StringParser<CharType>::NextCharacter ();
853
861
}
854
862
855
- token.string_val .resize (m_position - start - 1 );
856
- if (token.string_val .size () > 0 )
857
- memcpy (&token.string_val [0 ], start, (m_position - start - 1 )*sizeof (CharType));
863
+ const size_t numChars = m_position - start - 1 ;
864
+ const size_t prevSize = token.string_val .size ();
865
+ token.string_val .resize (prevSize + numChars);
866
+ memcpy (const_cast <CharType *>(token.string_val .c_str () + prevSize), start, numChars * sizeof (CharType));
858
867
859
868
token.kind = JSON_Parser<CharType>::Token::TKN_StringLiteral;
860
869
861
870
return true ;
862
871
}
863
872
864
- template <typename CharType>
865
- bool JSON_StringParser<CharType>::finish_parsing_string_with_unescape_char(typename JSON_Parser<CharType>::Token &token)
866
- {
867
- // This function handles parsing the string when an unescape character is encountered.
868
- // It is called once the part before the unescape char is copied to the token.string_val string
869
-
870
- typename JSON_Parser<CharType>::int_type ch;
871
-
872
- if (!JSON_StringParser<CharType>::handle_unescape_char (token))
873
- return false ;
874
-
875
- while ((ch = JSON_StringParser<CharType>::NextCharacter ()) != ' "' )
876
- {
877
- if (ch == ' \\ ' )
878
- {
879
- if (!JSON_StringParser<CharType>::handle_unescape_char (token))
880
- return false ;
881
- }
882
- else
883
- {
884
- if (ch == eof<CharType>())
885
- return false ;
886
-
887
- token.string_val .push_back (static_cast <CharType>(ch));
888
- }
889
- }
890
-
891
- token.kind = JSON_StringParser<CharType>::Token::TKN_StringLiteral;
892
-
893
- return true ;
894
- }
895
-
896
873
template <typename CharType>
897
874
void JSON_Parser<CharType>::GetNextToken(typename JSON_Parser<CharType>::Token& result)
898
875
{
0 commit comments