Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string with explicit escape sequence into relative character

Tags:

c++

escaping

I need a function to convert "explicit" escape sequences into the relative non-printable character. Es:

char str[] = "\\n";
cout << "Line1" << convert_esc(str) << "Line2" << endl:

would give this output:

Line1

Line2

Is there any function that does this?

like image 711
Michele De Pascalis Avatar asked Apr 10 '11 13:04

Michele De Pascalis


People also ask

What is use of '\ r escape sequence in C?

\r (Carriage Return) This is the escape sequence to position the cursor at the beginning of the line. Its ASCII value is 013.

What is the use of '\ t escape sequence?

Escape sequences They are primarily used to put nonprintable characters in character and string literals. For example, you can use escape sequences to put such characters as tab, carriage return, and backspace into an output stream.

What is the '\ n escape character?

Escape sequences are used inside strings, not just those for printf, to represent special characters. In particular, the \n escape sequence represents the newline character.


1 Answers

You can do that fairly easy, using the boost string algorithm library. For example:

#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>

void escape(std::string& str)
{
  boost::replace_all(str, "\\\\", "\\");
  boost::replace_all(str, "\\t",  "\t");
  boost::replace_all(str, "\\n",  "\n");
  // ... add others here ...
}

int main()
{
  std::string str = "This\\tis\\n \\\\a test\\n123";

  std::cout << str << std::endl << std::endl;
  escape(str);
  std::cout << str << std::endl;

  return 0;
}

This is surely not the most efficient way to do this (because it iterates the string multiple times), but it is compact and easy to understand.

Update: As ybungalobill has pointed out, this implementation will be wrong, whenever a replacement string produces a character sequence, that a later replacement is searching for or when a replacement removes/modifies a character sequence, that should have been replaced.

An example for the first case is "\\\\n" -> "\\n" -> "\n". When you put the "\\\\" -> "\\" replacement last (which seems to be the solution at a first glance), you get an example for the latter case "\\\\n" -> "\\\n". Obviously there is no simple solution to this problem, which makes this technique only feasible for very simple escape sequences.

If you need a generic (and more efficient) solution, you should implement a state machine that iterates the string, as proposed by davka.

like image 180
ollb Avatar answered Oct 03 '22 14:10

ollb