OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 void BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, | 683 void BasicJsonStringifier::SerializeStringUnchecked_(const SrcChar* src, |
684 DestChar* dest, | 684 DestChar* dest, |
685 int length) { | 685 int length) { |
686 dest += current_index_; | 686 dest += current_index_; |
687 DestChar* dest_start = dest; | 687 DestChar* dest_start = dest; |
688 | 688 |
689 // Assert that uc16 character is not truncated down to 8 bit. | 689 // Assert that uc16 character is not truncated down to 8 bit. |
690 // The <uc16, char> version of this method must not be called. | 690 // The <uc16, char> version of this method must not be called. |
691 ASSERT(sizeof(*dest) >= sizeof(*src)); | 691 ASSERT(sizeof(*dest) >= sizeof(*src)); |
692 | 692 |
693 *(dest++) = '"'; | |
694 for (int i = 0; i < length; i++) { | 693 for (int i = 0; i < length; i++) { |
695 SrcChar c = src[i]; | 694 SrcChar c = src[i]; |
696 if (DoNotEscape(c)) { | 695 if (DoNotEscape(c)) { |
697 *(dest++) = static_cast<DestChar>(c); | 696 *(dest++) = static_cast<DestChar>(c); |
698 } else { | 697 } else { |
699 const char* chars = &JsonEscapeTable[c * kJsonEscapeTableEntrySize]; | 698 const char* chars = &JsonEscapeTable[c * kJsonEscapeTableEntrySize]; |
700 while (*chars != '\0') *(dest++) = *(chars++); | 699 while (*chars != '\0') *(dest++) = *(chars++); |
701 } | 700 } |
702 } | 701 } |
703 | 702 |
704 *(dest++) = '"'; | |
705 current_index_ += static_cast<int>(dest - dest_start); | 703 current_index_ += static_cast<int>(dest - dest_start); |
706 } | 704 } |
707 | 705 |
708 | 706 |
709 template <bool is_ascii, typename Char> | 707 template <bool is_ascii, typename Char> |
710 void BasicJsonStringifier::SerializeString_(Vector<const Char> vector, | 708 void BasicJsonStringifier::SerializeString_(Vector<const Char> vector, |
711 Handle<String> string) { | 709 Handle<String> string) { |
712 int length = vector.length(); | 710 int length = vector.length(); |
| 711 Append_<is_ascii, char>('"'); |
713 // We make a rough estimate to find out if the current string can be | 712 // We make a rough estimate to find out if the current string can be |
714 // serialized without allocating a new string part. The worst case length of | 713 // serialized without allocating a new string part. The worst case length of |
715 // an escaped character is 6. Shifting left by 3 is a more pessimistic | 714 // an escaped character is 6. Shifting the remainin string length right by 3 |
716 // estimate than multiplying by 6, but faster to calculate. | 715 // is a more pessimistic estimate, but faster to calculate. |
717 static const int kEnclosingQuotesLength = 2; | 716 |
718 if (current_index_ + (length << 3) + kEnclosingQuotesLength < part_length_) { | 717 if (((part_length_ - current_index_) >> 3) > length) { |
719 if (is_ascii) { | 718 if (is_ascii) { |
720 SerializeStringUnchecked_( | 719 SerializeStringUnchecked_( |
721 vector.start(), | 720 vector.start(), |
722 SeqAsciiString::cast(*current_part_)->GetChars(), | 721 SeqAsciiString::cast(*current_part_)->GetChars(), |
723 length); | 722 length); |
724 } else { | 723 } else { |
725 SerializeStringUnchecked_( | 724 SerializeStringUnchecked_( |
726 vector.start(), | 725 vector.start(), |
727 SeqTwoByteString::cast(*current_part_)->GetChars(), | 726 SeqTwoByteString::cast(*current_part_)->GetChars(), |
728 length); | 727 length); |
729 } | 728 } |
730 } else { | 729 } else { |
731 Append_<is_ascii, char>('"'); | |
732 String* string_location = *string; | 730 String* string_location = *string; |
733 for (int i = 0; i < length; i++) { | 731 for (int i = 0; i < length; i++) { |
734 Char c = vector[i]; | 732 Char c = vector[i]; |
735 if (DoNotEscape(c)) { | 733 if (DoNotEscape(c)) { |
736 Append_<is_ascii, Char>(c); | 734 Append_<is_ascii, Char>(c); |
737 } else { | 735 } else { |
738 Append_<is_ascii, char>( | 736 Append_<is_ascii, char>( |
739 &JsonEscapeTable[c * kJsonEscapeTableEntrySize]); | 737 &JsonEscapeTable[c * kJsonEscapeTableEntrySize]); |
740 } | 738 } |
741 // If GC moved the string, we need to refresh the vector. | 739 // If GC moved the string, we need to refresh the vector. |
742 if (*string != string_location) { | 740 if (*string != string_location) { |
743 vector = GetCharVector<Char>(string); | 741 vector = GetCharVector<Char>(string); |
744 string_location = *string; | 742 string_location = *string; |
745 } | 743 } |
746 } | 744 } |
747 Append_<is_ascii, char>('"'); | |
748 } | 745 } |
| 746 |
| 747 Append_<is_ascii, char>('"'); |
749 } | 748 } |
750 | 749 |
751 | 750 |
752 template <> | 751 template <> |
753 bool BasicJsonStringifier::DoNotEscape(char c) { | 752 bool BasicJsonStringifier::DoNotEscape(char c) { |
754 return c >= '#' && c <= '~' && c != '\\'; | 753 return c >= '#' && c <= '~' && c != '\\'; |
755 } | 754 } |
756 | 755 |
757 | 756 |
758 template <> | 757 template <> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 SerializeString_<false, char>(flat.ToAsciiVector(), object); | 791 SerializeString_<false, char>(flat.ToAsciiVector(), object); |
793 } else { | 792 } else { |
794 SerializeString_<false, uc16>(flat.ToUC16Vector(), object); | 793 SerializeString_<false, uc16>(flat.ToUC16Vector(), object); |
795 } | 794 } |
796 } | 795 } |
797 } | 796 } |
798 | 797 |
799 } } // namespace v8::internal | 798 } } // namespace v8::internal |
800 | 799 |
801 #endif // V8_JSON_STRINGIFIER_H_ | 800 #endif // V8_JSON_STRINGIFIER_H_ |
OLD | NEW |