lsr.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // © 2019 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // lsr.h
  4. // created: 2019may08 Markus W. Scherer
  5. #ifndef __LSR_H__
  6. #define __LSR_H__
  7. #include "unicode/stringpiece.h"
  8. #include "unicode/utypes.h"
  9. #include "unicode/uobject.h"
  10. #include "cstring.h"
  11. U_NAMESPACE_BEGIN
  12. struct LSR final : public UMemory {
  13. static constexpr int32_t REGION_INDEX_LIMIT = 1001 + 26 * 26;
  14. static constexpr int32_t EXPLICIT_LSR = 7;
  15. static constexpr int32_t EXPLICIT_LANGUAGE = 4;
  16. static constexpr int32_t EXPLICIT_SCRIPT = 2;
  17. static constexpr int32_t EXPLICIT_REGION = 1;
  18. static constexpr int32_t IMPLICIT_LSR = 0;
  19. static constexpr int32_t DONT_CARE_FLAGS = 0;
  20. const char *language;
  21. const char *script;
  22. const char *region;
  23. char *owned = nullptr;
  24. /** Index for region, 0 if ill-formed. @see indexForRegion */
  25. int32_t regionIndex = 0;
  26. int32_t flags = 0;
  27. /** Only set for LSRs that will be used in a hash table. */
  28. int32_t hashCode = 0;
  29. LSR() : language("und"), script(""), region("") {}
  30. /** Constructor which aliases all subtag pointers. */
  31. LSR(const char *lang, const char *scr, const char *r, int32_t f) :
  32. language(lang), script(scr), region(r),
  33. regionIndex(indexForRegion(region)), flags(f) {}
  34. /**
  35. * Constructor which prepends the prefix to the language and script,
  36. * copies those into owned memory, and aliases the region.
  37. */
  38. LSR(char prefix, const char *lang, const char *scr, const char *r, int32_t f,
  39. UErrorCode &errorCode);
  40. LSR(StringPiece lang, StringPiece scr, StringPiece r, int32_t f,
  41. UErrorCode &errorCode);
  42. LSR(LSR &&other) noexcept;
  43. LSR(const LSR &other) = delete;
  44. inline ~LSR() {
  45. // Pure inline code for almost all instances.
  46. if (owned != nullptr) {
  47. deleteOwned();
  48. }
  49. }
  50. LSR &operator=(LSR &&other) noexcept;
  51. LSR &operator=(const LSR &other) = delete;
  52. /**
  53. * Returns a positive index (>0) for a well-formed region code.
  54. * Do not rely on a particular region->index mapping; it may change.
  55. * Returns 0 for ill-formed strings.
  56. */
  57. static int32_t indexForRegion(const char *region);
  58. UBool isEquivalentTo(const LSR &other) const;
  59. bool operator==(const LSR &other) const;
  60. inline bool operator!=(const LSR &other) const {
  61. return !operator==(other);
  62. }
  63. LSR &setHashCode();
  64. private:
  65. void deleteOwned();
  66. };
  67. U_NAMESPACE_END
  68. #endif // __LSR_H__