locale_data.hpp 2.9 KB

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