lsr.h 2.4 KB

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