Replace escapeable characters with escape sequence?

R

Rui Maciel

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
 
K

Keith Thompson

Rui Maciel said:
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.
 
J

Joe Wright

Rui said:
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).
 
K

Keith Thompson

Joe Wright said:
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.)
 
M

Malcolm McLean

Joe Wright said:
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.
 
K

Kenneth Brody

Keith said:
Rui Maciel said:
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:[email protected]>
 
K

Keith Thompson

CBFalconer said:
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.
 
C

CBFalconer

Keith said:
.... 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/>
 
M

Malcolm McLean

CBFalconer said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top