MCPcopy Create free account
hub / github.com/OpenIPC/firmware / print_string_ptr

Function print_string_ptr

general/package/xmdp/src/cjson/cJSON.c:896–1015  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers 2

print_stringFunction · 0.85
print_objectFunction · 0.85

Calls 1

ensureFunction · 0.85

Tested by

no test coverage detected