Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-07 10:23:56

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 ******************************************************************************
0005 *   Copyright (C) 1996-2016, International Business Machines
0006 *   Corporation and others.  All Rights Reserved.
0007 ******************************************************************************
0008 */
0009 
0010 /**
0011  * \file
0012  * \brief C++ API: Collation Service.
0013  */
0014 
0015 /**
0016 * File coll.h
0017 *
0018 * Created by: Helena Shih
0019 *
0020 * Modification History:
0021 *
0022 *  Date        Name        Description
0023 * 02/5/97      aliu        Modified createDefault to load collation data from
0024 *                          binary files when possible.  Added related methods
0025 *                          createCollationFromFile, chopLocale, createPathName.
0026 * 02/11/97     aliu        Added members addToCache, findInCache, and fgCache.
0027 * 02/12/97     aliu        Modified to create objects from RuleBasedCollator cache.
0028 *                          Moved cache out of Collation class.
0029 * 02/13/97     aliu        Moved several methods out of this class and into
0030 *                          RuleBasedCollator, with modifications.  Modified
0031 *                          createDefault() to call new RuleBasedCollator(Locale&)
0032 *                          constructor.  General clean up and documentation.
0033 * 02/20/97     helena      Added clone, operator==, operator!=, operator=, copy
0034 *                          constructor and getDynamicClassID.
0035 * 03/25/97     helena      Updated with platform independent data types.
0036 * 05/06/97     helena      Added memory allocation error detection.
0037 * 06/20/97     helena      Java class name change.
0038 * 09/03/97     helena      Added createCollationKeyValues().
0039 * 02/10/98     damiba      Added compare() with length as parameter.
0040 * 04/23/99     stephen     Removed EDecompositionMode, merged with
0041 *                          Normalizer::EMode.
0042 * 11/02/99     helena      Collator performance enhancements.  Eliminates the
0043 *                          UnicodeString construction and special case for NO_OP.
0044 * 11/23/99     srl         More performance enhancements. Inlining of
0045 *                          critical accessors.
0046 * 05/15/00     helena      Added version information API.
0047 * 01/29/01     synwee      Modified into a C++ wrapper which calls C apis
0048 *                          (ucol.h).
0049 * 2012-2014    markus      Rewritten in C++ again.
0050 */
0051 
0052 #ifndef COLL_H
0053 #define COLL_H
0054 
0055 #include "unicode/utypes.h"
0056 
0057 #if U_SHOW_CPLUSPLUS_API
0058 
0059 #if !UCONFIG_NO_COLLATION
0060 
0061 #include <functional>
0062 #include <string_view>
0063 #include <type_traits>
0064 
0065 #include "unicode/char16ptr.h"
0066 #include "unicode/uobject.h"
0067 #include "unicode/ucol.h"
0068 #include "unicode/unorm.h"
0069 #include "unicode/locid.h"
0070 #include "unicode/uniset.h"
0071 #include "unicode/umisc.h"
0072 #include "unicode/unistr.h"
0073 #include "unicode/uiter.h"
0074 #include "unicode/stringpiece.h"
0075 
0076 U_NAMESPACE_BEGIN
0077 
0078 class StringEnumeration;
0079 
0080 #if !UCONFIG_NO_SERVICE
0081 /**
0082  * @stable ICU 2.6
0083  */
0084 class CollatorFactory;
0085 #endif
0086 
0087 /**
0088 * @stable ICU 2.0
0089 */
0090 class CollationKey;
0091 
0092 /**
0093 * The <code>Collator</code> class performs locale-sensitive string
0094 * comparison.<br>
0095 * You use this class to build searching and sorting routines for natural
0096 * language text.
0097 * <p>
0098 * <code>Collator</code> is an abstract base class. Subclasses implement
0099 * specific collation strategies. One subclass,
0100 * <code>RuleBasedCollator</code>, is currently provided and is applicable
0101 * to a wide set of languages. Other subclasses may be created to handle more
0102 * specialized needs.
0103 * <p>
0104 * Like other locale-sensitive classes, you can use the static factory method,
0105 * <code>createInstance</code>, to obtain the appropriate
0106 * <code>Collator</code> object for a given locale. You will only need to
0107 * look at the subclasses of <code>Collator</code> if you need to
0108 * understand the details of a particular collation strategy or if you need to
0109 * modify that strategy.
0110 * <p>
0111 * The following example shows how to compare two strings using the
0112 * <code>Collator</code> for the default locale.
0113 * \htmlonly<blockquote>\endhtmlonly
0114 * <pre>
0115 * \code
0116 * // Compare two strings in the default locale
0117 * UErrorCode success = U_ZERO_ERROR;
0118 * Collator* myCollator = Collator::createInstance(success);
0119 * if (myCollator->compare("abc", "ABC") < 0)
0120 *   cout << "abc is less than ABC" << endl;
0121 * else
0122 *   cout << "abc is greater than or equal to ABC" << endl;
0123 * \endcode
0124 * </pre>
0125 * \htmlonly</blockquote>\endhtmlonly
0126 * <p>
0127 * You can set a <code>Collator</code>'s <em>strength</em> attribute to
0128 * determine the level of difference considered significant in comparisons.
0129 * Five strengths are provided: <code>PRIMARY</code>, <code>SECONDARY</code>,
0130 * <code>TERTIARY</code>, <code>QUATERNARY</code> and <code>IDENTICAL</code>.
0131 * The exact assignment of strengths to language features is locale dependent.
0132 * For example, in Czech, "e" and "f" are considered primary differences,
0133 * while "e" and "\u00EA" are secondary differences, "e" and "E" are tertiary
0134 * differences and "e" and "e" are identical. The following shows how both case
0135 * and accents could be ignored for US English.
0136 * \htmlonly<blockquote>\endhtmlonly
0137 * <pre>
0138 * \code
0139 * //Get the Collator for US English and set its strength to PRIMARY
0140 * UErrorCode success = U_ZERO_ERROR;
0141 * Collator* usCollator = Collator::createInstance(Locale::getUS(), success);
0142 * usCollator->setStrength(Collator::PRIMARY);
0143 * if (usCollator->compare("abc", "ABC") == 0)
0144 *     cout << "'abc' and 'ABC' strings are equivalent with strength PRIMARY" << endl;
0145 * \endcode
0146 * </pre>
0147 * \htmlonly</blockquote>\endhtmlonly
0148 *
0149 * The <code>getSortKey</code> methods
0150 * convert a string to a series of bytes that can be compared bitwise against
0151 * other sort keys using <code>strcmp()</code>. Sort keys are written as
0152 * zero-terminated byte strings.
0153 *
0154 * Another set of APIs returns a <code>CollationKey</code> object that wraps
0155 * the sort key bytes instead of returning the bytes themselves.
0156 * </p>
0157 * <p>
0158 * <strong>Note:</strong> <code>Collator</code>s with different Locale,
0159 * and CollationStrength settings will return different sort
0160 * orders for the same set of strings. Locales have specific collation rules,
0161 * and the way in which secondary and tertiary differences are taken into
0162 * account, for example, will result in a different sorting order for same
0163 * strings.
0164 * </p>
0165 * @see         RuleBasedCollator
0166 * @see         CollationKey
0167 * @see         CollationElementIterator
0168 * @see         Locale
0169 * @see         Normalizer2
0170 * @version     2.0 11/15/01
0171 */
0172 
0173 class U_I18N_API Collator : public UObject {
0174 public:
0175 
0176     // Collator public enums -----------------------------------------------
0177 
0178     /**
0179      * Base letter represents a primary difference. Set comparison level to
0180      * PRIMARY to ignore secondary and tertiary differences.<br>
0181      * Use this to set the strength of a Collator object.<br>
0182      * Example of primary difference, "abc" &lt; "abd"
0183      *
0184      * Diacritical differences on the same base letter represent a secondary
0185      * difference. Set comparison level to SECONDARY to ignore tertiary
0186      * differences. Use this to set the strength of a Collator object.<br>
0187      * Example of secondary difference, "&auml;" >> "a".
0188      *
0189      * Uppercase and lowercase versions of the same character represents a
0190      * tertiary difference.  Set comparison level to TERTIARY to include all
0191      * comparison differences. Use this to set the strength of a Collator
0192      * object.<br>
0193      * Example of tertiary difference, "abc" &lt;&lt;&lt; "ABC".
0194      *
0195      * Two characters are considered "identical" when they have the same unicode
0196      * spellings.<br>
0197      * For example, "&auml;" == "&auml;".
0198      *
0199      * UCollationStrength is also used to determine the strength of sort keys
0200      * generated from Collator objects.
0201      * @stable ICU 2.0
0202      */
0203     enum ECollationStrength
0204     {
0205         PRIMARY    = UCOL_PRIMARY,  // 0
0206         SECONDARY  = UCOL_SECONDARY,  // 1
0207         TERTIARY   = UCOL_TERTIARY,  // 2
0208         QUATERNARY = UCOL_QUATERNARY,  // 3
0209         IDENTICAL  = UCOL_IDENTICAL  // 15
0210     };
0211 
0212 
0213     // Cannot use #ifndef U_HIDE_DEPRECATED_API for the following, it is
0214     // used by virtual methods that cannot have that conditional.
0215 #ifndef U_FORCE_HIDE_DEPRECATED_API
0216     /**
0217      * LESS is returned if source string is compared to be less than target
0218      * string in the compare() method.
0219      * EQUAL is returned if source string is compared to be equal to target
0220      * string in the compare() method.
0221      * GREATER is returned if source string is compared to be greater than
0222      * target string in the compare() method.
0223      * @see Collator#compare
0224      * @deprecated ICU 2.6. Use C enum UCollationResult defined in ucol.h
0225      */
0226     enum EComparisonResult
0227     {
0228         LESS = UCOL_LESS,  // -1
0229         EQUAL = UCOL_EQUAL,  // 0
0230         GREATER = UCOL_GREATER  // 1
0231     };
0232 #endif  // U_FORCE_HIDE_DEPRECATED_API
0233 
0234     // Collator public destructor -----------------------------------------
0235 
0236     /**
0237      * Destructor
0238      * @stable ICU 2.0
0239      */
0240     virtual ~Collator();
0241 
0242     // Collator public methods --------------------------------------------
0243 
0244     /**
0245      * Returns true if "other" is the same as "this".
0246      *
0247      * The base class implementation returns true if "other" has the same type/class as "this":
0248      * `typeid(*this) == typeid(other)`.
0249      *
0250      * Subclass implementations should do something like the following:
0251      *
0252      *     if (this == &other) { return true; }
0253      *     if (!Collator::operator==(other)) { return false; }  // not the same class
0254      *
0255      *     const MyCollator &o = (const MyCollator&)other;
0256      *     (compare this vs. o's subclass fields)
0257      *
0258      * @param other Collator object to be compared
0259      * @return true if other is the same as this.
0260      * @stable ICU 2.0
0261      */
0262     virtual bool operator==(const Collator& other) const;
0263 
0264     /**
0265      * Returns true if "other" is not the same as "this".
0266      * Calls ! operator==(const Collator&) const which works for all subclasses.
0267      * @param other Collator object to be compared
0268      * @return true if other is not the same as this.
0269      * @stable ICU 2.0
0270      */
0271     virtual bool operator!=(const Collator& other) const;
0272 
0273     /**
0274      * Makes a copy of this object.
0275      * @return a copy of this object, owned by the caller
0276      * @stable ICU 2.0
0277      */
0278     virtual Collator* clone() const = 0;
0279 
0280     /**
0281      * Creates the Collator object for the current default locale.
0282      * The default locale is determined by Locale::getDefault.
0283      * The UErrorCode& err parameter is used to return status information to the user.
0284      * To check whether the construction succeeded or not, you should check the
0285      * value of U_SUCCESS(err).  If you wish more detailed information, you can
0286      * check for informational error results which still indicate success.
0287      * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
0288      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
0289      * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
0290      * used; neither the requested locale nor any of its fall back locales
0291      * could be found.
0292      * The caller owns the returned object and is responsible for deleting it.
0293      *
0294      * @param err    the error code status.
0295      * @return       the collation object of the default locale.(for example, en_US)
0296      * @see Locale#getDefault
0297      * @stable ICU 2.0
0298      */
0299     static Collator* U_EXPORT2 createInstance(UErrorCode&  err);
0300 
0301     /**
0302      * Gets the collation object for the desired locale. The
0303      * resource of the desired locale will be loaded.
0304      *
0305      * Locale::getRoot() is the base collation table and all other languages are
0306      * built on top of it with additional language-specific modifications.
0307      *
0308      * For some languages, multiple collation types are available;
0309      * for example, "de@collation=phonebook".
0310      * Starting with ICU 54, collation attributes can be specified via locale keywords as well,
0311      * in the old locale extension syntax ("el@colCaseFirst=upper")
0312      * or in language tag syntax ("el-u-kf-upper").
0313      * See <a href="https://unicode-org.github.io/icu/userguide/collation/api">User Guide: Collation API</a>.
0314      *
0315      * The UErrorCode& err parameter is used to return status information to the user.
0316      * To check whether the construction succeeded or not, you should check
0317      * the value of U_SUCCESS(err).  If you wish more detailed information, you
0318      * can check for informational error results which still indicate success.
0319      * U_USING_FALLBACK_ERROR indicates that a fall back locale was used.  For
0320      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
0321      * used.  U_USING_DEFAULT_ERROR indicates that the default locale data was
0322      * used; neither the requested locale nor any of its fall back locales
0323      * could be found.
0324      *
0325      * The caller owns the returned object and is responsible for deleting it.
0326      * @param loc    The locale ID for which to open a collator.
0327      * @param err    the error code status.
0328      * @return       the created table-based collation object based on the desired
0329      *               locale.
0330      * @see Locale
0331      * @see ResourceLoader
0332      * @stable ICU 2.2
0333      */
0334     static Collator* U_EXPORT2 createInstance(const Locale& loc, UErrorCode& err);
0335 
0336 #ifndef U_FORCE_HIDE_DEPRECATED_API
0337     /**
0338      * The comparison function compares the character data stored in two
0339      * different strings. Returns information about whether a string is less
0340      * than, greater than or equal to another string.
0341      * @param source the source string to be compared with.
0342      * @param target the string that is to be compared with the source string.
0343      * @return Returns a byte value. GREATER if source is greater
0344      * than target; EQUAL if source is equal to target; LESS if source is less
0345      * than target
0346      * @deprecated ICU 2.6 use the overload with UErrorCode &
0347      */
0348     virtual EComparisonResult compare(const UnicodeString& source,
0349                                       const UnicodeString& target) const;
0350 #endif  // U_FORCE_HIDE_DEPRECATED_API
0351 
0352     /**
0353      * The comparison function compares the character data stored in two
0354      * different strings. Returns information about whether a string is less
0355      * than, greater than or equal to another string.
0356      * @param source the source string to be compared with.
0357      * @param target the string that is to be compared with the source string.
0358      * @param status possible error code
0359      * @return Returns an enum value. UCOL_GREATER if source is greater
0360      * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
0361      * than target
0362      * @stable ICU 2.6
0363      */
0364     virtual UCollationResult compare(const UnicodeString& source,
0365                                       const UnicodeString& target,
0366                                       UErrorCode &status) const = 0;
0367 
0368 #ifndef U_FORCE_HIDE_DEPRECATED_API
0369     /**
0370      * Does the same thing as compare but limits the comparison to a specified
0371      * length
0372      * @param source the source string to be compared with.
0373      * @param target the string that is to be compared with the source string.
0374      * @param length the length the comparison is limited to
0375      * @return Returns a byte value. GREATER if source (up to the specified
0376      *         length) is greater than target; EQUAL if source (up to specified
0377      *         length) is equal to target; LESS if source (up to the specified
0378      *         length) is less  than target.
0379      * @deprecated ICU 2.6 use the overload with UErrorCode &
0380      */
0381     virtual EComparisonResult compare(const UnicodeString& source,
0382                                       const UnicodeString& target,
0383                                       int32_t length) const;
0384 #endif  // U_FORCE_HIDE_DEPRECATED_API
0385 
0386     /**
0387      * Does the same thing as compare but limits the comparison to a specified
0388      * length
0389      * @param source the source string to be compared with.
0390      * @param target the string that is to be compared with the source string.
0391      * @param length the length the comparison is limited to
0392      * @param status possible error code
0393      * @return Returns an enum value. UCOL_GREATER if source (up to the specified
0394      *         length) is greater than target; UCOL_EQUAL if source (up to specified
0395      *         length) is equal to target; UCOL_LESS if source (up to the specified
0396      *         length) is less  than target.
0397      * @stable ICU 2.6
0398      */
0399     virtual UCollationResult compare(const UnicodeString& source,
0400                                       const UnicodeString& target,
0401                                       int32_t length,
0402                                       UErrorCode &status) const = 0;
0403 
0404 #ifndef U_FORCE_HIDE_DEPRECATED_API
0405     /**
0406      * The comparison function compares the character data stored in two
0407      * different string arrays. Returns information about whether a string array
0408      * is less than, greater than or equal to another string array.
0409      * <p>Example of use:
0410      * <pre>
0411      * .       char16_t ABC[] = {0x41, 0x42, 0x43, 0};  // = "ABC"
0412      * .       char16_t abc[] = {0x61, 0x62, 0x63, 0};  // = "abc"
0413      * .       UErrorCode status = U_ZERO_ERROR;
0414      * .       Collator *myCollation =
0415      * .                         Collator::createInstance(Locale::getUS(), status);
0416      * .       if (U_FAILURE(status)) return;
0417      * .       myCollation->setStrength(Collator::PRIMARY);
0418      * .       // result would be Collator::EQUAL ("abc" == "ABC")
0419      * .       // (no primary difference between "abc" and "ABC")
0420      * .       Collator::EComparisonResult result =
0421      * .                             myCollation->compare(abc, 3, ABC, 3);
0422      * .       myCollation->setStrength(Collator::TERTIARY);
0423      * .       // result would be Collator::LESS ("abc" &lt;&lt;&lt; "ABC")
0424      * .       // (with tertiary difference between "abc" and "ABC")
0425      * .       result = myCollation->compare(abc, 3, ABC, 3);
0426      * </pre>
0427      * @param source the source string array to be compared with.
0428      * @param sourceLength the length of the source string array.  If this value
0429      *        is equal to -1, the string array is null-terminated.
0430      * @param target the string that is to be compared with the source string.
0431      * @param targetLength the length of the target string array.  If this value
0432      *        is equal to -1, the string array is null-terminated.
0433      * @return Returns a byte value. GREATER if source is greater than target;
0434      *         EQUAL if source is equal to target; LESS if source is less than
0435      *         target
0436      * @deprecated ICU 2.6 use the overload with UErrorCode &
0437      */
0438     virtual EComparisonResult compare(const char16_t* source, int32_t sourceLength,
0439                                       const char16_t* target, int32_t targetLength)
0440                                       const;
0441 #endif  // U_FORCE_HIDE_DEPRECATED_API
0442 
0443     /**
0444      * The comparison function compares the character data stored in two
0445      * different string arrays. Returns information about whether a string array
0446      * is less than, greater than or equal to another string array.
0447      * @param source the source string array to be compared with.
0448      * @param sourceLength the length of the source string array.  If this value
0449      *        is equal to -1, the string array is null-terminated.
0450      * @param target the string that is to be compared with the source string.
0451      * @param targetLength the length of the target string array.  If this value
0452      *        is equal to -1, the string array is null-terminated.
0453      * @param status possible error code
0454      * @return Returns an enum value. UCOL_GREATER if source is greater
0455      * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
0456      * than target
0457      * @stable ICU 2.6
0458      */
0459     virtual UCollationResult compare(const char16_t* source, int32_t sourceLength,
0460                                       const char16_t* target, int32_t targetLength,
0461                                       UErrorCode &status) const = 0;
0462 
0463     /**
0464      * Compares two strings using the Collator.
0465      * Returns whether the first one compares less than/equal to/greater than
0466      * the second one.
0467      * This version takes UCharIterator input.
0468      * @param sIter the first ("source") string iterator
0469      * @param tIter the second ("target") string iterator
0470      * @param status ICU status
0471      * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
0472      * @stable ICU 4.2
0473      */
0474     virtual UCollationResult compare(UCharIterator &sIter,
0475                                      UCharIterator &tIter,
0476                                      UErrorCode &status) const;
0477 
0478     /**
0479      * Compares two UTF-8 strings using the Collator.
0480      * Returns whether the first one compares less than/equal to/greater than
0481      * the second one.
0482      * This version takes UTF-8 input.
0483      * Note that a StringPiece can be implicitly constructed
0484      * from a std::string or a NUL-terminated const char * string.
0485      * @param source the first UTF-8 string
0486      * @param target the second UTF-8 string
0487      * @param status ICU status
0488      * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
0489      * @stable ICU 4.2
0490      */
0491     virtual UCollationResult compareUTF8(const StringPiece &source,
0492                                          const StringPiece &target,
0493                                          UErrorCode &status) const;
0494 
0495     /**
0496      * Transforms the string into a series of characters that can be compared
0497      * with CollationKey::compareTo. It is not possible to restore the original
0498      * string from the chars in the sort key.
0499      * <p>Use CollationKey::equals or CollationKey::compare to compare the
0500      * generated sort keys.
0501      * If the source string is null, a null collation key will be returned.
0502      *
0503      * Note that sort keys are often less efficient than simply doing comparison.
0504      * For more details, see the ICU User Guide.
0505      *
0506      * @param source the source string to be transformed into a sort key.
0507      * @param key the collation key to be filled in
0508      * @param status the error code status.
0509      * @return the collation key of the string based on the collation rules.
0510      * @see CollationKey#compare
0511      * @stable ICU 2.0
0512      */
0513     virtual CollationKey& getCollationKey(const UnicodeString&  source,
0514                                           CollationKey& key,
0515                                           UErrorCode& status) const = 0;
0516 
0517     /**
0518      * Transforms the string into a series of characters that can be compared
0519      * with CollationKey::compareTo. It is not possible to restore the original
0520      * string from the chars in the sort key.
0521      * <p>Use CollationKey::equals or CollationKey::compare to compare the
0522      * generated sort keys.
0523      * <p>If the source string is null, a null collation key will be returned.
0524      *
0525      * Note that sort keys are often less efficient than simply doing comparison.
0526      * For more details, see the ICU User Guide.
0527      *
0528      * @param source the source string to be transformed into a sort key.
0529      * @param sourceLength length of the collation key
0530      * @param key the collation key to be filled in
0531      * @param status the error code status.
0532      * @return the collation key of the string based on the collation rules.
0533      * @see CollationKey#compare
0534      * @stable ICU 2.0
0535      */
0536     virtual CollationKey& getCollationKey(const char16_t*source,
0537                                           int32_t sourceLength,
0538                                           CollationKey& key,
0539                                           UErrorCode& status) const = 0;
0540     /**
0541      * Generates the hash code for the collation object
0542      * @stable ICU 2.0
0543      */
0544     virtual int32_t hashCode() const = 0;
0545 
0546 #ifndef U_FORCE_HIDE_DEPRECATED_API
0547     /**
0548      * Gets the locale of the Collator
0549      *
0550      * @param type can be either requested, valid or actual locale. For more
0551      *             information see the definition of ULocDataLocaleType in
0552      *             uloc.h
0553      * @param status the error code status.
0554      * @return locale where the collation data lives. If the collator
0555      *         was instantiated from rules, locale is empty.
0556      * @deprecated ICU 2.8 This API is under consideration for revision
0557      * in ICU 3.0.
0558      */
0559     virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const = 0;
0560 #endif  // U_FORCE_HIDE_DEPRECATED_API
0561 
0562     /**
0563      * Convenience method for comparing two strings based on the collation rules.
0564      * @param source the source string to be compared with.
0565      * @param target the target string to be compared with.
0566      * @return true if the first string is greater than the second one,
0567      *         according to the collation rules. false, otherwise.
0568      * @see Collator#compare
0569      * @stable ICU 2.0
0570      */
0571     UBool greater(const UnicodeString& source, const UnicodeString& target)
0572                   const;
0573 
0574     /**
0575      * Convenience method for comparing two strings based on the collation rules.
0576      * @param source the source string to be compared with.
0577      * @param target the target string to be compared with.
0578      * @return true if the first string is greater than or equal to the second
0579      *         one, according to the collation rules. false, otherwise.
0580      * @see Collator#compare
0581      * @stable ICU 2.0
0582      */
0583     UBool greaterOrEqual(const UnicodeString& source,
0584                          const UnicodeString& target) const;
0585 
0586     /**
0587      * Convenience method for comparing two strings based on the collation rules.
0588      * @param source the source string to be compared with.
0589      * @param target the target string to be compared with.
0590      * @return true if the strings are equal according to the collation rules.
0591      *         false, otherwise.
0592      * @see Collator#compare
0593      * @stable ICU 2.0
0594      */
0595     UBool equals(const UnicodeString& source, const UnicodeString& target) const;
0596 
0597 #ifndef U_HIDE_DRAFT_API
0598 
0599     /**
0600      * Creates a comparison function object that uses this collator.
0601      * Like <code>std::equal_to</code> but uses the collator instead of <code>operator==</code>.
0602      * @draft ICU 76
0603      */
0604     inline auto equal_to() const { return Predicate<std::equal_to, UCOL_EQUAL>(*this); }
0605 
0606     /**
0607      * Creates a comparison function object that uses this collator.
0608      * Like <code>std::greater</code> but uses the collator instead of <code>operator&gt;</code>.
0609      * @draft ICU 76
0610      */
0611     inline auto greater() const { return Predicate<std::equal_to, UCOL_GREATER>(*this); }
0612 
0613     /**
0614      * Creates a comparison function object that uses this collator.
0615      * Like <code>std::less</code> but uses the collator instead of <code>operator&lt;</code>.
0616      * @draft ICU 76
0617      */
0618     inline auto less() const { return Predicate<std::equal_to, UCOL_LESS>(*this); }
0619 
0620     /**
0621      * Creates a comparison function object that uses this collator.
0622      * Like <code>std::not_equal_to</code> but uses the collator instead of <code>operator!=</code>.
0623      * @draft ICU 76
0624      */
0625     inline auto not_equal_to() const { return Predicate<std::not_equal_to, UCOL_EQUAL>(*this); }
0626 
0627     /**
0628      * Creates a comparison function object that uses this collator.
0629      * Like <code>std::greater_equal</code> but uses the collator instead of <code>operator&gt;=</code>.
0630      * @draft ICU 76
0631      */
0632     inline auto greater_equal() const { return Predicate<std::not_equal_to, UCOL_LESS>(*this); }
0633 
0634     /**
0635      * Creates a comparison function object that uses this collator.
0636      * Like <code>std::less_equal</code> but uses the collator instead of <code>operator&lt;=</code>.
0637      * @draft ICU 76
0638      */
0639     inline auto less_equal() const { return Predicate<std::not_equal_to, UCOL_GREATER>(*this); }
0640 
0641 #endif  // U_HIDE_DRAFT_API
0642 
0643 #ifndef U_FORCE_HIDE_DEPRECATED_API
0644     /**
0645      * Determines the minimum strength that will be used in comparison or
0646      * transformation.
0647      * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
0648      * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
0649      * are ignored.
0650      * @return the current comparison level.
0651      * @see Collator#setStrength
0652      * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
0653      */
0654     virtual ECollationStrength getStrength() const;
0655 
0656     /**
0657      * Sets the minimum strength to be used in comparison or transformation.
0658      * <p>Example of use:
0659      * <pre>
0660      *  \code
0661      *  UErrorCode status = U_ZERO_ERROR;
0662      *  Collator*myCollation = Collator::createInstance(Locale::getUS(), status);
0663      *  if (U_FAILURE(status)) return;
0664      *  myCollation->setStrength(Collator::PRIMARY);
0665      *  // result will be "abc" == "ABC"
0666      *  // tertiary differences will be ignored
0667      *  Collator::ComparisonResult result = myCollation->compare("abc", "ABC");
0668      * \endcode
0669      * </pre>
0670      * @see Collator#getStrength
0671      * @param newStrength the new comparison level.
0672      * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
0673      */
0674     virtual void setStrength(ECollationStrength newStrength);
0675 #endif  // U_FORCE_HIDE_DEPRECATED_API
0676 
0677     /**
0678      * Retrieves the reordering codes for this collator.
0679      * @param dest The array to fill with the script ordering.
0680      * @param destCapacity The length of dest. If it is 0, then dest may be nullptr and the function
0681      *  will only return the length of the result without writing any codes (pre-flighting).
0682      * @param status A reference to an error code value, which must not indicate
0683      * a failure before the function call.
0684      * @return The length of the script ordering array.
0685      * @see ucol_setReorderCodes
0686      * @see Collator#getEquivalentReorderCodes
0687      * @see Collator#setReorderCodes
0688      * @see UScriptCode
0689      * @see UColReorderCode
0690      * @stable ICU 4.8
0691      */
0692      virtual int32_t getReorderCodes(int32_t *dest,
0693                                      int32_t destCapacity,
0694                                      UErrorCode& status) const;
0695 
0696     /**
0697      * Sets the ordering of scripts for this collator.
0698      *
0699      * <p>The reordering codes are a combination of script codes and reorder codes.
0700      * @param reorderCodes An array of script codes in the new order. This can be nullptr if the
0701      * length is also set to 0. An empty array will clear any reordering codes on the collator.
0702      * @param reorderCodesLength The length of reorderCodes.
0703      * @param status error code
0704      * @see ucol_setReorderCodes
0705      * @see Collator#getReorderCodes
0706      * @see Collator#getEquivalentReorderCodes
0707      * @see UScriptCode
0708      * @see UColReorderCode
0709      * @stable ICU 4.8
0710      */
0711      virtual void setReorderCodes(const int32_t* reorderCodes,
0712                                   int32_t reorderCodesLength,
0713                                   UErrorCode& status) ;
0714 
0715     /**
0716      * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
0717      * codes will be grouped and must reorder together.
0718      * Beginning with ICU 55, scripts only reorder together if they are primary-equal,
0719      * for example Hiragana and Katakana.
0720      *
0721      * @param reorderCode The reorder code to determine equivalence for.
0722      * @param dest The array to fill with the script equivalence reordering codes.
0723      * @param destCapacity The length of dest. If it is 0, then dest may be nullptr and the
0724      * function will only return the length of the result without writing any codes (pre-flighting).
0725      * @param status A reference to an error code value, which must not indicate
0726      * a failure before the function call.
0727      * @return The length of the of the reordering code equivalence array.
0728      * @see ucol_setReorderCodes
0729      * @see Collator#getReorderCodes
0730      * @see Collator#setReorderCodes
0731      * @see UScriptCode
0732      * @see UColReorderCode
0733      * @stable ICU 4.8
0734      */
0735     static int32_t U_EXPORT2 getEquivalentReorderCodes(int32_t reorderCode,
0736                                 int32_t* dest,
0737                                 int32_t destCapacity,
0738                                 UErrorCode& status);
0739 
0740     /**
0741      * Get name of the object for the desired Locale, in the desired language
0742      * @param objectLocale must be from getAvailableLocales
0743      * @param displayLocale specifies the desired locale for output
0744      * @param name the fill-in parameter of the return value
0745      * @return display-able name of the object for the object locale in the
0746      *         desired language
0747      * @stable ICU 2.0
0748      */
0749     static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
0750                                          const Locale& displayLocale,
0751                                          UnicodeString& name);
0752 
0753     /**
0754     * Get name of the object for the desired Locale, in the language of the
0755     * default locale.
0756     * @param objectLocale must be from getAvailableLocales
0757     * @param name the fill-in parameter of the return value
0758     * @return name of the object for the desired locale in the default language
0759     * @stable ICU 2.0
0760     */
0761     static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
0762                                          UnicodeString& name);
0763 
0764     /**
0765      * Get the set of Locales for which Collations are installed.
0766      *
0767      * <p>Note this does not include locales supported by registered collators.
0768      * If collators might have been registered, use the overload of getAvailableLocales
0769      * that returns a StringEnumeration.</p>
0770      *
0771      * @param count the output parameter of number of elements in the locale list
0772      * @return the list of available locales for which collations are installed
0773      * @stable ICU 2.0
0774      */
0775     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
0776 
0777     /**
0778      * Return a StringEnumeration over the locales available at the time of the call,
0779      * including registered locales.  If a severe error occurs (such as out of memory
0780      * condition) this will return null. If there is no locale data, an empty enumeration
0781      * will be returned.
0782      * @return a StringEnumeration over the locales available at the time of the call
0783      * @stable ICU 2.6
0784      */
0785     static StringEnumeration* U_EXPORT2 getAvailableLocales();
0786 
0787     /**
0788      * Create a string enumerator of all possible keywords that are relevant to
0789      * collation. At this point, the only recognized keyword for this
0790      * service is "collation".
0791      * @param status input-output error code
0792      * @return a string enumeration over locale strings. The caller is
0793      * responsible for closing the result.
0794      * @stable ICU 3.0
0795      */
0796     static StringEnumeration* U_EXPORT2 getKeywords(UErrorCode& status);
0797 
0798     /**
0799      * Given a keyword, create a string enumeration of all values
0800      * for that keyword that are currently in use.
0801      * @param keyword a particular keyword as enumerated by
0802      * ucol_getKeywords. If any other keyword is passed in, status is set
0803      * to U_ILLEGAL_ARGUMENT_ERROR.
0804      * @param status input-output error code
0805      * @return a string enumeration over collation keyword values, or nullptr
0806      * upon error. The caller is responsible for deleting the result.
0807      * @stable ICU 3.0
0808      */
0809     static StringEnumeration* U_EXPORT2 getKeywordValues(const char *keyword, UErrorCode& status);
0810 
0811     /**
0812      * Given a key and a locale, returns an array of string values in a preferred
0813      * order that would make a difference. These are all and only those values where
0814      * the open (creation) of the service with the locale formed from the input locale
0815      * plus input keyword and that value has different behavior than creation with the
0816      * input locale alone.
0817      * @param keyword        one of the keys supported by this service.  For now, only
0818      *                      "collation" is supported.
0819      * @param locale        the locale
0820      * @param commonlyUsed  if set to true it will return only commonly used values
0821      *                      with the given locale in preferred order.  Otherwise,
0822      *                      it will return all the available values for the locale.
0823      * @param status ICU status
0824      * @return a string enumeration over keyword values for the given key and the locale.
0825      * @stable ICU 4.2
0826      */
0827     static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* keyword, const Locale& locale,
0828                                                                     UBool commonlyUsed, UErrorCode& status);
0829 
0830     /**
0831      * Return the functionally equivalent locale for the given
0832      * requested locale, with respect to given keyword, for the
0833      * collation service.  If two locales return the same result, then
0834      * collators instantiated for these locales will behave
0835      * equivalently.  The converse is not always true; two collators
0836      * may in fact be equivalent, but return different results, due to
0837      * internal details.  The return result has no other meaning than
0838      * that stated above, and implies nothing as to the relationship
0839      * between the two locales.  This is intended for use by
0840      * applications who wish to cache collators, or otherwise reuse
0841      * collators when possible.  The functional equivalent may change
0842      * over time.  For more information, please see the <a
0843      * href="https://unicode-org.github.io/icu/userguide/locale#locales-and-services">
0844      * Locales and Services</a> section of the ICU User Guide.
0845      * @param keyword a particular keyword as enumerated by
0846      * ucol_getKeywords.
0847      * @param locale the requested locale
0848      * @param isAvailable reference to a fillin parameter that
0849      * indicates whether the requested locale was 'available' to the
0850      * collation service. A locale is defined as 'available' if it
0851      * physically exists within the collation locale data.
0852      * @param status reference to input-output error code
0853      * @return the functionally equivalent collation locale, or the root
0854      * locale upon error.
0855      * @stable ICU 3.0
0856      */
0857     static Locale U_EXPORT2 getFunctionalEquivalent(const char* keyword, const Locale& locale,
0858                                           UBool& isAvailable, UErrorCode& status);
0859 
0860 #if !UCONFIG_NO_SERVICE
0861     /**
0862      * Register a new Collator.  The collator will be adopted.
0863      * Because ICU may choose to cache collators internally, this must be
0864      * called at application startup, prior to any calls to
0865      * Collator::createInstance to avoid undefined behavior.
0866      * @param toAdopt the Collator instance to be adopted
0867      * @param locale the locale with which the collator will be associated
0868      * @param status the in/out status code, no special meanings are assigned
0869      * @return a registry key that can be used to unregister this collator
0870      * @stable ICU 2.6
0871      */
0872     static URegistryKey U_EXPORT2 registerInstance(Collator* toAdopt, const Locale& locale, UErrorCode& status);
0873 
0874     /**
0875      * Register a new CollatorFactory.  The factory will be adopted.
0876      * Because ICU may choose to cache collators internally, this must be
0877      * called at application startup, prior to any calls to
0878      * Collator::createInstance to avoid undefined behavior.
0879      * @param toAdopt the CollatorFactory instance to be adopted
0880      * @param status the in/out status code, no special meanings are assigned
0881      * @return a registry key that can be used to unregister this collator
0882      * @stable ICU 2.6
0883      */
0884     static URegistryKey U_EXPORT2 registerFactory(CollatorFactory* toAdopt, UErrorCode& status);
0885 
0886     /**
0887      * Unregister a previously-registered Collator or CollatorFactory
0888      * using the key returned from the register call.  Key becomes
0889      * invalid after a successful call and should not be used again.
0890      * The object corresponding to the key will be deleted.
0891      * Because ICU may choose to cache collators internally, this should
0892      * be called during application shutdown, after all calls to
0893      * Collator::createInstance to avoid undefined behavior.
0894      * @param key the registry key returned by a previous call to registerInstance
0895      * @param status the in/out status code, no special meanings are assigned
0896      * @return true if the collator for the key was successfully unregistered
0897      * @stable ICU 2.6
0898      */
0899     static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
0900 #endif /* UCONFIG_NO_SERVICE */
0901 
0902     /**
0903      * Gets the version information for a Collator.
0904      * @param info the version # information, the result will be filled in
0905      * @stable ICU 2.0
0906      */
0907     virtual void getVersion(UVersionInfo info) const = 0;
0908 
0909     /**
0910      * Returns a unique class ID POLYMORPHICALLY. Pure virtual method.
0911      * This method is to implement a simple version of RTTI, since not all C++
0912      * compilers support genuine RTTI. Polymorphic operator==() and clone()
0913      * methods call this method.
0914      * @return The class ID for this object. All objects of a given class have
0915      *         the same class ID.  Objects of other classes have different class
0916      *         IDs.
0917      * @stable ICU 2.0
0918      */
0919     virtual UClassID getDynamicClassID() const override = 0;
0920 
0921     /**
0922      * Universal attribute setter
0923      * @param attr attribute type
0924      * @param value attribute value
0925      * @param status to indicate whether the operation went on smoothly or
0926      *        there were errors
0927      * @stable ICU 2.2
0928      */
0929     virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
0930                               UErrorCode &status) = 0;
0931 
0932     /**
0933      * Universal attribute getter
0934      * @param attr attribute type
0935      * @param status to indicate whether the operation went on smoothly or
0936      *        there were errors
0937      * @return attribute value
0938      * @stable ICU 2.2
0939      */
0940     virtual UColAttributeValue getAttribute(UColAttribute attr,
0941                                             UErrorCode &status) const = 0;
0942 
0943     /**
0944      * Sets the variable top to the top of the specified reordering group.
0945      * The variable top determines the highest-sorting character
0946      * which is affected by UCOL_ALTERNATE_HANDLING.
0947      * If that attribute is set to UCOL_NON_IGNORABLE, then the variable top has no effect.
0948      *
0949      * The base class implementation sets U_UNSUPPORTED_ERROR.
0950      * @param group one of UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION,
0951      *              UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_CURRENCY;
0952      *              or UCOL_REORDER_CODE_DEFAULT to restore the default max variable group
0953      * @param errorCode Standard ICU error code. Its input value must
0954      *                  pass the U_SUCCESS() test, or else the function returns
0955      *                  immediately. Check for U_FAILURE() on output or use with
0956      *                  function chaining. (See User Guide for details.)
0957      * @return *this
0958      * @see getMaxVariable
0959      * @stable ICU 53
0960      */
0961     virtual Collator &setMaxVariable(UColReorderCode group, UErrorCode &errorCode);
0962 
0963     /**
0964      * Returns the maximum reordering group whose characters are affected by UCOL_ALTERNATE_HANDLING.
0965      *
0966      * The base class implementation returns UCOL_REORDER_CODE_PUNCTUATION.
0967      * @return the maximum variable reordering group.
0968      * @see setMaxVariable
0969      * @stable ICU 53
0970      */
0971     virtual UColReorderCode getMaxVariable() const;
0972 
0973 #ifndef U_FORCE_HIDE_DEPRECATED_API
0974     /**
0975      * Sets the variable top to the primary weight of the specified string.
0976      *
0977      * Beginning with ICU 53, the variable top is pinned to
0978      * the top of one of the supported reordering groups,
0979      * and it must not be beyond the last of those groups.
0980      * See setMaxVariable().
0981      * @param varTop one or more (if contraction) char16_ts to which the variable top should be set
0982      * @param len length of variable top string. If -1 it is considered to be zero terminated.
0983      * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
0984      *    U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
0985      *    U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
0986      *    the last reordering group supported by setMaxVariable()
0987      * @return variable top primary weight
0988      * @deprecated ICU 53 Call setMaxVariable() instead.
0989      */
0990     virtual uint32_t setVariableTop(const char16_t *varTop, int32_t len, UErrorCode &status) = 0;
0991 
0992     /**
0993      * Sets the variable top to the primary weight of the specified string.
0994      *
0995      * Beginning with ICU 53, the variable top is pinned to
0996      * the top of one of the supported reordering groups,
0997      * and it must not be beyond the last of those groups.
0998      * See setMaxVariable().
0999      * @param varTop a UnicodeString size 1 or more (if contraction) of char16_ts to which the variable top should be set
1000      * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
1001      *    U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
1002      *    U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
1003      *    the last reordering group supported by setMaxVariable()
1004      * @return variable top primary weight
1005      * @deprecated ICU 53 Call setMaxVariable() instead.
1006      */
1007     virtual uint32_t setVariableTop(const UnicodeString &varTop, UErrorCode &status) = 0;
1008 
1009     /**
1010      * Sets the variable top to the specified primary weight.
1011      *
1012      * Beginning with ICU 53, the variable top is pinned to
1013      * the top of one of the supported reordering groups,
1014      * and it must not be beyond the last of those groups.
1015      * See setMaxVariable().
1016      * @param varTop primary weight, as returned by setVariableTop or ucol_getVariableTop
1017      * @param status error code
1018      * @deprecated ICU 53 Call setMaxVariable() instead.
1019      */
1020     virtual void setVariableTop(uint32_t varTop, UErrorCode &status) = 0;
1021 #endif  // U_FORCE_HIDE_DEPRECATED_API
1022 
1023     /**
1024      * Gets the variable top value of a Collator.
1025      * @param status error code (not changed by function). If error code is set, the return value is undefined.
1026      * @return the variable top primary weight
1027      * @see getMaxVariable
1028      * @stable ICU 2.0
1029      */
1030     virtual uint32_t getVariableTop(UErrorCode &status) const = 0;
1031 
1032     /**
1033      * Get a UnicodeSet that contains all the characters and sequences
1034      * tailored in this collator.
1035      * @param status      error code of the operation
1036      * @return a pointer to a UnicodeSet object containing all the
1037      *         code points and sequences that may sort differently than
1038      *         in the root collator. The object must be disposed of by using delete
1039      * @stable ICU 2.4
1040      */
1041     virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
1042 
1043 #ifndef U_FORCE_HIDE_DEPRECATED_API
1044     /**
1045      * Same as clone().
1046      * The base class implementation simply calls clone().
1047      * @return a copy of this object, owned by the caller
1048      * @see clone()
1049      * @deprecated ICU 50 no need to have two methods for cloning
1050      */
1051     virtual Collator* safeClone() const;
1052 #endif  // U_FORCE_HIDE_DEPRECATED_API
1053 
1054     /**
1055      * Get the sort key as an array of bytes from a UnicodeString.
1056      * Sort key byte arrays are zero-terminated and can be compared using
1057      * strcmp().
1058      *
1059      * Note that sort keys are often less efficient than simply doing comparison.
1060      * For more details, see the ICU User Guide.
1061      *
1062      * @param source string to be processed.
1063      * @param result buffer to store result in. If nullptr, number of bytes needed
1064      *        will be returned.
1065      * @param resultLength length of the result buffer. If if not enough the
1066      *        buffer will be filled to capacity.
1067      * @return Number of bytes needed for storing the sort key
1068      * @stable ICU 2.2
1069      */
1070     virtual int32_t getSortKey(const UnicodeString& source,
1071                               uint8_t* result,
1072                               int32_t resultLength) const = 0;
1073 
1074     /**
1075      * Get the sort key as an array of bytes from a char16_t buffer.
1076      * Sort key byte arrays are zero-terminated and can be compared using
1077      * strcmp().
1078      *
1079      * Note that sort keys are often less efficient than simply doing comparison.
1080      * For more details, see the ICU User Guide.
1081      *
1082      * @param source string to be processed.
1083      * @param sourceLength length of string to be processed.
1084      *        If -1, the string is 0 terminated and length will be decided by the
1085      *        function.
1086      * @param result buffer to store result in. If nullptr, number of bytes needed
1087      *        will be returned.
1088      * @param resultLength length of the result buffer. If if not enough the
1089      *        buffer will be filled to capacity.
1090      * @return Number of bytes needed for storing the sort key
1091      * @stable ICU 2.2
1092      */
1093     virtual int32_t getSortKey(const char16_t*source, int32_t sourceLength,
1094                                uint8_t*result, int32_t resultLength) const = 0;
1095 
1096     /**
1097      * Produce a bound for a given sortkey and a number of levels.
1098      * Return value is always the number of bytes needed, regardless of
1099      * whether the result buffer was big enough or even valid.<br>
1100      * Resulting bounds can be used to produce a range of strings that are
1101      * between upper and lower bounds. For example, if bounds are produced
1102      * for a sortkey of string "smith", strings between upper and lower
1103      * bounds with one level would include "Smith", "SMITH", "sMiTh".<br>
1104      * There are two upper bounds that can be produced. If UCOL_BOUND_UPPER
1105      * is produced, strings matched would be as above. However, if bound
1106      * produced using UCOL_BOUND_UPPER_LONG is used, the above example will
1107      * also match "Smithsonian" and similar.<br>
1108      * For more on usage, see example in cintltst/capitst.c in procedure
1109      * TestBounds.
1110      * Sort keys may be compared using <TT>strcmp</TT>.
1111      * @param source The source sortkey.
1112      * @param sourceLength The length of source, or -1 if null-terminated.
1113      *                     (If an unmodified sortkey is passed, it is always null
1114      *                      terminated).
1115      * @param boundType Type of bound required. It can be UCOL_BOUND_LOWER, which
1116      *                  produces a lower inclusive bound, UCOL_BOUND_UPPER, that
1117      *                  produces upper bound that matches strings of the same length
1118      *                  or UCOL_BOUND_UPPER_LONG that matches strings that have the
1119      *                  same starting substring as the source string.
1120      * @param noOfLevels  Number of levels required in the resulting bound (for most
1121      *                    uses, the recommended value is 1). See users guide for
1122      *                    explanation on number of levels a sortkey can have.
1123      * @param result A pointer to a buffer to receive the resulting sortkey.
1124      * @param resultLength The maximum size of result.
1125      * @param status Used for returning error code if something went wrong. If the
1126      *               number of levels requested is higher than the number of levels
1127      *               in the source key, a warning (U_SORT_KEY_TOO_SHORT_WARNING) is
1128      *               issued.
1129      * @return The size needed to fully store the bound.
1130      * @see ucol_keyHashCode
1131      * @stable ICU 2.1
1132      */
1133     static int32_t U_EXPORT2 getBound(const uint8_t       *source,
1134             int32_t             sourceLength,
1135             UColBoundMode       boundType,
1136             uint32_t            noOfLevels,
1137             uint8_t             *result,
1138             int32_t             resultLength,
1139             UErrorCode          &status);
1140 
1141 
1142 protected:
1143 
1144     // Collator protected constructors -------------------------------------
1145 
1146     /**
1147     * Default constructor.
1148     * Constructor is different from the old default Collator constructor.
1149     * The task for determining the default collation strength and normalization
1150     * mode is left to the child class.
1151     * @stable ICU 2.0
1152     */
1153     Collator();
1154 
1155 #ifndef U_HIDE_DEPRECATED_API
1156     /**
1157     * Constructor.
1158     * Empty constructor, does not handle the arguments.
1159     * This constructor is done for backward compatibility with 1.7 and 1.8.
1160     * The task for handling the argument collation strength and normalization
1161     * mode is left to the child class.
1162     * @param collationStrength collation strength
1163     * @param decompositionMode
1164     * @deprecated ICU 2.4. Subclasses should use the default constructor
1165     * instead and handle the strength and normalization mode themselves.
1166     */
1167     Collator(UCollationStrength collationStrength,
1168              UNormalizationMode decompositionMode);
1169 #endif  /* U_HIDE_DEPRECATED_API */
1170 
1171     /**
1172     * Copy constructor.
1173     * @param other Collator object to be copied from
1174     * @stable ICU 2.0
1175     */
1176     Collator(const Collator& other);
1177 
1178 public:
1179    /**
1180     * Used internally by registration to define the requested and valid locales.
1181     * @param requestedLocale the requested locale
1182     * @param validLocale the valid locale
1183     * @param actualLocale the actual locale
1184     * @internal
1185     */
1186     virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
1187 
1188     /** Get the short definition string for a collator. This internal API harvests the collator's
1189      *  locale and the attribute set and produces a string that can be used for opening
1190      *  a collator with the same attributes using the ucol_openFromShortString API.
1191      *  This string will be normalized.
1192      *  The structure and the syntax of the string is defined in the "Naming collators"
1193      *  section of the users guide:
1194      *  https://unicode-org.github.io/icu/userguide/collation/concepts#collator-naming-scheme
1195      *  This function supports preflighting.
1196      *
1197      *  This is internal, and intended to be used with delegate converters.
1198      *
1199      *  @param locale a locale that will appear as a collators locale in the resulting
1200      *                short string definition. If nullptr, the locale will be harvested
1201      *                from the collator.
1202      *  @param buffer space to hold the resulting string
1203      *  @param capacity capacity of the buffer
1204      *  @param status for returning errors. All the preflighting errors are featured
1205      *  @return length of the resulting string
1206      *  @see ucol_openFromShortString
1207      *  @see ucol_normalizeShortDefinitionString
1208      *  @see ucol_getShortDefinitionString
1209      *  @internal
1210      */
1211     virtual int32_t internalGetShortDefinitionString(const char *locale,
1212                                                      char *buffer,
1213                                                      int32_t capacity,
1214                                                      UErrorCode &status) const;
1215 
1216     /**
1217      * Implements ucol_strcollUTF8().
1218      * @internal
1219      */
1220     virtual UCollationResult internalCompareUTF8(
1221             const char *left, int32_t leftLength,
1222             const char *right, int32_t rightLength,
1223             UErrorCode &errorCode) const;
1224 
1225     /**
1226      * Implements ucol_nextSortKeyPart().
1227      * @internal
1228      */
1229     virtual int32_t
1230     internalNextSortKeyPart(
1231             UCharIterator *iter, uint32_t state[2],
1232             uint8_t *dest, int32_t count, UErrorCode &errorCode) const;
1233 
1234 #ifndef U_HIDE_INTERNAL_API
1235     /** @internal */
1236     static inline Collator *fromUCollator(UCollator *uc) {
1237         return reinterpret_cast<Collator *>(uc);
1238     }
1239     /** @internal */
1240     static inline const Collator *fromUCollator(const UCollator *uc) {
1241         return reinterpret_cast<const Collator *>(uc);
1242     }
1243     /** @internal */
1244     inline UCollator *toUCollator() {
1245         return reinterpret_cast<UCollator *>(this);
1246     }
1247     /** @internal */
1248     inline const UCollator *toUCollator() const {
1249         return reinterpret_cast<const UCollator *>(this);
1250     }
1251 #endif  // U_HIDE_INTERNAL_API
1252 
1253 private:
1254     /**
1255      * Assignment operator. Private for now.
1256      */
1257     Collator& operator=(const Collator& other) = delete;
1258 
1259     friend class CFactory;
1260     friend class SimpleCFactory;
1261     friend class ICUCollatorFactory;
1262     friend class ICUCollatorService;
1263     static Collator* makeInstance(const Locale& desiredLocale,
1264                                   UErrorCode& status);
1265 
1266 #ifndef U_HIDE_DRAFT_API
1267     /**
1268      * Function object for performing comparisons using a Collator.
1269      * @internal
1270      */
1271     template <template <typename...> typename Compare, UCollationResult result>
1272     class Predicate {
1273       public:
1274         explicit Predicate(const Collator& parent) : collator(parent) {}
1275 
1276         template <
1277             typename T, typename U,
1278             typename = std::enable_if_t<ConvertibleToU16StringView<T> && ConvertibleToU16StringView<U>>>
1279         bool operator()(const T& lhs, const U& rhs) const {
1280             UErrorCode status = U_ZERO_ERROR;
1281             return compare(
1282                 collator.compare(
1283                     UnicodeString::readOnlyAlias(lhs),
1284                     UnicodeString::readOnlyAlias(rhs),
1285                     status),
1286                 result);
1287         }
1288 
1289         bool operator()(std::string_view lhs, std::string_view rhs) const {
1290             UErrorCode status = U_ZERO_ERROR;
1291             return compare(collator.compareUTF8(lhs, rhs, status), result);
1292         }
1293 
1294 #if defined(__cpp_char8_t)
1295         bool operator()(std::u8string_view lhs, std::u8string_view rhs) const {
1296             UErrorCode status = U_ZERO_ERROR;
1297             return compare(collator.compareUTF8(lhs, rhs, status), result);
1298         }
1299 #endif
1300 
1301       private:
1302         const Collator& collator;
1303         static constexpr Compare<UCollationResult> compare{};
1304     };
1305 #endif  // U_HIDE_DRAFT_API
1306 };
1307 
1308 #if !UCONFIG_NO_SERVICE
1309 /**
1310  * A factory, used with registerFactory, the creates multiple collators and provides
1311  * display names for them.  A factory supports some number of locales-- these are the
1312  * locales for which it can create collators.  The factory can be visible, in which
1313  * case the supported locales will be enumerated by getAvailableLocales, or invisible,
1314  * in which they are not.  Invisible locales are still supported, they are just not
1315  * listed by getAvailableLocales.
1316  * <p>
1317  * If standard locale display names are sufficient, Collator instances can
1318  * be registered using registerInstance instead.</p>
1319  * <p>
1320  * Note: if the collators are to be used from C APIs, they must be instances
1321  * of RuleBasedCollator.</p>
1322  *
1323  * @stable ICU 2.6
1324  */
1325 class U_I18N_API CollatorFactory : public UObject {
1326 public:
1327 
1328     /**
1329      * Destructor
1330      * @stable ICU 3.0
1331      */
1332     virtual ~CollatorFactory();
1333 
1334     /**
1335      * Return true if this factory is visible.  Default is true.
1336      * If not visible, the locales supported by this factory will not
1337      * be listed by getAvailableLocales.
1338      * @return true if the factory is visible.
1339      * @stable ICU 2.6
1340      */
1341     virtual UBool visible() const;
1342 
1343     /**
1344      * Return a collator for the provided locale.  If the locale
1345      * is not supported, return nullptr.
1346      * @param loc the locale identifying the collator to be created.
1347      * @return a new collator if the locale is supported, otherwise nullptr.
1348      * @stable ICU 2.6
1349      */
1350     virtual Collator* createCollator(const Locale& loc) = 0;
1351 
1352     /**
1353      * Return the name of the collator for the objectLocale, localized for the displayLocale.
1354      * If objectLocale is not supported, or the factory is not visible, set the result string
1355      * to bogus.
1356      * @param objectLocale the locale identifying the collator
1357      * @param displayLocale the locale for which the display name of the collator should be localized
1358      * @param result an output parameter for the display name, set to bogus if not supported.
1359      * @return the display name
1360      * @stable ICU 2.6
1361      */
1362     virtual  UnicodeString& getDisplayName(const Locale& objectLocale,
1363                                            const Locale& displayLocale,
1364                                            UnicodeString& result);
1365 
1366     /**
1367      * Return an array of all the locale names directly supported by this factory.
1368      * The number of names is returned in count.  This array is owned by the factory.
1369      * Its contents must never change.
1370      * @param count output parameter for the number of locales supported by the factory
1371      * @param status the in/out error code
1372      * @return a pointer to an array of count UnicodeStrings.
1373      * @stable ICU 2.6
1374      */
1375     virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) = 0;
1376 };
1377 #endif /* UCONFIG_NO_SERVICE */
1378 
1379 // Collator inline methods -----------------------------------------------
1380 
1381 U_NAMESPACE_END
1382 
1383 #endif /* #if !UCONFIG_NO_COLLATION */
1384 
1385 #endif /* U_SHOW_CPLUSPLUS_API */
1386 
1387 #endif