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:
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:
but it's ignored..
i even tryied these: elseif(blnString==true && text[CharacterIndex]=='\"' && text[CharacterIndex+1]=='\"' )
but is ignored too.. why?
i try these too: elseif(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 '""' :(
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?
'""' 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.
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:
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;
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);
}
^ 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).
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.
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).
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?