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.

3
  • 3
    With C++11, you can use raw string literals: see e.g. stackoverflow.com/questions/10501599/… Commented Jul 17, 2013 at 12:28
  • Well, that would seem like more work. I'm looking at ways of speeding up my work :) Thanks anyway :) Commented Jul 17, 2013 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.... Commented Jul 17, 2013 at 12:41

7 Answers 7

16

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.

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

Comments

7

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

2 Comments

Don't put underscores at the start of things
@doctorlove 17.6.4.3.5 - Literal suffix identifiers that do not start with an underscore are reserved for future standardization.
2
String str = "tblCustomers";
str = "'" + str + "'";

See more options here

3 Comments

Thanks, but that seems like an equal amount of work to double-quote strings. Isn't there a standard function to do this?
String QuotedStr(String &quotes, String &orig) { return quotes + orig + quotes; }
And I'd settle for just passing the string since I always want to double-quote it.
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? :>

1 Comment

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.
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.

2 Comments

Thanks. Is an ASCII symbol compatible across OSs and languages like English, Arabic and German? I think you meant Unicode :)
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.
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.

Comments

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.

Comments

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.