Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:42:59+00:00 2026-06-03T15:42:59+00:00

Let me start off by saying that I am not an expert in C.

  • 0

Let me start off by saying that I am not an expert in C. I have been reviewing the code of a JSON parser.

I am trying to understand this piece of code.

/* Render the cstring provided to an escaped version that can be printed. */
static char *print_string_ptr(const char *str)
{
    const char *ptr;
    char *ptr2,*out;
    int len=0;
    unsigned char token;

    if (!str)
        return cJSON_strdup("");
    ptr = str;
    while ((token = *ptr) && ++len) {
        if (strchr("\"\\\b\f\n\r\t", token))
            len++;
        else if (token < 32)
            len += 5;
        ptr++;
    }

    out = (char*)cJSON_malloc(len + 3);
    if (!out)
      return 0;

    ptr2 = out;
    ptr = str;
    *ptr2++ = '\"';
    while (*ptr) {
        if ((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\')
            *ptr2++ = *ptr++;
        else {
            *ptr2++ = '\\';
            switch (token = *ptr++) {
                case '\\':      *ptr2++='\\';   break;
                case '\"':      *ptr2++='\"';   break;
                case '\b':      *ptr2++='b';    break;
                case '\f':      *ptr2++='f';    break;
                case '\n':      *ptr2++='n';    break;
                case '\r':      *ptr2++='r';    break;
                case '\t':      *ptr2++='t';    break;
                default:
                    /* escape and print */
                    sprintf(ptr2, "u%04x", token);
                    ptr2 += 5;
                    break;
            }
        }
    }
    *ptr2++ = '\"';
    *ptr2++ = 0;
    return out;
}

A really general summary of how this code actually works would be really great, my impression has been that it is “beautifying” the JSON string, is that correct?

At first glance it appears to be replacing \r with r, but what would the point of this be?

I have been researching the functionality of sprintf, but for simple things such as printing out currency values or other formatting issues. But I haven’t got a clue what the sprintf function is doing here:

sprintf(ptr2,"u%04x",token);ptr2+=5;

And what is the purpose of the ptr2+=5 ?

Any insight into this would really be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:43:02+00:00Added an answer on June 3, 2026 at 3:43 pm

    What it’s doing is turning control characters into the escape sequences you’d normally use in C source code.

    if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') 
        *ptr2++=*ptr++;
    

    This is basically saying “if we have a normal character like a letter, digit, etc., just copy it directly from input to output.”

     else
     {
         *ptr2++='\\';
    

    Otherwise, we’re going to produce an escape sequence in the output, which will start with a backslash.

         switch (token=*ptr++)
         {
             case '\\':      *ptr2++='\\';   break;
             case '\"':      *ptr2++='\"';   break;
             case '\b':      *ptr2++='b';    break;
    

    Then, depending on which control character it finds, it generates the second character of the escape sequence, so an actual ‘backspace’ character in the input (which will compare equal to ‘\b’) will produce the two characters `\’ and ‘b’ in the output.

              case '\f':      *ptr2++='f';    break;
              case '\n':      *ptr2++='n';    break;
              case '\r':      *ptr2++='r';    break;
              case '\t':      *ptr2++='t';    break;
    

    and the same for form-feed, new-line, carriage return and tab.

              default: sprintf(ptr2,"u%04x",token);ptr2+=5;   break;  
    

    Otherwise, render the control character in hexadecimal, so it becomes something like \1234.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me start off by saying that the code in question is part of
Ok, let me start off by saying that I'm don't have the slightest clue
Okay let me start off by saying I have been looking around a lot
First off, let me start by saying that I am totally new to working
Let me start out by saying that I'm not a C developer and I
First let me start off by saying I do not believe I am leaking,
Let me start off by saying that I am very new to WPF and
Preface Let me start off by saying that I'm a relatively new programmer and
Ok, let me first start off by saying that I've only ever dealt with
Let me start off by saying, I'm not new to programming but am very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.