utf8.reader.h.xml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <chapter xml:id="utf8.reader.h">
  2. <title><tt>__vic/utf8/reader.h</tt></title>
  3. <chapter xml:id="utf8--reader">
  4. <title><tt>utf8::reader</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. template<class ByteReader>
  7. class utf8::reader
  8. {
  9. public:
  10. using byte_reader_type = ByteReader;
  11. ByteReader &get_byte_reader();
  12. const ByteReader &get_byte_reader() const;
  13. template<class... Args>
  14. explicit reader(Args&&... args); // since C++11
  15. reader(); // C++98 only
  16. explicit reader(ByteReader r); // C++98 only
  17. status_t parse(unicode_t &cp);
  18. bool read(unicode_t &cp);
  19. };
  20. template<class ByteReader>
  21. utf8::reader<ByteReader> utf8::make_reader(ByteReader r);
  22. ]]></code-block>
  23. <p>Вычитывает UTF-8 code points из последовательности байтов.
  24. Последовательность читается посредством <tt>ByteReader</tt>, который
  25. моделирует <tt>Reader&lt;unsigned char></tt> (см. <xref to="readers"/>).</p>
  26. <section><title>Члены класса</title>
  27. <synopsis>
  28. <prototype>ByteReader &amp;get_byte_reader()</prototype>
  29. <prototype>const ByteReader &amp;get_byte_reader() const</prototype>
  30. <p>Возвращает ссылку на используемый byte reader.</p>
  31. </synopsis>
  32. <synopsis>
  33. <prototype>template&lt;class... Args>
  34. explicit reader(Args&amp;&amp;... args) <sign>C++11</sign></prototype>
  35. <p>Передаёт все параметры в используемый byte reader.</p>
  36. </synopsis>
  37. <synopsis>
  38. <prototype>reader() <sign>C++98 only</sign></prototype>
  39. <prototype>explicit reader(ByteReader r) <sign>C++98 only</sign></prototype>
  40. <p>Конструкторы для режима C++98.</p>
  41. </synopsis>
  42. <synopsis>
  43. <prototype>status_t parse(unicode_t &amp;cp)</prototype>
  44. <p>Пытается извлечь следующий code point из последовательности байтов, используя
  45. <tt>ByteReader</tt>. В случае успеха возвращается <tt>utf8::status::ok</tt>,
  46. code point сохраняется в <tt>cp</tt>. Если байты кончились, возвращается
  47. <tt>utf8::status::eof</tt>. Другие значения возвращаются в случае ошибок,
  48. подробности см. в <xref to="utf8--status"/>. Для доступа к отдельным байтам
  49. используется <tt>ByteReader::read()</tt>.</p>
  50. <note>Функция сама по себе не бросает исключений, но их может бросать
  51. <tt>ByteReader::read()</tt>.</note>
  52. </synopsis>
  53. <synopsis>
  54. <prototype>bool read(unicode_t &amp;cp)</prototype>
  55. <p>То же самое, что <tt>parse()</tt>, но возвращает <tt>true</tt> в случае
  56. успеха, <tt>false</tt> в случае EOF, бросает исключения из
  57. <xref to="utf8.exceptions.h"/> в остальных случаях.</p>
  58. </synopsis>
  59. </section>
  60. <section><title>Свободные функции</title>
  61. <synopsis>
  62. <prototype>template&lt;class ByteReader>
  63. utf8::reader&lt;ByteReader> utf8::make_reader(ByteReader r)</prototype>
  64. <p>Создаёт UTF-8 reader используя указанный <tt>ByteReader</tt>.</p>
  65. </synopsis>
  66. </section>
  67. <section><title>Пример</title>
  68. <code-block lang="C++"><![CDATA[
  69. #include<__vic/utf8/reader.h>
  70. #include<__vic/readers/string.h>
  71. #include<string>
  72. #include<cstdint>
  73. #include<iostream>
  74. // C++11
  75. using utf8_string_reader = __vic::utf8::reader<__vic::string_reader>;
  76. // C++98
  77. struct utf8_string_reader : __vic::utf8::reader<__vic::string_reader>
  78. {
  79. explicit utf8_string_reader(const std::string &s)
  80. : __vic::utf8::reader<__vic::string_reader>(__vic::string_reader(s)) {}
  81. };
  82. void print_utf8_code_points(const string &s)
  83. {
  84. utf8_string_reader r(s);
  85. __vic::unicode_t cp;
  86. while(r.read(cp))
  87. std::cout << uint_least32_t(cp) << '\n';
  88. }
  89. ]]></code-block>
  90. </section>
  91. </chapter>
  92. </chapter>