MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / dump_escaped

Method dump_escaped

Source/Utils/json.hpp:15816–16062  ·  view source on GitHub ↗

! @brief dump escaped string Escape a string by replacing certain special characters by a sequence of an escape character (backslash) and another character and other control characters by a sequence of "\u" followed by a four-digit hex representation. The escaped string is written to output stream @a o. @param[in] s the str

Source from the content-addressed store, hash-verified

15814 @complexity Linear in the length of string @a s.
15815 */
15816 void dump_escaped(const string_t& s, const bool ensure_ascii)
15817 {
15818 std::uint32_t codepoint;
15819 std::uint8_t state = UTF8_ACCEPT;
15820 std::size_t bytes = 0; // number of bytes written to string_buffer
15821
15822 // number of bytes written at the point of the last valid byte
15823 std::size_t bytes_after_last_accept = 0;
15824 std::size_t undumped_chars = 0;
15825
15826 for (std::size_t i = 0; i < s.size(); ++i)
15827 {
15828 const auto byte = static_cast<uint8_t>(s[i]);
15829
15830 switch (decode(state, codepoint, byte))
15831 {
15832 case UTF8_ACCEPT: // decode found a new code point
15833 {
15834 switch (codepoint)
15835 {
15836 case 0x08: // backspace
15837 {
15838 string_buffer[bytes++] = '\\';
15839 string_buffer[bytes++] = 'b';
15840 break;
15841 }
15842
15843 case 0x09: // horizontal tab
15844 {
15845 string_buffer[bytes++] = '\\';
15846 string_buffer[bytes++] = 't';
15847 break;
15848 }
15849
15850 case 0x0A: // newline
15851 {
15852 string_buffer[bytes++] = '\\';
15853 string_buffer[bytes++] = 'n';
15854 break;
15855 }
15856
15857 case 0x0C: // formfeed
15858 {
15859 string_buffer[bytes++] = '\\';
15860 string_buffer[bytes++] = 'f';
15861 break;
15862 }
15863
15864 case 0x0D: // carriage return
15865 {
15866 string_buffer[bytes++] = '\\';
15867 string_buffer[bytes++] = 'r';
15868 break;
15869 }
15870
15871 case 0x22: // quotation mark
15872 {
15873 string_buffer[bytes++] = '\\';

Callers

nothing calls this directly

Calls 3

createFunction · 0.85
sizeMethod · 0.45
write_charactersMethod · 0.45

Tested by

no test coverage detected