Replace escapeable characters with escape sequence?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rui Maciel

    Replace escapeable characters with escape sequence?

    Is there a function which takes a string and outputs another equivalent
    string where all the non-ASCII characters (i.e., escapable characters) are
    replaced with the respective escape character sequence
    (i.e., '\t', '\n', "\uXXXX").

    Thanks in advance
    Rui Maciel
  • Keith Thompson

    #2
    Re: Replace escapeable characters with escape sequence?

    Rui Maciel <rui.maciel@gma il.comwrites:
    Is there a function which takes a string and outputs another equivalent
    string where all the non-ASCII characters (i.e., escapable characters) are
    replaced with the respective escape character sequence
    (i.e., '\t', '\n', "\uXXXX").
    There will be as soon as you write it.

    There's no such function in the standard C library. It's a useful
    enough task that I'm sure it's been done before, but it might be just
    as easy to roll your own.

    Do you really want the function to output the resulting string, or
    return it? The latter would likely be more useful, but in general
    returning a string from a function can be tricky. See the FAQ for
    help if you need it.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • Joe Wright

      #3
      Re: Replace escapeable characters with escape sequence?

      Rui Maciel wrote:
      Is there a function which takes a string and outputs another equivalent
      string where all the non-ASCII characters (i.e., escapable characters) are
      replaced with the respective escape character sequence
      (i.e., '\t', '\n', "\uXXXX").
      >
      There is no C function for that but it might be an interesting project
      for you. Hint: ASCII controls have value less than 32 (space).

      --
      Joe Wright
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • Keith Thompson

        #4
        Re: Replace escapeable characters with escape sequence?

        Joe Wright <joewwright@com cast.netwrites:
        Rui Maciel wrote:
        >Is there a function which takes a string and outputs another equivalent
        >string where all the non-ASCII characters (i.e., escapable characters) are
        >replaced with the respective escape character sequence
        >(i.e., '\t', '\n', "\uXXXX").
        >>
        >
        There is no C function for that but it might be an interesting project
        for you. Hint: ASCII controls have value less than 32 (space).
        The mention of "\uXXXX" implies that the OP isn't just interested in
        ASCII. (And character 127, DEL, is also a control character.)

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <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."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Malcolm McLean

          #5
          Re: Replace escapeable characters with escape sequence?


          "Joe Wright" <joewwright@com cast.netwrote in message
          news:CMWdnXUuxY R5tMvbnZ2dnUVZ_ gednZ2d@comcast .com...
          Rui Maciel wrote:
          >Is there a function which takes a string and outputs another equivalent
          >string where all the non-ASCII characters (i.e., escapable characters)
          >are
          >replaced with the respective escape character sequence
          >(i.e., '\t', '\n', "\uXXXX").
          >>
          >
          There is no C function for that but it might be an interesting project for
          you. Hint: ASCII controls have value less than 32 (space).
          >
          That's a bad hint.
          A better hint.

          Write a function
          int myisescape(char ch)

          That returns 0 if the character is not escaped, 1 if it is escaped.
          (The my is because of an evil rule that allows functions beginning with "is"
          to break. Don't try island, isoleucine, israel as identifiers).

          The another function
          void escapesequence( char *ret, char ch)

          This will put the escape sequence for ch into the buffer. Make the buffer
          big enough - in practise 64 bytes should be ample.

          --
          Free games and programming goodies.




          Comment

          • Kenneth Brody

            #6
            Re: Replace escapeable characters with escape sequence?

            Keith Thompson wrote:
            >
            Rui Maciel <rui.maciel@gma il.comwrites:
            Is there a function which takes a string and outputs another equivalent
            string where all the non-ASCII characters (i.e., escapable characters) are
            replaced with the respective escape character sequence
            (i.e., '\t', '\n', "\uXXXX").
            >
            There will be as soon as you write it.
            [...]
            Do you really want the function to output the resulting string, or
            return it? The latter would likely be more useful, but in general
            returning a string from a function can be tricky. See the FAQ for
            help if you need it.
            <pedant>
            To me, a function's "output" can include the return value, just
            as its parameters can be called "input".
            </pedant>

            --
            +-------------------------+--------------------+-----------------------+
            | Kenneth J. Brody | www.hvcomputer.com | #include |
            | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
            +-------------------------+--------------------+-----------------------+
            Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


            Comment

            • CBFalconer

              #7
              Re: Replace escapeable characters with escape sequence?

              Keith Thompson wrote:
              >
              .... snip ...
              >
              Do you really want the function to output the resulting string, or
              return it? The latter would likely be more useful, but in general
              returning a string from a function can be tricky. See the FAQ for
              help if you need it.
              Why do you say this? Take a look at ggets (on my home page, see
              the organization header) which can easily return an arbitrary
              string. However returning that as the function value would require
              an extra, confusing (IMO), input parameter as a place to record
              errors.

              --
              <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
              <http://www.securityfoc us.com/columnists/423>
              <http://www.aaxnet.com/editor/edit043.html>
              <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
              cbfalconer at maineline dot net



              --
              Posted via a free Usenet account from http://www.teranews.com

              Comment

              • Keith Thompson

                #8
                Re: Replace escapeable characters with escape sequence?

                CBFalconer <cbfalconer@yah oo.comwrites:
                Keith Thompson wrote:
                >>
                ... snip ...
                >>
                >Do you really want the function to output the resulting string, or
                >return it? The latter would likely be more useful, but in general
                >returning a string from a function can be tricky. See the FAQ for
                >help if you need it.
                >
                Why do you say this? Take a look at ggets (on my home page, see
                the organization header) which can easily return an arbitrary
                string. However returning that as the function value would require
                an extra, confusing (IMO), input parameter as a place to record
                errors.
                I said it was tricky, not impossible.

                It's tricky, in part, because there are multiple ways to do it, with a
                number of tradeoffs. It's a common stumbling block for beginners.

                ggets() "returns" an arbitrary string via a char** parameter, and
                requires the caller to free the allocated string. Another approach is
                to return a char* result and require the caller to free the allocated
                string. Yet another approach is to return a pointer to a static array
                object, as some of the C standard library functions do (this relieves
                the caller of the responsibility of freeing the string, but it has
                other problems). Yet another approach is to require the caller to
                pre-allocate a buffer and pass a pointer to it, as fgets() does.

                This is all much simpler and more convenient in some higher-level
                languages; in many of them, you can simply return a string value and
                let the caller use it.

                This is not meant as a criticism either of C or of ggets, just an
                observation.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <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."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • CBFalconer

                  #9
                  Re: Replace escapeable characters with escape sequence?

                  Keith Thompson wrote:
                  >
                  .... snip ...
                  >
                  This is not meant as a criticism either of C or of ggets, just an
                  observation.
                  Accepted as such. I was wondering if you had spotted something bad.

                  --
                  If you want to post a followup via groups.google.c om, ensure
                  you quote enough for the article to make sense. Google is only
                  an interface to Usenet; it's not Usenet itself. Don't assume
                  your readers can, or ever will, see any previous articles.
                  More details at: <http://cfaj.freeshell. org/google/>


                  --
                  Posted via a free Usenet account from http://www.teranews.com

                  Comment

                  • Malcolm McLean

                    #10
                    Re: Replace escapeable characters with escape sequence?


                    "CBFalconer " <cbfalconer@yah oo.comwrote in message
                    news:46574FED.2 7E02824@yahoo.c om...
                    Keith Thompson wrote:
                    >>
                    ... snip ...
                    >>
                    >This is not meant as a criticism either of C or of ggets, just an
                    >observation.
                    >
                    Accepted as such. I was wondering if you had spotted something bad.
                    >
                    If I was writing the code the I'd write it as


                    char *ggets(int *err)

                    and allow err to be null. However there is no good way of handling error
                    conditions in C. If we define return codes then the caller has got to write

                    switch(err)
                    {
                    case GGETS_OUTOFMEMO RY:
                    fprintf(stderr, "Out of memory\n");
                    exit(EXIT_FAILU RE);
                    case GGETS_IOERROR:
                    fprintf(stderr, "Input device failed\n");
                    exit(EXIT_FAILU RE);
                    case GGETS_EOF:
                    normalcodetohan dleendofinput() ;
                    break;
                    case GGETS_OK:
                    processline();
                    free(ptr);
                    break;
                    }

                    This becomes way unacceptable, particualarly when you've got many other
                    functions all returning their similar error codes, but there is no easy way
                    round it. If you flush a message to stderr within ggets() and return NULL in
                    the string then you break everything that doesn't want to handle errors in
                    that way.
                    --
                    Free games and programming goodies.


                    Comment

                    Working...