Dequote a string, unescaping escaped quotes

Discussion in 'C Programming' started by Jonny, Dec 19, 2005.

  1. Jonny

    Jonny Guest

    Hi,

    I am trying to write a C function which will dequote the string in a
    char * variable, and unescape any escaped quotes, so that, for example:

    "hello"

    would become:

    hello

    and:

    "\"hello\""

    would become:

    "hello"

    much in the same way as char *argv command-line arguments are treated
    when passed to function main.

    Please can you help.

    Regards,
    Jonny
    Jonny, Dec 19, 2005
    #1
    1. Advertising

  2. Jonny

    Daniel Kraft Guest

    Jonny wrote:
    > I am trying to write a C function which will dequote the string in a
    > char * variable, and unescape any escaped quotes, so that, for example:
    >
    > "hello"
    >
    > would become:
    >
    > hello
    >
    > and:
    >
    > "\"hello\""
    >
    > would become:
    >
    > "hello"
    >


    Dear Jonny!

    I think the best way is to go through the string character-by-character;
    then you simply ignore the opening quote und continue until you find its
    contra-piece.

    Inside the loop you copy every character which isn't a quote nor a
    backslash to your output; if you find a backslash, read the next
    character and output it, regardless of its meaning.

    Maybe you might want some further checks inside that, but in general
    this should implement what you expect.

    Example code (without error-checking or something like that):

    void unescape(const char* in, char* out)
    {
    // Opening quote expected.
    assert(*in=='\"');

    // Loop until closing quote is found.
    for(++in; *in!='\"'; ++in)
    {

    // Character to copy?
    if(*in!='\\')
    {
    *(out++)=*in;
    continue;
    }

    // No. Copy next one.
    *(out++)=*(++in);
    }
    }

    Yours,
    Daniel
    Daniel Kraft, Dec 19, 2005
    #2
    1. Advertising

  3. Jonny

    Michael Mair Guest

    Daniel Kraft wrote:
    <snip>
    > Example code (without error-checking or something like that):
    >
    > void unescape(const char* in, char* out)
    > {
    > // Opening quote expected.


    C99 style comments can be misleading on usenet.

    > assert(*in=='\"');


    Note that the assert macro should be used to make debugging
    easier and not to catch errors, let alone perform error handling.
    Example:
    if (*in != '\"') {
    /* You can issue an error message if wished
    fprintf(stderr, "useful error message pointing to line,"
    " function, and file\n");
    * maybe implemented by macro
    */
    return;
    }
    This is quite enough. If you feel sure that you need to abort
    the program on an error, use
    if (*in != '\"') {
    assert(0 && "Unexpected character, expected \'\"\'");
    return;
    }
    That works even if _NDEBUG is defined i.e. when assert does
    nothing.

    > // Loop until closing quote is found.
    > for(++in; *in!='\"'; ++in)
    > {
    >
    > // Character to copy?
    > if(*in!='\\')
    > {
    > *(out++)=*in;
    > continue;
    > }
    >
    > // No. Copy next one.
    > *(out++)=*(++in);


    An if/else construction may be clearer than if/continue.
    Note that the above will also "ruin" explicitly wanted
    backslashes.

    > }
    > }
    >
    > Yours,
    > Daniel



    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Dec 19, 2005
    #3
  4. Jonny

    Flash Gordon Guest

    Jonny wrote:
    > Hi,
    >
    > I am trying to write a C function which will dequote the string in a
    > char * variable, and unescape any escaped quotes, so that, for example:


    <snip examples>

    Then go ahead and write it.

    > Please can you help.


    Yes, you've missed off a semi-colon on line 42.

    In other words, if you want help with your code then post it together
    with information about what specific problems you have with it. We are
    not going to write your code for you.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Dec 19, 2005
    #4
  5. Jonny

    Michael Mair Guest

    Jonny wrote:
    > Hi,
    >
    > I am trying to write a C function which will dequote the string in a
    > char * variable, and unescape any escaped quotes, so that, for example:
    >
    > "hello"
    >
    > would become:
    >
    > hello
    >
    > and:
    >
    > "\"hello\""
    >
    > would become:
    >
    > "hello"
    >
    > much in the same way as char *argv command-line arguments are treated
    > when passed to function main.


    Do you want to copy the source string to a destination string
    or replace everything in the source string? You can do both with
    one function but be sure to think it through before.

    Finding:
    Just use strchr() to find quotes. If the number of backslashes
    immediately preceding the quote is odd, you can 'replace' \" by ",
    otherwise you replace " by nothing.
    Note that you must not run to a position before the beginning
    of the string, so use strrchr() to count backwards or take
    appropriate precautions. Restart strchr() and your counting such
    that you do not change already changed parts of the string.

    Bring your best shot at it for us to see and we will help you
    by pointing out problems and answering C questions.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Dec 19, 2005
    #5
  6. Michael Mair <> wrote:

    > This is quite enough. If you feel sure that you need to abort
    > the program on an error, use
    > if (*in != '\"') {
    > assert(0 && "Unexpected character, expected \'\"\'");
    > return;


    I suspect that OP will want

    exit( EXIT_FAILURE );

    if he does in fact want to abort the program on an error in the
    presence of _NDEBUG.

    > }


    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 19, 2005
    #6
  7. Jonny <> writes:
    > I am trying to write a C function which will dequote the string in a
    > char * variable, and unescape any escaped quotes, so that, for example:
    >
    > "hello"
    >
    > would become:
    >
    > hello
    >
    > and:
    >
    > "\"hello\""
    >
    > would become:
    >
    > "hello"
    >
    > much in the same way as char *argv command-line arguments are treated
    > when passed to function main.


    You'll need to define the problem more precisely. The language
    doesn't define any particular treatment of command-line arguments;
    they're just made available to the program by some system-specific
    means.

    If you were on a Unix-like system, I'd say that you're probably trying
    to duplicate what the shell does with command-line arguments before
    invoking a program; for example

    echo "\"hello\""

    translates each \" to " and drops the outer " characters. This is
    often very similar to the way string literals are treated within a C
    program, but it's not identical. It can also vary from one shell to
    another, and even within some shells depending on what options are
    specified.

    That's just one example of what you might be talking about. It's
    likely to be different on other operating systems.

    Once you've defined the problem, try implementing a solution in C. If
    you have problems with it, post here again and we can try to help.

    If this is a homework assignment, please say so.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 19, 2005
    #7
  8. Jonny

    Jonny Guest

    I wrote:

    > I am trying to write a C function which will dequote the string in a
    > char * variable, and unescape any escaped quotes, so that, for example:
    >
    > "hello"
    >
    > would become:
    >
    > hello
    >
    > and:
    >
    > "\"hello\""
    >
    > would become:
    >
    > "hello"
    >
    > much in the same way as char *argv command-line arguments are treated
    > when passed to function main.
    >
    > Please can you help.


    Many thanks to you all for your help and advice. I wasn't sure where to
    start, but you have given me enough information to have a go at this.

    Regards,
    Jonny
    Jonny, Dec 20, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vance Kessler

    Re: Unescaping ASP vbscript escaped string

    Vance Kessler, Mar 1, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,591
    Vance Kessler
    Mar 1, 2004
  2. Chris
    Replies:
    1
    Views:
    13,633
    Oisin
    Mar 24, 2006
  3. Greg
    Replies:
    7
    Views:
    26,708
    vektor
    May 17, 2011
  4. jOhn
    Replies:
    1
    Views:
    222
    Phlip
    Jan 29, 2008
  5. sprite
    Replies:
    2
    Views:
    293
    sprite
    Sep 2, 2010
Loading...

Share This Page