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 | 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 | /** Only set for LSRs that will be used in a hash table. */ |
26 | int32_t hashCode = 0; |
27 | |
28 | LSR() : language("und" ), script("" ), region("" ) {} |
29 | |
30 | /** Constructor which aliases all subtag pointers. */ |
31 | LSR(const char *lang, const char *scr, const char *r) : |
32 | language(lang), script(scr), region(r), |
33 | regionIndex(indexForRegion(region)) {} |
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, 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 | |
48 | LSR &operator=(LSR &&other) U_NOEXCEPT; |
49 | LSR &operator=(const LSR &other) = delete; |
50 | |
51 | /** |
52 | * Returns a positive index (>0) for a well-formed region code. |
53 | * Do not rely on a particular region->index mapping; it may change. |
54 | * Returns 0 for ill-formed strings. |
55 | */ |
56 | static int32_t indexForRegion(const char *region); |
57 | |
58 | UBool operator==(const LSR &other) const; |
59 | |
60 | inline UBool operator!=(const LSR &other) const { |
61 | return !operator==(other); |
62 | } |
63 | |
64 | LSR &setHashCode(); |
65 | |
66 | private: |
67 | void deleteOwned(); |
68 | }; |
69 | |
70 | U_NAMESPACE_END |
71 | |
72 | #endif // __LSR_H__ |
73 | |