web-dev-qa-db-ja.com

C ++文字列のエスケープ

C++ std :: stringを別のstd :: stringに変換する最も簡単な方法は何ですか?これには、印刷できない文字がすべてエスケープされていますか?

たとえば、2文字の文字列[0x61,0x01]の場合、結果の文字列は「a\x01」または「a%01」になります。

18
Danra

Boostの 文字列アルゴリズムライブラリ を見てください。 is_print 分類子(およびその演算子!オーバーロード)を使用して、印刷できない文字を選択できます。また、 find_format() 関数を使用すると、それらを任意の形式に置き換えることができます。

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

struct character_escaper
{
    template<typename FindResultT>
    std::string operator()(const FindResultT& Match) const
    {
        std::string s;
        for (typename FindResultT::const_iterator i = Match.begin();
             i != Match.end();
             i++) {
            s += str(boost::format("\\x%02x") % static_cast<int>(*i));
        }
        return s;
    }
};

int main (int argc, char **argv)
{
    std::string s("a\x01");
    boost::find_format_all(s, boost::token_Finder(!boost::is_print()), character_escaper());
    std::cout << s << std::endl;
    return 0;
}
11
Josh Kelley

実行文字セットがASCIIおよびCHAR_BITが8のスーパーセットであると想定します。OutIterの場合はback_inserterを渡します。 =(たとえば、vector <char>または別の文字列)、ostream_iterator、またはその他の適切な出力イテレータ。

template<class OutIter>
OutIter write_escaped(std::string const& s, OutIter out) {
  *out++ = '"';
  for (std::string::const_iterator i = s.begin(), end = s.end(); i != end; ++i) {
    unsigned char c = *i;
    if (' ' <= c and c <= '~' and c != '\\' and c != '"') {
      *out++ = c;
    }
    else {
      *out++ = '\\';
      switch(c) {
      case '"':  *out++ = '"';  break;
      case '\\': *out++ = '\\'; break;
      case '\t': *out++ = 't';  break;
      case '\r': *out++ = 'r';  break;
      case '\n': *out++ = 'n';  break;
      default:
        char const* const hexdig = "0123456789ABCDEF";
        *out++ = 'x';
        *out++ = hexdig[c >> 4];
        *out++ = hexdig[c & 0xF];
      }
    }
  }
  *out++ = '"';
  return out;
}
9
Roger Pate

「最も簡単な方法」とは、他のリソース(ライブラリなど)に依存せずに、短くても簡単に理解できることを意味すると仮定すると、次のようになります。

#include <cctype>
#include <sstream>

// s is our escaped output string
std::string s = "";
// loop through all characters
for(char c : your_string)
{
    // check if a given character is printable
    // the cast is necessary to avoid undefined behaviour
    if(isprint((unsigned char)c))
        s += c;
    else
    {
        std::stringstream stream;
        // if the character is not printable
        // we'll convert it to a hex string using a stringstream
        // note that since char is signed we have to cast it to unsigned first
        stream << std::hex << (unsigned int)(unsigned char)(c);
        std::string code = stream.str();
        s += std::string("\\x")+(code.size()<2?"0":"")+code;
        // alternatively for URL encodings:
        //s += std::string("%")+(code.size()<2?"0":"")+code;
    }
}
5
Scindix

ある人の印刷できない文字は、別の人のマルチバイト文字です。したがって、どのバイトがどの文字にマップされ、どのバイトが印刷できないかを判断する前に、エンコーディングを定義する必要があります。

3
Douglas Leeder

Spirit.Karmaを使用してエスケープされた文字列出力を生成する の方法に関する記事を見たことがありますか?

2
hkaiser