0

I'm currently facing an issue with a method parsing a string to another method. The problem is that I want to prevent it from using possible escape sequences.

The string I want to parse is not constant so (as far as I know) using the R declaration to make it a raw literal is not applicable here since I have to use variables.

Furthermore, in some cases there is user input included into the string (unconverted), so simply escaping those sequences by replacing a "\" character with "\\" is not an option either, the input can include those sequences too.

To be more precise on the issue: A string formatted like f.e. " "\x10\x4 \x6(" " is getting auto compiled and converted into a non-human readable format as soon as it gets parsed to the next function. I want to prevent that conversion without In order to get the exact same string in the next function which needs to work with it.

Hope someone can help me since I'm new to c++ programming. Thanks in advance :D

#include "pch.h"
#include <iostream>

int main()
{
    stringTester stringtester;
    std::string test = stringtester.exampleString();
    stringtester.stringOutput(test);
}

std::string stringTester::exampleString()
{
    std::string exampleInput = "\x10\x5\x1a\aTestInput\\n \x6(";
    return exampleInput;
}

void stringTester::stringOutput(std::string test)
{
    std::cout << test << std::endl;
}

The actual output her (copied from console) is " TestInput\n ( ", whereas the wanted output would be the original string "\x10\x5\x1a\aTestInput\n \x6("

Edit: It seems like on SO it can't show the unknown characters. There are xtra characters in front and after the "TestInput\n ("

6
  • And yes, i'm aware of the double letters, but my keyboard is broken :P Sorry for that Commented Sep 17, 2019 at 13:33
  • 1
    Can we please get some code and a better sample of what the input and output should be? Commented Sep 17, 2019 at 13:34
  • 1
    Create a minimal reproducible example Commented Sep 17, 2019 at 13:35
  • Unrelated: You have two complete paragraphs that looks very similar. Commented Sep 17, 2019 at 13:41
  • @eerorika Hope the example helps Commented Sep 17, 2019 at 13:53

2 Answers 2

1

When you write a string literal in your source code the compiler replaces escape sequences with the character that they represent. That's why the quoted string in your example gets turned into nonsense. The way to fix that is to either replace each backslash with two backslashes or to make it a raw string literal.

When your program reads text input it doesn't do any of those adjustments. So if the code does

std::string input;
std::cin >> input;

and the user types the characters \x10\x5\x1a\aTestInput\\n \x6( into the console, input will end up with the characters \x10\x5\x1a\aTestInput\\n \x6(.

Once you've got the string, whether as a string literal or as text from the console, you can do whatever you want with it.

Sign up to request clarification or add additional context in comments.

Comments

0

You have two possibilities for a backslash to remain a backslash in your C/C++ strings (and Java, JavaScript, PHP...)

  1. Double all the Backslashes

Just as you said, you want to double all backslashes. This is fine. If the input was:

\\\\

Then your C/C++ string is going to be:

"\\\\\\\\"

(a mouthful, I know...)

  1. Use the Hex/Octal Character

The other way, if you don't like the double backslash too much (if it scares you, somehow), is to use the character sequence in octal or hex (or Unicode in newer versions):

\    becomes   "\134"   or   "\x5C"

As you may notice, though, this means 4 characters per backslash. So most people will generally just double the backslash (one 2 characters). Plus the double backslash is well understood. The code point may not be as well known by programmers coming behind you.

As a side note, if your user can enter any character, then they can also enter the double quote (") character. It is important that you also escape those. You can similarly use the backslash and the double quote character or its code point:

\"   or   \042   or   \x22

3 Comments

There is also raw string R"(\\\\)" which is equivalent to "\\\\\\\\".
@Jarod42 Can you put a double quote (") inside a raw string?
Yes. it is if you have )" that would be "problematic". but raw string allow delimiter to solve that: R"delimiter("""""\\\\\)")delimiter".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.