Convert String with Explicit Escape Sequence into Relative Character

Convert string with explicit escape sequence into relative character

I think that you must write such function yourself since escape characters is a compile-time feature, i.e. when you write "\n" the compiler would replace the \n sequence with the eol character. The resulting string is of length 1 (excluding the terminating zero character).

In your case a string "\\n" is of length 2 (again excluding terminating zero) and contains \ and n.

You need to scan your string and when encountering \ check the following char. if it is one of the legal escapes, you should replace both of them with the corresponding character, otherwise skip or leave them both as is.

( http://ideone.com/BvcDE ):

string unescape(const string& s)
{
string res;
string::const_iterator it = s.begin();
while (it != s.end())
{
char c = *it++;
if (c == '\\' && it != s.end())
{
switch (*it++) {
case '\\': c = '\\'; break;
case 'n': c = '\n'; break;
case 't': c = '\t'; break;
// all other escapes
default:
// invalid escape sequence - skip it. alternatively you can copy it as is, throw an exception...
continue;
}
}
res += c;
}

return res;
}

How to convert a letter character into the corresponding escaped special character

n and \n are completely different characters. They are not related to each other in any way. \n is just a mnemonic. There is no formula.

How to supply escape characters as command-line arguments

Unfortunately, you need to do it manually: processing escape sequences is compiler's job, by the time the "hello\tworld" constant ends up in the area of string constants in the compiled code, the \t is already replaced by the ASCII code 9 (TAB). It should not be that difficult - in fact, it's an exercise number 3.2 of the classic K&R book:

Exercise 3-2. Write a function escape(s,t) that converts characters like newline and tab into
visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a
function for the other direction as well, converting escape sequences into the real characters.

Why is NOT std::string defined in program equal to std::string loaded from file when printing out using prinft() and string::c_str()?

Solution was to read text file by each character and then printing the string is correct.

std::string shader;
std::ifstream inputStream(path, std::ifstream::in);
char character;
if (inputStream.is_open())
{
while (inputStream.get(character))
{
shader += character;
}
}

Output when printing shader:

Sample Image

Adding \n after each getline should work too.

Convert string to UTF-8 escape sequence

Prior to C++11, there's no mandated support for UTF-8 in the standard.

There are two steps here:

  • convert to UTF-8 (unless it's already in UTF-8)
  • URL-escape the result (update: James Kanze covers this part)

Neither of them is particularly difficult to write for yourself portably, assuming you know what character encoding the input string uses[*]. Which means other people have done it before, you shouldn't need to write it yourself. If you search for them separately you might have better luck finding platform-independent code for each step.

Note there are two different ways to URL-escape a space character, either as + or as %20. Your example uses %20, so if that's important to you then don't accidentally use a URL-escape routine that does the other.

[*] It's not ISO-Latin-1, since that doesn't have the Euro sign[**], but it might be Windows CP-1252.

[**] Unless it's been added recently. Anyway, your example codes the Euro sign as UTF-8 bytes 0xE2 0x82 0xAC, which represent the Unicode code point 0x20AC, not code point 0x80 which it has in CP1252. So if it was originally a single-byte encoding then clearly an intelligent single-byte-to-unicode-code-point conversion has been applied along the way. You could say there are three steps:

  • convert the std::string to Unicode code points (depends on input encoding).
  • convert the Unicode to UTF-8
  • URL-escape the UTF-8

I am trying to print escape characters using for loop. My motive is to see the values that gets printed

This will resolve your problem

for i in range(100):
r = "\{}".format(i)
decoded_string = bytes(r, "utf-8").decode("unicode_escape")
x = ("\{}".format(i))
print(x+" = "+decoded_string)

Java escaping escape sequence

You may use the StringEscapeUtils. There is method escapeJava() on it. Unfortunately, imo, there is no way to escape unicode literals like \u0073 so for your example input "\"\n\u0073\"", StringEscapeUtils.escapeJava("\"\n\u0073\"") will return \"\ns\"



Related Topics



Leave a reply



Submit