473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
May 24 '07 #1
9 3412
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
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
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

"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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PD | last post by:
Hello, I am new to python, but i am quite curious about the following. suppose you had print '\378' which should not work because \377 is the max. then it displays two characters (an 8...
14
by: Jon Maz | last post by:
Hi, I have been getting hopelessly confused with escaping escape characters in JScript! All I want to do is write a simple funtion: function DoubleUpBackSlash(inputString) { ??????? }
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
0
by: Mike Cooper | last post by:
Hi everyone, I am accessing several binary (PCL) files sequentially using a for loop. For each file I am using the fileget() command to populate the contents of the file into a string. I use...
4
by: jpierson | last post by:
Hi, I'm having a few problems with the replace function for replacing characters in a textbox. "C:\" is the string i am tryin to remove ,with it a command I am sending does not work. ...
3
by: Eckhard Schwabe | last post by:
I only found one post on Google where someone mentions the same problem with a DataSet: XmlDataReader in .Net 1.1 can not read XML files from a path which contains "%10" or "%3f". code to...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.