locale_data.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. // Copyright (c) 2023-2024 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 script_;
  20. std::string country_;
  21. std::string encoding_;
  22. std::string variant_;
  23. bool utf8_;
  24. public:
  25. /// Default to C locale with US-ASCII encoding
  26. locale_data();
  27. /// Construct from the parsed locale \see \ref parse
  28. ///
  29. /// \throws std::invalid_argument: parsing failed
  30. explicit locale_data(const std::string& locale_name);
  31. /// Return language (usually 2 lowercase letters, i.e. ISO-639 or 'C')
  32. const std::string& language() const { return language_; }
  33. /// Return the ISO-15924 abbreviation script code if present
  34. const std::string& script() const { return script_; }
  35. /// Return country (usually 2 uppercase letters, i.e. ISO-3166)
  36. const std::string& country() const { return country_; }
  37. /// Return encoding/codeset, e.g. ISO8859-1 or UTF-8
  38. const std::string& encoding() const { return encoding_; }
  39. /// Set encoding, will be made uppercase by default as-if it was parsed
  40. /// Returns \c *this for chaining
  41. locale_data& encoding(std::string new_encoding, bool uppercase = true);
  42. /// Return variant/modifier, e.g. euro or stroke
  43. const std::string& variant() const { return variant_; }
  44. /// Return iff the encoding is UTF-8
  45. bool is_utf8() const { return utf8_; }
  46. /// Parse a locale identifier of the form `[language[_script][_territory][.codeset][@modifier]]`
  47. ///
  48. /// Allows a dash as the delimiter: `[language-territory]`
  49. /// Return true if the identifier is valid:
  50. /// - `language` is given and consists of ASCII letters
  51. /// - `script` is only considered if it consists of exactly 4 ASCII letters
  52. /// - `territory`, if given, consists of ASCII letters (usually ISO-3166)
  53. /// - Any field started by a delimiter (`_`, `-`, `.`, `@`) is not empty
  54. /// Otherwise parsing is aborted. Valid values already parsed stay set, other are defaulted.
  55. bool parse(const std::string& locale_name);
  56. /// Get a representation in the form `[language[_territory][.codeset][@modifier]]`
  57. /// codeset is omitted if it is US-ASCII
  58. std::string to_string() const;
  59. private:
  60. void reset();
  61. bool parse_from_lang(const std::string& input);
  62. bool parse_from_script(const std::string& input);
  63. bool parse_from_country(const std::string& input);
  64. bool parse_from_encoding(const std::string& input);
  65. bool parse_from_variant(const std::string& input);
  66. };
  67. }}} // namespace boost::locale::util
  68. #ifdef BOOST_MSVC
  69. # pragma warning(pop)
  70. #endif
  71. #endif