locale_data.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Throws `std::invalid_argument` if parsing fails
  28. explicit locale_data(const std::string& locale_name);
  29. /// Return language (usually 2 lowercase letters, i.e. ISO-639 or 'C')
  30. const std::string& language() const { return language_; }
  31. /// Return country (usually 2 uppercase letters, i.e. ISO-3166)
  32. const std::string& country() const { return country_; }
  33. /// Return encoding/codeset, e.g. ISO8859-1 or UTF-8
  34. const std::string& encoding() const { return encoding_; }
  35. /// Return variant/modifier, e.g. euro or stroke
  36. const std::string& variant() const { return variant_; }
  37. /// Return iff the encoding is UTF-8
  38. bool is_utf8() const { return utf8_; }
  39. /// <summary>
  40. /// Parse a locale identifier of the form [language[_territory][.codeset][@modifier]]
  41. /// Allows a dash as the delimiter: [language-territory]
  42. ///
  43. /// Return true if the identifier is valid:
  44. /// - `language` is given and consists of ASCII letters
  45. /// - `territory`, if given, consists of ASCII letters
  46. /// - Any field started by a delimiter (`_`, `-`, `.`, `@`) is not empty
  47. /// Otherwise parsing is aborted. Valid values already parsed stay set, other are defaulted.
  48. /// </summary>
  49. bool parse(const std::string& locale_name);
  50. /// Get a representation in the form [language[_territory][.codeset][@modifier]]
  51. /// codeset is omitted if it is US-ASCII
  52. std::string to_string() const;
  53. private:
  54. void reset();
  55. bool parse_from_lang(const std::string& input);
  56. bool parse_from_country(const std::string& input);
  57. bool parse_from_encoding(const std::string& input);
  58. bool parse_from_variant(const std::string& input);
  59. };
  60. }}} // namespace boost::locale::util
  61. #ifdef BOOST_MSVC
  62. # pragma warning(pop)
  63. #endif
  64. #endif