Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Visual Studio with std::quoted

Just making sure that it is indeed a bug and not something I might have misunderstood about the functionality of std::quoted

Here is the code that should in my opinion escape double quotes with double quotes and then unescape them back to the original string:

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

int main()
{
    std::string s = R"(something"something)";
    std::cout << "Original: \t" << s << std::endl;

    std::ostringstream oss;
    oss << std::quoted(s, '"', '"');
    std::string s1 = oss.str();
    std::cout << "Quoted: \t" << s1 << std::endl;

    std::istringstream iss(s1);
    std::string s2;
    iss >> std::quoted(s2, '"', '"');
    std::cout << "Unquoted: \t" << s2 << std::endl;

    return 0;
}

The expected output:

Original:   something"something
Quoted:     "something""something"
Unquoted:   something"something

However this is what I get in VS2017 15.6.6:

Original:   something"something
Quoted:     "something""something"
Unquoted:   something

Could anyone confirm this is a bug?

UPDATE:

Good news everyone. The ticket I filed with MS got marked as fixed.

like image 859
Killzone Kid Avatar asked Apr 26 '18 16:04

Killzone Kid


1 Answers

After further research into this problem I would like to answer my own question. Thanks to @Justin and @MatteoItalia for the insightful comments. In short, this is not a bug but rather an undefined behaviour, as VC++ seems to follow the standard to the dot, while other compilers taking liberties to interpret it in own way, because there is no strict guideline on how to deal with cases of delim == escape, and this paragraph doesn't explain it:

Until an unescaped delim character is reached or !in, extract characters from in and append them to s, except that if an escape is reached, ignore it and append the next character to s.

This is exactly what is happening with VC++, the first double quote " after something is interpreted as unescaped delim character rather than escape character, which terminates the routine. IMO it needs clarification, something like: If escape character is found, check if it is followed by delim character and if so, discard escape character. Here is original proposal I think.

I have filed a report at MS bug tracker, but not much hope for the fix TBH. Then I found this ticket where @Barry suggests to use existing implementation here and here from GCC, and this is exactly what I did.

This works as expected for me, but if you have any concerns or suggestions please do share.

like image 52
Killzone Kid Avatar answered Nov 06 '22 15:11

Killzone Kid