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