utf8.writer.h.xml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <chapter xml:id="utf8.writer.h">
  2. <title><tt>__vic/utf8/writer.h</tt></title>
  3. <chapter xml:id="utf8--writer">
  4. <title><tt>utf8::writer</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. template<class ByteWriter>
  7. class utf8::writer
  8. {
  9. public:
  10. using byte_writer_type = ByteWriter;
  11. ByteWriter &get_byte_writer();
  12. const ByteWriter &get_byte_writer() const;
  13. template<class... Args>
  14. explicit writer(Args&&... args); // since C++11
  15. writer(); // C++98 only
  16. explicit writer(ByteWriter w); // C++98 only
  17. void write(unicode_t cp);
  18. };
  19. template<class ByteWriter>
  20. utf8::writer<ByteWriter> utf8::make_writer(ByteWriter w);
  21. ]]></code-block>
  22. <p>UTF-8 code points to byte sequence writer. <tt>ByteWriter</tt>
  23. which models <tt>Writer&lt;unsigned char></tt> (see <xref to="writers"/>)
  24. is used as a byte output.</p>
  25. <section><title>Class members</title>
  26. <synopsis>
  27. <prototype>ByteWriter &amp;get_byte_writer()</prototype>
  28. <prototype>const ByteWriter &amp;get_byte_writer() const</prototype>
  29. <p>Returns reference to the used byte writer.</p>
  30. </synopsis>
  31. <synopsis>
  32. <prototype>template&lt;class... Args>
  33. explicit writer(Args&amp;&amp;... args) <sign>C++11</sign></prototype>
  34. <p>Forwards all parameters to the used byte writer.</p>
  35. </synopsis>
  36. <synopsis>
  37. <prototype>writer() <sign>C++98 only</sign></prototype>
  38. <prototype>explicit writer(ByteWriter r) <sign>C++98 only</sign></prototype>
  39. <p>Constructors for C++98 mode.</p>
  40. </synopsis>
  41. <synopsis>
  42. <prototype>void write(unicode_t cp)</prototype>
  43. <p>Writes the specified code point according to UTF-8 encoding rules.
  44. <tt>ByteWriter::write()</tt> is used to write individual bytes.</p>
  45. </synopsis>
  46. </section>
  47. <section><title>Free functions</title>
  48. <synopsis>
  49. <prototype>template&lt;class ByteWriter>
  50. utf8::writer&lt;ByteWriter> utf8::make_writer(ByteWriter w)</prototype>
  51. <p>Creates UTF-8 writer using specified <tt>ByteWriter</tt>.</p>
  52. </synopsis>
  53. </section>
  54. <section><title>Example</title>
  55. <code-block lang="C++"><![CDATA[
  56. #include<__vic/utf8/writer.h>
  57. #include<__vic/writers/string.h>
  58. #include<string>
  59. #include<vector>
  60. // C++11
  61. using utf8_string_writer = __vic::utf8::writer<__vic::string_writer>;
  62. // C++98
  63. struct utf8_string_writer : __vic::utf8::writer<__vic::string_writer>
  64. {
  65. explicit utf8_string_writer(std::string &s)
  66. : __vic::utf8::writer<__vic::string_writer>(__vic::string_writer(s)) {}
  67. };
  68. std::string encode_utf8(const std::vector<__vic::unicode_t> &code_points)
  69. {
  70. std::string utf8_res;
  71. utf8_string_writer w(utf8_res);
  72. for(auto cp : code_points) w.write(cp);
  73. return utf8_res;
  74. }
  75. ]]></code-block>
  76. </section>
  77. </chapter>
  78. </chapter>