By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

Replace escapeable characters with escape sequence?

P: n/a
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
May 24 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Rui Maciel <ru********@gmail.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_Keith) ks***@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"
May 24 '07 #2

P: n/a
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 ---
May 25 '07 #3

P: n/a
Joe Wright <jo********@comcast.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_Keith) ks***@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"
May 25 '07 #4

P: n/a

"Joe Wright" <jo********@comcast.netwrote in message
news:CM******************************@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.
http://www.personal.leeds.ac.uk/~bgy1mm

May 25 '07 #5

P: n/a
Keith Thompson wrote:
>
Rui Maciel <ru********@gmail.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:Th*************@gmail.com>
May 25 '07 #6

P: n/a
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.securityfocus.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

May 25 '07 #7

P: n/a
CBFalconer <cb********@yahoo.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_Keith) ks***@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"
May 25 '07 #8

P: n/a
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.com, 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

May 25 '07 #9

P: n/a

"CBFalconer" <cb********@yahoo.comwrote in message
news:46***************@yahoo.com...
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_OUTOFMEMORY:
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
case GGETS_IOERROR:
fprintf(stderr, "Input device failed\n");
exit(EXIT_FAILURE);
case GGETS_EOF:
normalcodetohandleendofinput();
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.
http://www.personal.leeds.ac.uk/~bgy1mm

May 26 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.