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