backslash gets to end of string when appending double quotes - c++

I'm finishing up a c++ program and have one last issue that I can't seem to find a fix for. I had the code cpcmd += '"' + path + '"' + " " + '"' + dek + '"'
after finding a question similar to mine I tried the following.
cpcmd += "\"" + path + "\"" + " " + "\"" + dek + "\""
cpcmd += \" + path + \" + " " + \" + dek + \"
The latter of which doesn't even compile.If I could avoid using double quotes I would but I need to pass commands to cmd.
Why does adding double quotes the first way I tried cause an extra \ to be added to the end of the string I'm appending to? It gets added between the double quote and the end of the string it's being appended to.
In answer to the comment yes cpcmd is a string all variables here are.
I knew I'd be downvoted but I don't understand why. If I was able to find an answer to this question from googling I wouldn't have asked it.
In answer to comment asking for a full example the following will produce what I'm talking about in the debugger.
i
#include <iostream>
#include <string>
int main()
{
std::string test = "this is a string";
test += '"';
std::cout << test;
}
Update: My apologies I got caught in looking at the debugger so much that I assumed that what's exactly shown in it for the values of the local variables is what gets passed. Again I'm sorry please don't downvote this anymore. I've voted myself to have it deleted.

Related

Escape Character for aws CloudWatch Log Query Insights

I'm working with an api that excecutes an aws Insights query. I check some business layers and then add filters to the query.
Those filters are from a list of errors that I can't change.
The trouble is that I cant make an standard function to add the filters. Now I'm using
public CWL_InsightsQuery NotLike(string field, string value, int index = -1) {
if (index < 0) {
int Index = QSegments.FindIndex(x => x.StartsWith("filter"));
QSegments[Index] = QSegments[Index] + " and " + SetInternalField(field) + " not like /"" + value + "/" ";
} else {
QSegments[index] = QSegments[index] + " and " + SetInternalField(field) + " not like /"" + value + "/" ";
}
return this;
}
QSegments stands for the construction of the query.
In simple terms, I ended up with an string like
|filter Data not like "value here lol"
this IS working, and its fine. The trouble starts when value has quotation marks, or different special characters.
So, Value can be this "is a" very 'unique' value /and i hate it/
so, I cant use '', neither / or " to declare the filter string.
Is there any escape character, as # in C#?
Would need something in CloudWatch Log insights like
#"I love how ""this query"" works 'every' time /i/ need it"
Thank you very much!

Removing expressions from QString using QRegExp

I'm having an issue removing expressions from a QString using QRegExp. I tried a countless number of regex to no avail. What am I doing wrong?
Sample Text (QString myString) In this instance, myString contains "\u0006\u0007\u0013Hello".
myString.remove(QRegExp("\\[u][0-9]{4}"));
It does not remove any instances of \uXXXX where X = numbers.
However, when I am specific such as:
myString.remove("\u0006");
It does remove it.
String literals are not always the same as character sequence
for (char c : "\u0006\u0007\u0013Hello".toCharArray()) {
System.out.println( c + " (" + (int)c + ")" );
}
System.out.println( "--------------" );
for (char c : "\\u0006\\u0007\\u0013Hello".toCharArray()) {
System.out.println( c + " (" + (int)c + ")" );
}
In the first example \u0006 is encoding an unicode code point, whereas in second the string actually contains a backslash.
The string literal only exist at compile time, at runtime they are character sequences.
Regexes are working over character sequence not over string litteral, and also backlash have special meaning and need to be escaped.
Also note that \u0041 is another way to encode A.
Maybe what you are looking for are unicode categories, maybe following can help:
string.replaceAll( "\\p{Cc}", "" )

Issues with encoding plus sign (executing Poco::URI::setQueryParameters and Poco::URI::getQueryParameters gives unexpected result)

Suppose I have a URI parameter with value, which contains plus signs (+) and other special chars.
When I execute URI::setQueryParameters and then URI::getQueryParameters, the resulted value is not the same as the original one - all special chars are fine, except the plus sign.
Could you, please, advice what is the conventional way to do this?
Workaround: explicitly invoke URI::encode with reserved containing plus sign. But this doesn't seem to be right, it really looks like a workaround.
Anyway, if this is the correct way to achieve this, what symbols should I include in reserved, if I want to avoid such surprises in the future?
Other observations: URI::decode has a parameter named plusAsSpace (defaulted to false), but this does not help. URI::getQueryParameters replaces + with (space) before calling URI::decode.
Here's a sample code:
const std::string value_with_plus_signs = "value+with+plus+signs";
Poco::URI::QueryParameters out_params;
out_params.push_back(std::make_pair("param", value_with_plus_signs));
Poco::URI uri("path");
uri.setQueryParameters(out_params);
const auto in_params = uri.getQueryParameters();
std::cout << "Expected: '" << value_with_plus_signs << "', received: '"
<< in_params.front().second << "'" << std::endl;
output: Expected: 'value+with+plus+signs', received: 'value with plus signs'
It seems this was fixed in Poco (notice that '+' is added to the symbols that are encoded by default):
https://github.com/pocoproject/poco/issues/1260
https://github.com/pocoproject/poco/commit/c32e683b6c00950ddfce817dfe8f3fc0b6846455
I tested your code with poco 1.7.9p2 and I got the correct results.

Cleaning up formatting after deletion using regex

I have a function similar to the one below appearing in multiple files. I want to use regex to get rid of all references to outputString, since clearly, they're wasteful.
... other functions, class declarations, etc
public String toString()
{
String outputString = "";
return ... some stuff
+ outputString;
}
... other functions, class declarations, etc
I'm happy to do this in multiple passes. So far I've got regexes to find the first and last line (String outputString = "";$ and ( \+ outputString;)$). However, I've got two problems: first, I want to get rid of the whitespace that results in deleting the two lines that refer to outputString. Second, I need the final ; on the second last line to move up to the line above it.
As a bonus, I'd also like to know what's wrong with adding the line start anchor (^) to either of the regexes I specified. It seems like doing so would tighten them up, but when I try something like ^( \+ outputString;)$ I get zero results.
After all's said and done the function above should look like this:
... other functions, class declarations, etc
public String toString()
{
return ... some stuff;
}
... other functions, class declarations, etc
Here's an example of what "some stuff" might be:
"name" + ":" + getName()+ "," +
"id" + ":" + getId()+ "]" + System.getProperties().getProperty("line.separator") +
" " + "student = "+(getStudent()!=null?Integer.toHexString(System.identityHashCode(getStudent())):"null")
Here's a concrete example:
Current:
public void delete()
{
Student existingStudent = student;
student = null;
if (existingStudent != null)
{
existingStudent.delete();
}
}
public String toString()
{
String outputString = "";
return super.toString() + "["+
"name" + ":" + getName()+ "," +
"id" + ":" + getId()+ "]" + System.getProperties().getProperty("line.separator") +
" " + "student = "+(getStudent()!=null?Integer.toHexString(System.identityHashCode(getStudent())):"null")
+ outputString;
}
public String getId()
{
return id;
}
Required:
public void delete()
{
Student existingStudent = student;
student = null;
if (existingStudent != null)
{
existingStudent.delete();
}
}
public String toString()
{
return super.toString() + "["+
"name" + ":" + getName()+ "," +
"id" + ":" + getId()+ "]" + System.getProperties().getProperty("line.separator") +
" " + "student = "+(getStudent()!=null?Integer.toHexString(System.identityHashCode(getStudent())):"null");
}
public String getId()
{
return id;
}
1st pass:
Find:
.*outputString.*\R
Replace with empty string.
Demo:
https://regex101.com/r/g3aYnp/2
2nd pass:
Find:
(toString\(\)[\s\S]+\))(\s*\R\s*?\})
Replace:
$1;$2
https://regex101.com/r/oxsNRW/3
Assuming that the wanted part of the return expression does not contain any semi colons (i.e. ;) then you can do it in one replace. Search for:
^ +String outputString = "";\R( +return [^;]+?)\R +\+ outputString;
and replace with:
\1;
The idea is to match all three lines in one go, to keep the wanted part and to add the ;.
An interesting point in this replacement. My first attempt had ... return [^;]+)\R +\+ ... and it failed whereas ... return [^;]+)\r\n +\+ ... worked. The \R version appeared to leave a line-break before the final ;. Turning on menu => View => Show symbol => Show end of line reveals that the greedy term within the capture group collected the \r and the \R matched only the \n. Changing to a non-greedy form allowed the \R to match the entire \r\n.

Eclipse Conditional replace with regex

Given the text
public void MyFunction(int i, String str, boolean doIt) {
Log.i(TAG, "Enter MyFunction(int i, String str, boolean doIt)");
I want to make some replacements on the second line, but not the first
public void MyFunction(int i, String str, boolean doIt) {
Log.i(TAG, "Enter MyFunction( i:" + i + ", str:" + str ", doIt:" + doIt + ")");
So far using the following regex I manage to get these results:
find "\w+\s+(\w+)([,\)])"
replace with "$1:" + $1 + "$2"
public void MyFunction(i:" + i + ", str:" + str ", doIt:" + doIt + ") ") {
Log.i(TAG, "Enter MyFunction( i:" + i + ", str:" + str ", doIt:" + doIt + ") ");
Is there any way to force the replace to be executed only on the Log.i lines?
EDIT:
I tried the following regex
"Log\.i\(.*?\((\s*(\w+\s+(\w+)([,\)]))+"
but $1,$2,$3 only contains the last match (the last argument: doIt)
$1=boolean doIt)
$2=doIt
$3=)
when there should be 3 sets of $1,$2,$3, one for each argument.
If you know how to retrieve multiple matches, that would also make for a solution
I caved,
I used this little perl to do the job:
next unless /Log\.i/;
s/TAG,/TAGG/;
s/(final\s+)?[^ \(]+\s+(\w+)([,\)])/$2:\" \+ $2 \+ \"$3/g;
s/TAGG/TAG,/;
with the command line:
perl -pi <scriptname> <file>
If someone still wants to contribute some, I understand I could have run perl as Eclipse external tool to process the java files. How do I do that?
UPDATE:
I wrote a post on how to use external perl to run the script from within Eclipse IDE
see the post