MCPcopy Create free account
hub / github.com/antirez/botlib / print_string_ptr

Function print_string_ptr

cJSON.c:898–1017  ·  view source on GitHub ↗

Render the cstring provided to an escaped version that can be printed. */

Source from the content-addressed store, hash-verified

896
897/* Render the cstring provided to an escaped version that can be printed. */
898static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
899{
900 const unsigned char *input_pointer = NULL;
901 unsigned char *output = NULL;
902 unsigned char *output_pointer = NULL;
903 size_t output_length = 0;
904 /* numbers of additional characters needed for escaping */
905 size_t escape_characters = 0;
906
907 if (output_buffer == NULL)
908 {
909 return false;
910 }
911
912 /* empty string */
913 if (input == NULL)
914 {
915 output = ensure(output_buffer, sizeof("\"\""));
916 if (output == NULL)
917 {
918 return false;
919 }
920 strcpy((char*)output, "\"\"");
921
922 return true;
923 }
924
925 /* set "flag" to 1 if something needs to be escaped */
926 for (input_pointer = input; *input_pointer; input_pointer++)
927 {
928 switch (*input_pointer)
929 {
930 case '\"':
931 case '\\':
932 case '\b':
933 case '\f':
934 case '\n':
935 case '\r':
936 case '\t':
937 /* one character escape sequence */
938 escape_characters++;
939 break;
940 default:
941 if (*input_pointer < 32)
942 {
943 /* UTF-16 escape sequence uXXXX */
944 escape_characters += 5;
945 }
946 break;
947 }
948 }
949 output_length = (size_t)(input_pointer - input) + escape_characters;
950
951 output = ensure(output_buffer, output_length + sizeof("\"\""));
952 if (output == NULL)
953 {
954 return false;
955 }

Callers 2

print_stringFunction · 0.85
print_objectFunction · 0.85

Calls 1

ensureFunction · 0.85

Tested by

no test coverage detected