1 | // |
2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
3 | // Copyright (c) 2023 Alexander Grund |
4 | // |
5 | // Distributed under the Boost Software License, Version 1.0. |
6 | // https://www.boost.org/LICENSE_1_0.txt |
7 | |
8 | #ifndef BOOST_LOCALE_UTIL_LOCALE_DATA_HPP |
9 | #define BOOST_LOCALE_UTIL_LOCALE_DATA_HPP |
10 | |
11 | #include <boost/locale/config.hpp> |
12 | #include <string> |
13 | |
14 | #ifdef BOOST_MSVC |
15 | # pragma warning(push) |
16 | # pragma warning(disable : 4251) |
17 | #endif |
18 | |
19 | namespace boost { namespace locale { namespace util { |
20 | |
21 | /// Holder and parser for locale names/identifiers |
22 | class BOOST_LOCALE_DECL locale_data { |
23 | std::string language_; |
24 | std::string country_; |
25 | std::string encoding_; |
26 | std::string variant_; |
27 | bool utf8_; |
28 | |
29 | public: |
30 | /// Default to C locale with US-ASCII encoding |
31 | locale_data(); |
32 | /// Construct from the parsed locale \see \ref parse |
33 | /// |
34 | /// \throws std::invalid_argument: parsing failed |
35 | explicit locale_data(const std::string& locale_name); |
36 | |
37 | /// Return language (usually 2 lowercase letters, i.e. ISO-639 or 'C') |
38 | const std::string& language() const { return language_; } |
39 | /// Return country (usually 2 uppercase letters, i.e. ISO-3166) |
40 | const std::string& country() const { return country_; } |
41 | /// Return encoding/codeset, e.g. ISO8859-1 or UTF-8 |
42 | const std::string& encoding() const { return encoding_; } |
43 | /// Set encoding, will be made uppercase by default as-if it was parsed |
44 | /// Returns \c *this for chaining |
45 | locale_data& encoding(std::string new_encoding, bool uppercase = true); |
46 | /// Return variant/modifier, e.g. euro or stroke |
47 | const std::string& variant() const { return variant_; } |
48 | /// Return iff the encoding is UTF-8 |
49 | bool is_utf8() const { return utf8_; } |
50 | |
51 | /// Parse a locale identifier of the form `[language[_territory][.codeset][@modifier]]` |
52 | /// |
53 | /// Allows a dash as the delimiter: `[language-territory]` |
54 | /// Return true if the identifier is valid: |
55 | /// - `language` is given and consists of ASCII letters |
56 | /// - `territory`, if given, consists of ASCII letters |
57 | /// - Any field started by a delimiter (`_`, `-`, `.`, `@`) is not empty |
58 | /// Otherwise parsing is aborted. Valid values already parsed stay set, other are defaulted. |
59 | bool parse(const std::string& locale_name); |
60 | |
61 | /// Get a representation in the form `[language[_territory][.codeset][@modifier]]` |
62 | /// codeset is omitted if it is US-ASCII |
63 | std::string to_string() const; |
64 | |
65 | private: |
66 | void reset(); |
67 | bool parse_from_lang(const std::string& input); |
68 | bool parse_from_country(const std::string& input); |
69 | bool parse_from_encoding(const std::string& input); |
70 | bool parse_from_variant(const std::string& input); |
71 | }; |
72 | |
73 | }}} // namespace boost::locale::util |
74 | |
75 | #ifdef BOOST_MSVC |
76 | # pragma warning(pop) |
77 | #endif |
78 | #endif |
79 | |