C\C++: how compare aspes?

Pages: 12
see these string:
text.push_back("var const hell as integer= \" """" \"");
now see these string:
text.push_back("var const hell as integer= \" \"\" \"");
if i do with second string:
1
2
3
4
5
6
else if(blnString==true && text[CharacterIndex]==char(34) && text[CharacterIndex+1]==char(34) )
            {
                Tok.Name+="\"\"";
                CharacterIndex++;
                cout << "\n Double Aspes\n";
            }

i will get the "Double Aspes" print... seems ok.. but the 1st string is ignored.. why these? the '""' have another ascii value or something?
i even tryied these code:
1
2
3
4
5
6
else if(blnString==true && text[CharacterIndex]=='""' && text[CharacterIndex+1]=='""' )
            {
                Tok.Name+="\"\"";
                CharacterIndex++;
                cout << "\n Double Aspes\n";
            }

but it's ignored..
i even tryied these:
else if(blnString==true && text[CharacterIndex]=='\"' && text[CharacterIndex+1]=='\"' )
but is ignored too.. why?
i try these too:
else if(blnString==true && text[CharacterIndex]=='"' && text[CharacterIndex+1]=='"' )
but ignored too... so what i can do for fix these code?
how compare the '""'? i know compare the '\"', but not '""' :(
I am not entirely sure what you are asking.

to put the " symbol into a string, you must use \"
it can be in single quotes: '\"' (this is the letter " as a character)\
or double quotes: "\"" (this is the STRING with one letter, the " symbol)

to compare them,
you can say if (character == '\"')
or
if(string1 == string2) where the strings have " in them possibly

but I am not sure if this helps you.
maybe what you want is:
if (string[index] == '\"' && string[index+1] == '\"')
or
string quotes = "\"\"";
target.find(...quotes..); // see if the target string has double quotes, and where?
Last edited on
and more: what is '""' and '\"'?
how can i compare both? or the '""' is realy igored?
'""' is a compiler error or a bug. There is some sort of multiple character syntax but I do not use it, but even with that you would need escape characters (the \) to talk about quotes.

'\"' is one character: "
\" is how you tell the c++ compiler that the symbol is DATA not C++ SYNTAX.
the symbols that C++ uses as part of the language, specifically the ones used in STRING processing, have to have this so the compiler can tell the difference between text data vs c++ code. There are also some for characters that cannot be typed, like the computer beep or backspace and so on. https://en.cppreference.com/w/cpp/language/escape
you only really need the two quotes and backslash for most normal code. Once in a while the others may be useful. So that is \" \' and \\ as the 3 you will see frequently.
\n, \0, \t, and others that are used with cout are also common. These are the ones that you would not normally be able to type, because no printable symbol exists for them which makes it hard to put them into text data without the codes. There are more of those but here again the top 3 are those I listed. (You *can* put them in without the codes, eg (char)(0) or (char)(13) etc) -- assuming ascii is in use or a compatible mapping. The escape sequence codes abstract that so it works with various locale and unicode type settings.

I told you how to compare it above. you do it one letter at a time, or you find it as a string are 2 ways. there are other ways too, but its unclear what more you need.
Last edited on
This snippet:
 
"var const hell as integer= \" """" \""
is equivalent to this:
 
"var const hell as integer= \" " /*outside the string*/ "" /*outside the string*/ " \""
C and C++ allow implicit concatenation of consecutive string literals (e.g. "a" "b" is the same as "ab"), thus the above is actually equivalent to
 
"var const hell as integer= \"  \""
You might be better off using raw strings. These don't require escaped chars for ' and " etc.

See https://en.cppreference.com/w/cpp/language/string_literal (6)
helios.. thank you now i get the point why seems ignored... i get the concatenation without notice... how can i avoid these type of implicit concatenation?
how i can test several or 4 '"'?
for avoid several problems, my literal string must only accept the'\"' and not '""'.
what i mean is: i can test character to character... the 2 double quotes can be sperared by space or not.... but the best is compare:
1
2
if(char(x) =='"' && char(x+1) =='"' && char(x+2) =='"' && char(x+3) =='"')
   do something; 

or use a boolean between the 4 double quotes for test it?
The problem is not how you're testing the string, but how you're writing it. If you want the string to contain four quotes in a row, write that:
 
"var const hell as integer= \" \"\"\"\" \""
Or you can read seeplus' link and use raw string literals.
helios now correct me 1 thing: how can i test if was used '""'?
You can't. It's as if you wrote int a = 00001; and asked how you can count the zeroes in front of the 1. 00001 and 1 represent the same value. In the exact same sense, "var const hell as integer= \" """" \"" and "var const hell as integer= \" \"" are the same string of characters. In fact, I encourage you to test it yourself:
1
2
3
auto a = "var const hell as integer= \" """" \"";
auto b = "var const hell as integer= \"  \"";
std::cout << (strcmp(a, b) ? "They're different?!" : "They're equal...") << std::endl;
A double quote character inside a string literal must be escaped.

This is a string literal containing four double quotes: "\"\"\"\"".

For example
1
2
#include <iostream>
int main() { std::cout << "\"\"\"\"" << '\n'; }

The output is exactly """"
Last edited on
But """" is an empty string! It's "" concatenated with "".


"\" """" \""

is actually "  "


There is

"\" " which is "(space)
then
"" which is empty string
then
" \"" which is (space)"

So the result is "  "


If you have:


R"(\" """" \")"

then this is:

\" """" \"


or without the un-needed escaping


R"(" """" ")"

is " """" "


What are you trying to achieve?
Last edited on
"What are you trying to achieve?"
see these string:
"hello world "" hey "" "
i need detect the '""' for my language syntax and avoid it
I think a simple loop is all you need to detect it. avoiding it or handling it is on you to logic up into your project.
1
2
3
4
5
6
7
8
char q='\"'; //it is tiresome to keep typing it. 
int qc{}
bool problemdetected{};
for(char &c: thestring)
{
  qc = (qc+1)*(c==q); //non quotes set it back to zero. 
  problemdetected |= (qc>1);
}
Last edited on
^ If this (once factored into an actual program) is not what you're looking for, you need to be very clear as to what the actual issue is.

If the problem is the escape sequences in string literals for your unit tests, load the test string from a file and you won't have to worry about that (or use raw strings, like others suggested).
Last edited on
ganado: are you tell me that i just need ignore it?
the stream do it for me?
I'm not saying you need to do anything. You do whatever you think is more maintainable, readable, etc. All I'm saying is that loading the test string from a file is an alternative to embedding it in C++ as a string literal. Either way will work.
jonnin: please explain what that 'for' lines do
it goes over the string one letter at a time looking for quotes, and counting them.
the counter resets if it gets other letters.
if it finds 2 or more in a row, your error is tripped (the boolean) and it stays tripped (or= so once true, stays true).

"hello world "" hey "" "
assuming this, including the outer quotes, is the example..
" qc = 1 (0+1 * true{1}) -> 1
h qc = 0 ( (1+1)*false{0}) ->0
e qc = 0 ( (1+1)*false{0}) ->0
l qc = 0 ( (1+1)*false{0}) ->0
... over and over..... worl.. ->
d qc = 0 ( (1+1)*false{0}) ->0
" qc = 1 (0+1 * true{1}) -> 1
" qc = 2 (1+1 * true{1}) -> 1 !!!BOOL TRIPS!!! qc>1!!!
...whatever else happens, you have detected the problem now.

it is rather compact form but what it is doing is simple.... it counts back to back quotes looking for 2 or more in a row, if it finds it, it trips an alert.

a common tweak here is instead of a bool to detect a yes/no problem or not, you can make that also a counter, and it serves as a bool (0, none detected, all clear) and a counter (not zero, true, and how many times it happened). I don't know if that has a name, I think of them as 'super boolean'. That helps if you want to know how many times the input is broken. Finally, if you don't want to know how many times it is broken, the first time you trip the bool to true you can stop the loop (break statement).
Last edited on
now i did a simple test... and YES, is the best:
1
2
3
4
5
6
string text2="var const hell as integer= \" "" "" \"";
    for(unsigned int i=0; i<text2.size(); i++)
    {
        if(text2[i]=='\"')
            cout << i<< "\tquotes\t";
    }

i have 1 question: why the ' "" ""' isn't counted?
i only get 2 prints instead 4.... why i can't control the '""'?
seems be an empty char or something..
the compiler have an option for i avoid the implicit concatenation?
Last edited on
Pages: 12