2

In Pascal Lazarus/Delphi, we have a function QuotedStr() that wraps any string within single quotes.

Here's an example of my current C++ code:

//I need to quote tblCustomers
pqxx::result r = txn.exec( "Select * from \"tblCustomers\" "); 

Another one:

//I need to quote cCustomerName
std::cout << "Name: " << r[a]["\"cCustomerName\""];

Similar to the above, I have to frequently double-quote strings. Typing this in is kind of slowing me down. Is there a standard function I can use for this?

BTW, I develop using Ubuntu/Windows with Code::Blocks. The technique used must be compatible across both platforms. If there's no function, this means that I must write one.

itsols
  • 5,406
  • 7
  • 51
  • 95
  • 3
    With C++11, you can use raw string literals: see e.g. http://stackoverflow.com/questions/10501599/define-stdstring-in-c-without-escape-characters/10501649#10501649 – Tony Delroy Jul 17 '13 at 12:28
  • Well, that would seem like more work. I'm looking at ways of speeding up my work :) Thanks anyway :) – itsols Jul 17 '13 at 12:34
  • `R"(...)"` is more work than `"..."` and having to worry about escaping everywhere? Not sure how you figure that. If there's only one or two characters to escape it may be more verbose, but there's less to think about or to go wrong.... – Tony Delroy Jul 17 '13 at 12:41

7 Answers7

13

C++14 added std::quoted which does exactly that, and more actually: it takes care of escaping quotes and backslashes in output streams, and of unescaping them in input streams. It is efficient, in that it does not create a new string, it's really a IO manipulator. (So you don't get a string, as you'd like.)

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
  std::string in = "\\Hello \"Wörld\"\\\n";

  std::stringstream ss;
  ss << std::quoted(in);
  std::string out;
  ss >> std::quoted(out);
  std::cout << '{' << in << "}\n"
            << '{' << ss.str() << "}\n"
            << '{' << out << "}\n";
}

gives

{\Hello "Wörld"\
}
{"\\Hello \"Wörld\"\\
"}
{\Hello "Wörld"\
}

As described in its proposal, it was really designed for round-tripping of strings.

akim
  • 8,255
  • 3
  • 44
  • 60
6

Using C++11 you can create user defined literals like this:

#include <iostream>
#include <string>
#include <cstddef>

// Define user defined literal "_quoted" operator.
std::string operator"" _quoted(const char* text, std::size_t len) {
    return "\"" + std::string(text, len) + "\"";
}

int main() {
    std::cout << "tblCustomers"_quoted << std::endl;
    std::cout << "cCustomerName"_quoted << std::endl;
}

Output:

"tblCustomers"
"cCustomerName"

You can even define the operator with a shorter name if you want, e.g.:

std::string operator"" _q(const char* text, std::size_t len) { /* ... */ }
// ...
std::cout << "tblCustomers"_q << std::endl;

More info on user-defined literals

10se1ucgo
  • 71
  • 1
  • 9
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
2
String str = "tblCustomers";
str = "'" + str + "'";

See more options here

Community
  • 1
  • 1
levengli
  • 1,091
  • 7
  • 18
  • Thanks, but that seems like an equal amount of work to double-quote strings. Isn't there a standard function to do this? – itsols Jul 17 '13 at 12:29
  • `String QuotedStr(String &quotes, String &orig) { return quotes + orig + quotes; }` – levengli Jul 17 '13 at 12:31
  • And I'd settle for just passing the string since I always want to double-quote it. – itsols Jul 17 '13 at 12:47
0

No standard function, unless you count std::basic_string::operator+(), but writing it is trivial.

I'm somewhat confused by what's slowing you down - quoted( "cCustomerName" ) is more characters, no? :>

SteveLove
  • 3,137
  • 15
  • 17
  • Yes, it seems like more characters. But when I have to do it often, it's faster and less error-prone that keying in escaped quotes. – itsols Jul 17 '13 at 12:32
0

You could use your own placeholder character to stand for the quote, some ASCII symbol that will never be used, and replace it with " just before you output the strings.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • Thanks. Is an ASCII symbol compatible across OSs and languages like English, Arabic and German? I think you meant Unicode :) – itsols Jul 17 '13 at 12:39
  • ASCII is not Unicode. Google for ASCII table. Compatibility with the outside world doesn't matter, this is just in your source code. You replace it before you output the string. – Neil Kirk Jul 17 '13 at 12:56
0
#include <iostream>
#include <string>

struct quoted
{
    const char * _text;
    quoted( const char * text ) : _text(text) {}

    operator std::string () const
    {
        std::string quotedStr = "\"";
        quotedStr += _text;
        quotedStr += "\"";
        return quotedStr;
    }
};

std::ostream & operator<< ( std::ostream & ostr, const quoted & q )
{
    ostr << "\"" << q._text << "\"";
    return ostr;
}

int main ( int argc, char * argv[] )
{
    std::string strq = quoted( "tblCustomers" );
    std::cout << strq << std::endl;

    std::cout << quoted( "cCustomerName" ) << std::endl;
    return 0;
}

With this you get what you want.

0

What about using some C function and backslash to escape the quotes? Like sprintf_s:

#define BUF_SIZE 100
void execute_prog() {

  //Strings that will be predicted by quotes 
  string param1 = "C:\\users\\foo\\input file.txt", string param2 = "output.txt";

  //Char array with any buffer size 
  char command[BUF_SIZE];

  //Concating my prog call in the C string.
  //sprintf_s requires a buffer size for security reasons
  sprintf_s(command, BUF_SIZE, "program.exe  \"%s\" \"%s\"", param1.c_str(), 
    param2.c_str());

  system(command);

}

Resulting string is:

program.exe "C:\users\foo\input file.txt" "output.txt"

Here is the documentation.

Ramon Dias
  • 835
  • 2
  • 12
  • 23