1//
2// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3//
4// Distributed under the Boost Software License, Version 1.0.
5// https://www.boost.org/LICENSE_1_0.txt
6
7//
8// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
9//
10// BIG FAT WARNING FOR Microsoft Visual Studio Users
11//
12// YOU NEED TO CONVERT THIS SOURCE FILE ENCODING TO UTF-8 WITH BOM ENCODING.
13//
14// Unfortunately MSVC understands that the source code is encoded as
15// UTF-8 only if you add useless BOM in the beginning.
16//
17// So, before you compile "wide" examples with MSVC, please convert them to text
18// files with BOM. There are two very simple ways to do it:
19//
20// 1. Open file with Notepad and save it from there. It would convert
21// it to file with BOM.
22// 2. In Visual Studio go File->Advances Save Options... and select
23// Unicode (UTF-8 with signature) Codepage 65001
24//
25// Note: once converted to UTF-8 with BOM, this source code would not
26// compile with other compilers, because no-one uses BOM with UTF-8 today
27// because it is absolutely meaningless in context of UTF-8.
28//
29// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
30//
31#include <boost/locale.hpp>
32#include <ctime>
33#include <iostream>
34
35int main()
36{
37 using namespace boost::locale;
38
39 // Create system default locale
40 generator gen;
41 std::locale loc = gen("");
42 // We need the boundary facet, currently only available via ICU
43 if(!std::has_facet<boundary::boundary_indexing<wchar_t>>(loc: loc)) {
44 std::cout << "boundary detection not implemented in this environment\n";
45 return 0;
46 }
47 std::locale::global(loc: loc);
48 std::wcout.imbue(loc: loc);
49
50 // This is needed to prevent the C stdio library from
51 // converting strings to narrow on some platforms
52 std::ios_base::sync_with_stdio(sync: false);
53
54 std::wstring text = L"Hello World! あにま! Linux2.6 and Windows7 is word and number. שָלוֹם עוֹלָם!";
55
56 std::wcout << text << std::endl;
57
58 boundary::wssegment_index index(boundary::word, text.begin(), text.end());
59
60 for(boundary::wssegment_index::iterator p = index.begin(), e = index.end(); p != e; ++p) {
61 std::wcout << L"Part [" << *p << L"] has ";
62 if(p->rule() & boundary::word_number)
63 std::wcout << L"number(s) ";
64 if(p->rule() & boundary::word_letter)
65 std::wcout << L"letter(s) ";
66 if(p->rule() & boundary::word_kana)
67 std::wcout << L"kana character(s) ";
68 if(p->rule() & boundary::word_ideo)
69 std::wcout << L"ideographic character(s) ";
70 if(p->rule() & boundary::word_none)
71 std::wcout << L"no word characters";
72 std::wcout << std::endl;
73 }
74
75 index.map(type: boundary::character, begin: text.begin(), end: text.end());
76
77 for(const boundary::wssegment& p : index)
78 std::wcout << L"|" << p;
79 std::wcout << L"|\n\n";
80
81 index.map(type: boundary::line, begin: text.begin(), end: text.end());
82
83 for(const boundary::wssegment& p : index)
84 std::wcout << L"|" << p;
85 std::wcout << L"|\n\n";
86
87 index.map(type: boundary::sentence, begin: text.begin(), end: text.end());
88
89 for(const boundary::wssegment& p : index)
90 std::wcout << L"|" << p;
91 std::wcout << "|\n\n";
92}
93
94// boostinspect:noascii
95

source code of boost/libs/locale/examples/wboundary.cpp