"Unescaping" literal strings

This is a discussion on "Unescaping" literal strings within the C Programming forums, part of the General Programming Boards category; Hi everyone, This is my first post :-). I was wondering if there was a standard library function that converted ...

  1. #1
    Registered User
    Join Date
    Jun 2006

    "Unescaping" literal strings

    Hi everyone,
    This is my first post :-). I was wondering if there was a standard library function that converted between a literal string and its escaped C version.
    For example, a function that took
    \"Hey there,\" he said. \\Yo, \u4F5B.
    and returned
    "Hey there," he said. \Yo, 佛.
    ...and that could convert it back the other way, similar to a C parser. I know it would be simple to write something that does this, but getting UTF escapes would be a bigger pain. Thanks in advance,

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Look up each character one at a time. If it's to be 'escaped', display its escaped form. For example, instead of printing a double quote, print a \". If you need that in a string, then you'd use this instead: \\\"

    You'll need to write your own.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2006


    Thanks man, I was hoping for an equivalent of the PHP addslashes() and stripslashes(), but whatever. :-) Thanks for the quick reply. By the way, looking through the coreutils printf(1) source, found this function that does what I'm looking for (http://cvs.savannah.gnu.org/viewcvs/...s&view=markup).
    Posting it here if it helps anybody else:

    /* Print a \ escape sequence starting at ESCSTART.
       Return the number of characters in the escape sequence
       besides the backslash.
       If OCTAL_0 is nonzero, octal escapes are of the form \0ooo, where o
       is an octal digit; otherwise they are of the form \ooo.  */
    static int
    print_esc (const char *escstart, bool octal_0)
      const char *p = escstart + 1;
      int esc_value = 0;		/* Value of \nnn escape. */
      int esc_length;		/* Length of \nnn escape. */
      if (*p == 'x')
          /* A hexadecimal \xhh escape sequence must have 1 or 2 hex. digits.  */
          for (esc_length = 0, ++p;
    	   esc_length < 2 && ISXDIGIT (*p);
    	   ++esc_length, ++p)
    	esc_value = esc_value * 16 + hextobin (*p);
          if (esc_length == 0)
    	error (EXIT_FAILURE, 0, _("missing hexadecimal number in escape"));
          putchar (esc_value);
      else if (isodigit (*p))
          /* Parse \0ooo (if octal_0 && *p == '0') or \ooo (otherwise).
             Allow \ooo if octal_0 && *p != '0'; this is an undocumented
             extension to POSIX that is compatible with Bash 2.05b.  */
          for (esc_length = 0, p += octal_0 && *p == '0';
    	   esc_length < 3 && isodigit (*p);
    	   ++esc_length, ++p)
    	esc_value = esc_value * 8 + octtobin (*p);
          putchar (esc_value);
      else if (*p && strchr ("\"\\abcfnrtv", *p))
        print_esc_char (*p++);
      else if (*p == 'u' || *p == 'U')
          char esc_char = *p;
          unsigned int uni_value;
          uni_value = 0;
          for (esc_length = (esc_char == 'u' ? 4 : 8), ++p;
    	   esc_length > 0;
    	   --esc_length, ++p)
    	  if (!ISXDIGIT (*p))
    	    error (EXIT_FAILURE, 0, _("missing hexadecimal number in escape"));
    	  uni_value = uni_value * 16 + hextobin (*p);
          /* A universal character name shall not specify a character short
    	 identifier in the range 00000000 through 00000020, 0000007F through
    	 0000009F, or 0000D800 through 0000DFFF inclusive. A universal
    	 character name shall not designate a character in the required
    	 character set.  */
          if ((uni_value <= 0x9f
    	   && uni_value != 0x24 && uni_value != 0x40 && uni_value != 0x60)
    	  || (uni_value >= 0xd800 && uni_value <= 0xdfff))
    	error (EXIT_FAILURE, 0, _("invalid universal character name \\%c%0*x"),
    	       esc_char, (esc_char == 'u' ? 4 : 8), uni_value);
          print_unicode_char (stdout, uni_value, 0);
          putchar ('\\');
          if (*p)
    	  putchar (*p);
      return p - escstart - 1;
    /* Print string STR, evaluating \ escapes. */
    static void
    print_esc_string (const char *str)
      for (; *str; str++)
        if (*str == '\\')
          str += print_esc (str, true);
          putchar (*str);
    Thanks alot,

  4. #4
    Registered User
    Join Date
    Apr 2006
    I believe sprintf() does what your looking for.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    No, it doesn't.

    char s[BUFSIZ];
    sprintf(s, "\\\"Hello, World!\\\");
    \"Hello, World!\"

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 10:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21