strings without escaping

  • Thread starter magnus.moraberg
  • Start date
M

magnus.moraberg

Hi,

I'm testing a class who's objects relate to nodes within an xml file.
For example, my Class called "FireNode" has an object which relates to
this line in my xml doc -

<FireNode DEF='FireNode' firstField = '64 128 192 255' secondField =
'1 2 3 4'></FireNode>

I can also create an object and initialize it as follows -

auto_ptr<Node> node = populateNodeFromXmlFile("fireNode.xml");
FireNode* fireNode = dynamic_cast<FireNode*>(node.get());

where fireNode.xml contains only that xml line presented above and
populateNodeFromXmlFile is a static function provided my the api I'm
using.

In order to test FireNode, I what to carry out unit tests on objects
which I can initialize using different versions of "fireNode.xml".

However, there is also this static function available with the API -

populateNodeFromSTRING("<FireNode DEF=\'FireNode\' firstField = \'64
128 192 255\' secondField = \'1 2 3 4\'></FireNode>");

I like this function because it means that I have my xml code directly
in the c++ code and it will be easier for me to keep an eye on what
I'm testing. However, I don't like that I have to escape quotes etc.
In python you can do this -

str = r"<FireNode DEF='FireNode' firstField = '64 128 192 255'
secondField = '1 2 3 4'></FireNode>"

and eveything gets automatically escaped. C# has something similar. Is
there anyway of doing this is C++ or any suggestions of what I could
do?

Thanks,

Barry
 
B

Bart van Ingen Schenau

populateNodeFromSTRING("<FireNode DEF=\'FireNode\' firstField = \'64
128 192 255\' secondField = \'1 2 3 4\'></FireNode>");

I like this function because it means that I have my xml code directly
in the c++ code and it will be easier for me to keep an eye on what
I'm testing. However, I don't like that I have to escape quotes etc.
In python you can do this -

str = r"<FireNode DEF='FireNode' firstField = '64 128 192 255'
secondField = '1 2 3 4'></FireNode>"

and eveything gets automatically escaped. C# has something similar. Is
there anyway of doing this is C++ or any suggestions of what I could
do?

Withing a string-literal, you don't _need_ to escape everything. You
only need escapes for double quotes ("), backslashes (\) and special
characters (like newline, etc.). Especially single quotes (') don't
need to be escaped.

So, you could write the string just as:
"<FireNode DEF='FireNode' firstField = '64 128 192 255' secondField =
'1 2 3 4'> said:
Thanks,

Barry

Bart v Ingen Schenau
 
M

Michael DOUBEZ

[snip - context]
However, there is also this static function available with the API -

populateNodeFromSTRING("<FireNode DEF=\'FireNode\' firstField = \'64
128 192 255\' secondField = \'1 2 3 4\'></FireNode>");

I like this function because it means that I have my xml code directly
in the c++ code and it will be easier for me to keep an eye on what
I'm testing. However, I don't like that I have to escape quotes etc.
[snip]

Is there anyway of doing this is C++ or any suggestions of what I could
do?

You don't need to escape single quote in C++ or in any language I know of.
You can directly write:
populateNodeFromSTRING("<FireNode DEF='FireNode' firstField='64 128 192
255' secondField='1 2 3 4'></FireNode>");

Concerning double-quotes and backslash there is no way in c++ to avoid
escaping them.
 
M

magnus.moraberg

On Feb 3, 2:16 pm, (e-mail address removed) wrote:










Withing a string-literal, you don't _need_ to escape everything. You
only need escapes for double quotes ("), backslashes (\) and special
characters (like newline, etc.). Especially single quotes (') don't
need to be escaped.

So, you could write the string just as:
"<FireNode DEF='FireNode' firstField = '64 128 192 255' secondField =
'1 2 3 4'></FireNode>"





Bart v Ingen Schenau

Great, Thanks!
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top