u5e
UnicodeTextC++Library
truncate_on_codepoint.cpp
1 
2 /**
3  * \page truncate_on_codepoint Example: Truncate a string on codepoint boundary
4  *
5  * The challenge of truncating a string is that if you operate on the
6  * native level, you risk breaking a codepoint in half.
7  *
8  * The u5e library provides an easy way to truncate a text, starting
9  * from a target native size and finding the correct boundary.
10  *
11  * \code
12  */// Example on how to truncate on correct codepoint boundaries
13 #include <algorithm>
14 #include <iostream>
15 #include <string>
16 #include <u5e/utf8_string.hpp>
17 using std::string;
18 using u5e::utf8_string;
19 int main(int argc, char** argv) {
20  // the original string
21  string str("Ola\xCC\x81!");
22 
23  // we find the point on the native string where we want to truncate.
24  // Count 4 in octets leaves us in the middle of a codepoint
25  string::const_iterator stri = str.cbegin();
26  std::advance(stri, 4);
27 
28  // now we produce the output string truncated at where we want.
29  // note that this assumes that the text was already in utf8. this is
30  // not a conversion operator, it just makes sure the append happens
31  // while respecting the codepoint boundaries in the utf8 text.
32  utf8_string output("");
33  output.append(str.cbegin(), stri);
34 
35  std::cout << output.native_string << std::endl;
36 }
37 /**
38  * \endcode
39  */
main u5e namespace
basic_encodedstring(const NativeString &s)
basic_encodedstring & append(const_iterator first, const_iterator last)
basic_encodedstring< utf8, std::string > utf8_string
A basic_encodedstring of utf8 and std::string.
Definition: utf8_string.hpp:19