u5e
UnicodeTextC++Library
grapheme_iteration.cpp
1 /**
2  * \page grapheme_iteration Example: Iterate over graphemes in utf8 text
3  *
4  * This example covers the basic usage of iterating over utf8 text
5  * grapheme by grapheme.
6  *
7  * \code
8  */// Example on how to iterate
9 
10 #include <experimental/string_view>
11 #include <u5e/utf8_string_view.hpp>
12 #include <u5e/utf8_string_view_grapheme.hpp>
13 #include <u5e/utf8_string_view_grapheme_iterator.hpp>
14 #include <stdio.h>
15 
16 using std::experimental::string_view;
17 using u5e::utf8_string_view;
18 using u5e::utf8_string_view_grapheme;
19 using u5e::utf8_string_view_grapheme_iterator;
20 
21 int main(int argc, char **argv) {
22 
23  // for each argument
24  for (int i = 1; i < argc; i++) {
25 
26  // get a utf8_string_view
27  utf8_string_view p(argv[i]);
28 
29  // get the grapheme iterator from that
30  // Iterate grapheme by grapheme
32  gi != p.grapheme_end(); gi++) {
34  printf("[");
35 
36  // then iterate the codepoints inside that grapheme
37  for (utf8_string_view::const_iterator it = g.codepoint_begin();
38  it != g.codepoint_end(); it++ ) {
39  // the value dereferenced is the codepoint, not octets even if
40  // the original text had "wide" chars.
41  printf(" U+%06llx", (long long unsigned int)*it);
42  }
43 
44  printf(" ]");
45  }
46 
47  printf("\n");
48  }
49  return 0;
50 }
51 /**
52  * \endcode
53  */
main u5e namespace
utf8_const_iterator operator++(int junk)
basic_encodedstring< utf8, std::experimental::string_view > utf8_string_view
A basic_encodedstring of utf8 and string_view.
basic_grapheme< utf8_string_view > utf8_string_view_grapheme
A basic_grapheme of utf8_string_view.
const_codepoint_iterator codepoint_end()
basic_grapheme_iterator< utf8_string_view > utf8_string_view_grapheme_iterator
A basic_grapheme_iterator of utf8_string_view.
basic_encodedstring(const NativeString &s)
basic_grapheme_iterator< basic_encodedstring > grapheme_end()
bool operator!=(const utf8_const_iterator &rhs) const
bool operator!=(basic_grapheme_iterator other)
const codepoint operator*()
basic_grapheme_iterator< basic_encodedstring > grapheme_begin()
const_codepoint_iterator codepoint_begin()
basic_grapheme_iterator operator++(int i)