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