2 Replies Latest reply: Nov 22, 2012 5:41 AM by zuchy RSS

    String::View - how to convert to wchar_t

    zuchy
      Hello coherence friends,
      i would like to know, how can I convert String::View to wchar_t in C++
      I need to transfer the result from coherence to xqilla/xerces parser which is operating with XMLCh* that is typed as const wchar_t*

      If I have:

      String::View mystringview

      obtained from deserialized POF object

      how can I convert it to variable of type wchar_t please ?

      I am quite lost...

      I am using coh 3.6.1.

      thank you,
      Jan
        • 1. Re: String::View - how to convert to wchar_t
          Mfalco-Oracle
          Hi Jan,

          The easiest thing would be to convert to a std::wstring first and then access it's wchar_t array. The Coherence String supports direct assignment to/from std::string and std::wstring.
          size32_t cch = mystringview->length(); // character count
          size_t cb = sizeof(XMLCh + 1) * cch; // byte count
          XMLCh* xstr = (XMLCh*) malloc(cb);
          
          std::wstring wstr = mystringview; // converts UTF-8 to wchar_t and inserts content into wstring
          memcpy(xstr, wstr.data(), cb);
          This however involved making at least one intermediate copy of the data. A more efficient solution would be to iterate over the contents of the String extracting one wchar_t at a time. Internally the String is stored in UTF-8 format, and so direct memcpy into wchar_t format isn't doable. The following is the iterative approach.
          size32_t cch = mystringview->length(); // character count
          size_t cb = sizeof(XMLCh + 1) * cch; // byte count
          XMLCh* xstr = (XMLCh*) malloc(cb);
          
          const char* iter = mystringview->getCString(); // obtain raw access to UTF-8 array
          for (size_t i = 0; i < cch; ++i)
              {
              xstr[i] = String::next(iter); // decodes the next wchar_t from the UTF-8 array, and increments iter accordingly
              }
          xstr[i] = 0;
          thanks,

          Mark
          Oracle Coherence
          • 2. Re: String::View - how to convert to wchar_t
            zuchy
            Thank you Mark, that's exactly what I am lookking for,
            Have a nice day, Jan