473,796 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3426
Rui Maciel <ru********@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) 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********@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) 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********@com cast.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********@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: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.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

May 25 '07 #7
CBFalconer <cb********@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) 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.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

May 25 '07 #9

"CBFalconer " <cb********@yah oo.comwrote in message
news:46******** *******@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.
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
1882
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 and a heart in my case...). What else does'nt quite
14
3545
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
62116
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 supposed to write this function? String.replace(/</g,'&lt;');
14
3281
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 backslashes are removed.
6
8886
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 Regex.Replace("<stuff>text</stuff>", "<stuff>", "<stuff>\n" would give yo <stuff text</stuff
0
1479
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 the following commands to do so: LengthofOverlay = FileLen(PCLOven_Burn_Directory & OverlayForm) OverlayContent = StrDup(LengthofOverlay, "x") Seek(OverlayInteger, 1)
4
14532
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. txtS_Filename.Text.Replace("C:\", ""); The compiler has a problem with the newline slash character but even
3
15784
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 reproduce: string filename = "%10.xml"; //XML file with this name is existing XmlReader reader = new XmlTextReader(filename);
5
2581
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 @ shouldn't be replaced.
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.