1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 1996-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
12 * \brief C++ API: Collation Service.
18 * Created by: Helena Shih
20 * Modification History:
22 * Date Name Description
23 * 02/5/97 aliu Modified createDefault to load collation data from
24 * binary files when possible. Added related methods
25 * createCollationFromFile, chopLocale, createPathName.
26 * 02/11/97 aliu Added members addToCache, findInCache, and fgCache.
27 * 02/12/97 aliu Modified to create objects from RuleBasedCollator cache.
28 * Moved cache out of Collation class.
29 * 02/13/97 aliu Moved several methods out of this class and into
30 * RuleBasedCollator, with modifications. Modified
31 * createDefault() to call new RuleBasedCollator(Locale&)
32 * constructor. General clean up and documentation.
33 * 02/20/97 helena Added clone, operator==, operator!=, operator=, copy
34 * constructor and getDynamicClassID.
35 * 03/25/97 helena Updated with platform independent data types.
36 * 05/06/97 helena Added memory allocation error detection.
37 * 06/20/97 helena Java class name change.
38 * 09/03/97 helena Added createCollationKeyValues().
39 * 02/10/98 damiba Added compare() with length as parameter.
40 * 04/23/99 stephen Removed EDecompositionMode, merged with
42 * 11/02/99 helena Collator performance enhancements. Eliminates the
43 * UnicodeString construction and special case for NO_OP.
44 * 11/23/99 srl More performance enhancements. Inlining of
46 * 05/15/00 helena Added version information API.
47 * 01/29/01 synwee Modified into a C++ wrapper which calls C apis
49 * 2012-2014 markus Rewritten in C++ again.
55 #include "unicode/utypes.h"
57 #if !UCONFIG_NO_COLLATION
59 #include "unicode/uobject.h"
60 #include "unicode/ucol.h"
61 #include "unicode/normlzr.h"
62 #include "unicode/locid.h"
63 #include "unicode/uniset.h"
64 #include "unicode/umisc.h"
65 #include "unicode/uiter.h"
66 #include "unicode/stringpiece.h"
70 class StringEnumeration;
72 #if !UCONFIG_NO_SERVICE
76 class CollatorFactory;
85 * The <code>Collator</code> class performs locale-sensitive string
87 * You use this class to build searching and sorting routines for natural
90 * <code>Collator</code> is an abstract base class. Subclasses implement
91 * specific collation strategies. One subclass,
92 * <code>RuleBasedCollator</code>, is currently provided and is applicable
93 * to a wide set of languages. Other subclasses may be created to handle more
96 * Like other locale-sensitive classes, you can use the static factory method,
97 * <code>createInstance</code>, to obtain the appropriate
98 * <code>Collator</code> object for a given locale. You will only need to
99 * look at the subclasses of <code>Collator</code> if you need to
100 * understand the details of a particular collation strategy or if you need to
101 * modify that strategy.
103 * The following example shows how to compare two strings using the
104 * <code>Collator</code> for the default locale.
105 * \htmlonly<blockquote>\endhtmlonly
108 * // Compare two strings in the default locale
109 * UErrorCode success = U_ZERO_ERROR;
110 * Collator* myCollator = Collator::createInstance(success);
111 * if (myCollator->compare("abc", "ABC") < 0)
112 * cout << "abc is less than ABC" << endl;
114 * cout << "abc is greater than or equal to ABC" << endl;
117 * \htmlonly</blockquote>\endhtmlonly
119 * You can set a <code>Collator</code>'s <em>strength</em> attribute to
120 * determine the level of difference considered significant in comparisons.
121 * Five strengths are provided: <code>PRIMARY</code>, <code>SECONDARY</code>,
122 * <code>TERTIARY</code>, <code>QUATERNARY</code> and <code>IDENTICAL</code>.
123 * The exact assignment of strengths to language features is locale dependent.
124 * For example, in Czech, "e" and "f" are considered primary differences,
125 * while "e" and "\u00EA" are secondary differences, "e" and "E" are tertiary
126 * differences and "e" and "e" are identical. The following shows how both case
127 * and accents could be ignored for US English.
128 * \htmlonly<blockquote>\endhtmlonly
131 * //Get the Collator for US English and set its strength to PRIMARY
132 * UErrorCode success = U_ZERO_ERROR;
133 * Collator* usCollator = Collator::createInstance(Locale::getUS(), success);
134 * usCollator->setStrength(Collator::PRIMARY);
135 * if (usCollator->compare("abc", "ABC") == 0)
136 * cout << "'abc' and 'ABC' strings are equivalent with strength PRIMARY" << endl;
139 * \htmlonly</blockquote>\endhtmlonly
141 * The <code>getSortKey</code> methods
142 * convert a string to a series of bytes that can be compared bitwise against
143 * other sort keys using <code>strcmp()</code>. Sort keys are written as
144 * zero-terminated byte strings.
146 * Another set of APIs returns a <code>CollationKey</code> object that wraps
147 * the sort key bytes instead of returning the bytes themselves.
150 * <strong>Note:</strong> <code>Collator</code>s with different Locale,
151 * and CollationStrength settings will return different sort
152 * orders for the same set of strings. Locales have specific collation rules,
153 * and the way in which secondary and tertiary differences are taken into
154 * account, for example, will result in a different sorting order for same
157 * @see RuleBasedCollator
159 * @see CollationElementIterator
162 * @version 2.0 11/15/01
165 class U_I18N_API Collator : public UObject {
168 // Collator public enums -----------------------------------------------
171 * Base letter represents a primary difference. Set comparison level to
172 * PRIMARY to ignore secondary and tertiary differences.<br>
173 * Use this to set the strength of a Collator object.<br>
174 * Example of primary difference, "abc" < "abd"
176 * Diacritical differences on the same base letter represent a secondary
177 * difference. Set comparison level to SECONDARY to ignore tertiary
178 * differences. Use this to set the strength of a Collator object.<br>
179 * Example of secondary difference, "ä" >> "a".
181 * Uppercase and lowercase versions of the same character represents a
182 * tertiary difference. Set comparison level to TERTIARY to include all
183 * comparison differences. Use this to set the strength of a Collator
185 * Example of tertiary difference, "abc" <<< "ABC".
187 * Two characters are considered "identical" when they have the same unicode
189 * For example, "ä" == "ä".
191 * UCollationStrength is also used to determine the strength of sort keys
192 * generated from Collator objects.
195 enum ECollationStrength
197 PRIMARY = UCOL_PRIMARY, // 0
198 SECONDARY = UCOL_SECONDARY, // 1
199 TERTIARY = UCOL_TERTIARY, // 2
200 QUATERNARY = UCOL_QUATERNARY, // 3
201 IDENTICAL = UCOL_IDENTICAL // 15
205 // Cannot use #ifndef U_HIDE_DEPRECATED_API for the following, it is
206 // used by virtual methods that cannot have that conditional.
208 * LESS is returned if source string is compared to be less than target
209 * string in the compare() method.
210 * EQUAL is returned if source string is compared to be equal to target
211 * string in the compare() method.
212 * GREATER is returned if source string is compared to be greater than
213 * target string in the compare() method.
214 * @see Collator#compare
215 * @deprecated ICU 2.6. Use C enum UCollationResult defined in ucol.h
217 enum EComparisonResult
219 LESS = UCOL_LESS, // -1
220 EQUAL = UCOL_EQUAL, // 0
221 GREATER = UCOL_GREATER // 1
224 // Collator public destructor -----------------------------------------
232 // Collator public methods --------------------------------------------
235 * Returns TRUE if "other" is the same as "this".
237 * The base class implementation returns TRUE if "other" has the same type/class as "this":
238 * <code>typeid(*this) == typeid(other)</code>.
240 * Subclass implementations should do something like the following:
242 * if (this == &other) { return TRUE; }
243 * if (!Collator::operator==(other)) { return FALSE; } // not the same class
245 * const MyCollator &o = (const MyCollator&)other;
246 * (compare this vs. o's subclass fields)
248 * @param other Collator object to be compared
249 * @return TRUE if other is the same as this.
252 virtual UBool operator==(const Collator& other) const;
255 * Returns true if "other" is not the same as "this".
256 * Calls ! operator==(const Collator&) const which works for all subclasses.
257 * @param other Collator object to be compared
258 * @return TRUE if other is not the same as this.
261 virtual UBool operator!=(const Collator& other) const;
264 * Makes a copy of this object.
265 * @return a copy of this object, owned by the caller
268 virtual Collator* clone(void) const = 0;
271 * Creates the Collator object for the current default locale.
272 * The default locale is determined by Locale::getDefault.
273 * The UErrorCode& err parameter is used to return status information to the user.
274 * To check whether the construction succeeded or not, you should check the
275 * value of U_SUCCESS(err). If you wish more detailed information, you can
276 * check for informational error results which still indicate success.
277 * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
278 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
279 * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
280 * used; neither the requested locale nor any of its fall back locales
282 * The caller owns the returned object and is responsible for deleting it.
284 * @param err the error code status.
285 * @return the collation object of the default locale.(for example, en_US)
286 * @see Locale#getDefault
289 static Collator* U_EXPORT2 createInstance(UErrorCode& err);
292 * Gets the collation object for the desired locale. The
293 * resource of the desired locale will be loaded.
295 * Locale::getRoot() is the base collation table and all other languages are
296 * built on top of it with additional language-specific modifications.
298 * For some languages, multiple collation types are available;
299 * for example, "de@collation=phonebook".
300 * Starting with ICU 54, collation attributes can be specified via locale keywords as well,
301 * in the old locale extension syntax ("el@colCaseFirst=upper")
302 * or in language tag syntax ("el-u-kf-upper").
303 * See <a href="http://userguide.icu-project.org/collation/api">User Guide: Collation API</a>.
305 * The UErrorCode& err parameter is used to return status information to the user.
306 * To check whether the construction succeeded or not, you should check
307 * the value of U_SUCCESS(err). If you wish more detailed information, you
308 * can check for informational error results which still indicate success.
309 * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
310 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
311 * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
312 * used; neither the requested locale nor any of its fall back locales
315 * The caller owns the returned object and is responsible for deleting it.
316 * @param loc The locale ID for which to open a collator.
317 * @param err the error code status.
318 * @return the created table-based collation object based on the desired
321 * @see ResourceLoader
324 static Collator* U_EXPORT2 createInstance(const Locale& loc, UErrorCode& err);
327 * The comparison function compares the character data stored in two
328 * different strings. Returns information about whether a string is less
329 * than, greater than or equal to another string.
330 * @param source the source string to be compared with.
331 * @param target the string that is to be compared with the source string.
332 * @return Returns a byte value. GREATER if source is greater
333 * than target; EQUAL if source is equal to target; LESS if source is less
335 * @deprecated ICU 2.6 use the overload with UErrorCode &
337 virtual EComparisonResult compare(const UnicodeString& source,
338 const UnicodeString& target) const;
341 * The comparison function compares the character data stored in two
342 * different strings. Returns information about whether a string is less
343 * than, greater than or equal to another string.
344 * @param source the source string to be compared with.
345 * @param target the string that is to be compared with the source string.
346 * @param status possible error code
347 * @return Returns an enum value. UCOL_GREATER if source is greater
348 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
352 virtual UCollationResult compare(const UnicodeString& source,
353 const UnicodeString& target,
354 UErrorCode &status) const = 0;
357 * Does the same thing as compare but limits the comparison to a specified
359 * @param source the source string to be compared with.
360 * @param target the string that is to be compared with the source string.
361 * @param length the length the comparison is limited to
362 * @return Returns a byte value. GREATER if source (up to the specified
363 * length) is greater than target; EQUAL if source (up to specified
364 * length) is equal to target; LESS if source (up to the specified
365 * length) is less than target.
366 * @deprecated ICU 2.6 use the overload with UErrorCode &
368 virtual EComparisonResult compare(const UnicodeString& source,
369 const UnicodeString& target,
370 int32_t length) const;
373 * Does the same thing as compare but limits the comparison to a specified
375 * @param source the source string to be compared with.
376 * @param target the string that is to be compared with the source string.
377 * @param length the length the comparison is limited to
378 * @param status possible error code
379 * @return Returns an enum value. UCOL_GREATER if source (up to the specified
380 * length) is greater than target; UCOL_EQUAL if source (up to specified
381 * length) is equal to target; UCOL_LESS if source (up to the specified
382 * length) is less than target.
385 virtual UCollationResult compare(const UnicodeString& source,
386 const UnicodeString& target,
388 UErrorCode &status) const = 0;
391 * The comparison function compares the character data stored in two
392 * different string arrays. Returns information about whether a string array
393 * is less than, greater than or equal to another string array.
396 * . UChar ABC[] = {0x41, 0x42, 0x43, 0}; // = "ABC"
397 * . UChar abc[] = {0x61, 0x62, 0x63, 0}; // = "abc"
398 * . UErrorCode status = U_ZERO_ERROR;
399 * . Collator *myCollation =
400 * . Collator::createInstance(Locale::getUS(), status);
401 * . if (U_FAILURE(status)) return;
402 * . myCollation->setStrength(Collator::PRIMARY);
403 * . // result would be Collator::EQUAL ("abc" == "ABC")
404 * . // (no primary difference between "abc" and "ABC")
405 * . Collator::EComparisonResult result =
406 * . myCollation->compare(abc, 3, ABC, 3);
407 * . myCollation->setStrength(Collator::TERTIARY);
408 * . // result would be Collator::LESS ("abc" <<< "ABC")
409 * . // (with tertiary difference between "abc" and "ABC")
410 * . result = myCollation->compare(abc, 3, ABC, 3);
412 * @param source the source string array to be compared with.
413 * @param sourceLength the length of the source string array. If this value
414 * is equal to -1, the string array is null-terminated.
415 * @param target the string that is to be compared with the source string.
416 * @param targetLength the length of the target string array. If this value
417 * is equal to -1, the string array is null-terminated.
418 * @return Returns a byte value. GREATER if source is greater than target;
419 * EQUAL if source is equal to target; LESS if source is less than
421 * @deprecated ICU 2.6 use the overload with UErrorCode &
423 virtual EComparisonResult compare(const UChar* source, int32_t sourceLength,
424 const UChar* target, int32_t targetLength)
428 * The comparison function compares the character data stored in two
429 * different string arrays. Returns information about whether a string array
430 * is less than, greater than or equal to another string array.
431 * @param source the source string array to be compared with.
432 * @param sourceLength the length of the source string array. If this value
433 * is equal to -1, the string array is null-terminated.
434 * @param target the string that is to be compared with the source string.
435 * @param targetLength the length of the target string array. If this value
436 * is equal to -1, the string array is null-terminated.
437 * @param status possible error code
438 * @return Returns an enum value. UCOL_GREATER if source is greater
439 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
443 virtual UCollationResult compare(const UChar* source, int32_t sourceLength,
444 const UChar* target, int32_t targetLength,
445 UErrorCode &status) const = 0;
448 * Compares two strings using the Collator.
449 * Returns whether the first one compares less than/equal to/greater than
451 * This version takes UCharIterator input.
452 * @param sIter the first ("source") string iterator
453 * @param tIter the second ("target") string iterator
454 * @param status ICU status
455 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
458 virtual UCollationResult compare(UCharIterator &sIter,
459 UCharIterator &tIter,
460 UErrorCode &status) const;
463 * Compares two UTF-8 strings using the Collator.
464 * Returns whether the first one compares less than/equal to/greater than
466 * This version takes UTF-8 input.
467 * Note that a StringPiece can be implicitly constructed
468 * from a std::string or a NUL-terminated const char * string.
469 * @param source the first UTF-8 string
470 * @param target the second UTF-8 string
471 * @param status ICU status
472 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
475 virtual UCollationResult compareUTF8(const StringPiece &source,
476 const StringPiece &target,
477 UErrorCode &status) const;
480 * Transforms the string into a series of characters that can be compared
481 * with CollationKey::compareTo. It is not possible to restore the original
482 * string from the chars in the sort key.
483 * <p>Use CollationKey::equals or CollationKey::compare to compare the
484 * generated sort keys.
485 * If the source string is null, a null collation key will be returned.
487 * Note that sort keys are often less efficient than simply doing comparison.
488 * For more details, see the ICU User Guide.
490 * @param source the source string to be transformed into a sort key.
491 * @param key the collation key to be filled in
492 * @param status the error code status.
493 * @return the collation key of the string based on the collation rules.
494 * @see CollationKey#compare
497 virtual CollationKey& getCollationKey(const UnicodeString& source,
499 UErrorCode& status) const = 0;
502 * Transforms the string into a series of characters that can be compared
503 * with CollationKey::compareTo. It is not possible to restore the original
504 * string from the chars in the sort key.
505 * <p>Use CollationKey::equals or CollationKey::compare to compare the
506 * generated sort keys.
507 * <p>If the source string is null, a null collation key will be returned.
509 * Note that sort keys are often less efficient than simply doing comparison.
510 * For more details, see the ICU User Guide.
512 * @param source the source string to be transformed into a sort key.
513 * @param sourceLength length of the collation key
514 * @param key the collation key to be filled in
515 * @param status the error code status.
516 * @return the collation key of the string based on the collation rules.
517 * @see CollationKey#compare
520 virtual CollationKey& getCollationKey(const UChar*source,
521 int32_t sourceLength,
523 UErrorCode& status) const = 0;
525 * Generates the hash code for the collation object
528 virtual int32_t hashCode(void) const = 0;
531 * Gets the locale of the Collator
533 * @param type can be either requested, valid or actual locale. For more
534 * information see the definition of ULocDataLocaleType in
536 * @param status the error code status.
537 * @return locale where the collation data lives. If the collator
538 * was instantiated from rules, locale is empty.
539 * @deprecated ICU 2.8 This API is under consideration for revision
542 virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const = 0;
545 * Convenience method for comparing two strings based on the collation rules.
546 * @param source the source string to be compared with.
547 * @param target the target string to be compared with.
548 * @return true if the first string is greater than the second one,
549 * according to the collation rules. false, otherwise.
550 * @see Collator#compare
553 UBool greater(const UnicodeString& source, const UnicodeString& target)
557 * Convenience method for comparing two strings based on the collation rules.
558 * @param source the source string to be compared with.
559 * @param target the target string to be compared with.
560 * @return true if the first string is greater than or equal to the second
561 * one, according to the collation rules. false, otherwise.
562 * @see Collator#compare
565 UBool greaterOrEqual(const UnicodeString& source,
566 const UnicodeString& target) const;
569 * Convenience method for comparing two strings based on the collation rules.
570 * @param source the source string to be compared with.
571 * @param target the target string to be compared with.
572 * @return true if the strings are equal according to the collation rules.
574 * @see Collator#compare
577 UBool equals(const UnicodeString& source, const UnicodeString& target) const;
580 * Determines the minimum strength that will be used in comparison or
582 * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
583 * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
585 * @return the current comparison level.
586 * @see Collator#setStrength
587 * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
589 virtual ECollationStrength getStrength(void) const;
592 * Sets the minimum strength to be used in comparison or transformation.
596 * UErrorCode status = U_ZERO_ERROR;
597 * Collator*myCollation = Collator::createInstance(Locale::getUS(), status);
598 * if (U_FAILURE(status)) return;
599 * myCollation->setStrength(Collator::PRIMARY);
600 * // result will be "abc" == "ABC"
601 * // tertiary differences will be ignored
602 * Collator::ComparisonResult result = myCollation->compare("abc", "ABC");
605 * @see Collator#getStrength
606 * @param newStrength the new comparison level.
607 * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
609 virtual void setStrength(ECollationStrength newStrength);
612 * Retrieves the reordering codes for this collator.
613 * @param dest The array to fill with the script ordering.
614 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the function
615 * will only return the length of the result without writing any codes (pre-flighting).
616 * @param status A reference to an error code value, which must not indicate
617 * a failure before the function call.
618 * @return The length of the script ordering array.
619 * @see ucol_setReorderCodes
620 * @see Collator#getEquivalentReorderCodes
621 * @see Collator#setReorderCodes
623 * @see UColReorderCode
626 virtual int32_t getReorderCodes(int32_t *dest,
627 int32_t destCapacity,
628 UErrorCode& status) const;
631 * Sets the ordering of scripts for this collator.
633 * <p>The reordering codes are a combination of script codes and reorder codes.
634 * @param reorderCodes An array of script codes in the new order. This can be NULL if the
635 * length is also set to 0. An empty array will clear any reordering codes on the collator.
636 * @param reorderCodesLength The length of reorderCodes.
637 * @param status error code
638 * @see ucol_setReorderCodes
639 * @see Collator#getReorderCodes
640 * @see Collator#getEquivalentReorderCodes
642 * @see UColReorderCode
645 virtual void setReorderCodes(const int32_t* reorderCodes,
646 int32_t reorderCodesLength,
647 UErrorCode& status) ;
650 * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
651 * codes will be grouped and must reorder together.
652 * Beginning with ICU 55, scripts only reorder together if they are primary-equal,
653 * for example Hiragana and Katakana.
655 * @param reorderCode The reorder code to determine equivalence for.
656 * @param dest The array to fill with the script equivalence reordering codes.
657 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the
658 * function will only return the length of the result without writing any codes (pre-flighting).
659 * @param status A reference to an error code value, which must not indicate
660 * a failure before the function call.
661 * @return The length of the of the reordering code equivalence array.
662 * @see ucol_setReorderCodes
663 * @see Collator#getReorderCodes
664 * @see Collator#setReorderCodes
666 * @see UColReorderCode
669 static int32_t U_EXPORT2 getEquivalentReorderCodes(int32_t reorderCode,
671 int32_t destCapacity,
675 * Get name of the object for the desired Locale, in the desired langauge
676 * @param objectLocale must be from getAvailableLocales
677 * @param displayLocale specifies the desired locale for output
678 * @param name the fill-in parameter of the return value
679 * @return display-able name of the object for the object locale in the
683 static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
684 const Locale& displayLocale,
685 UnicodeString& name);
688 * Get name of the object for the desired Locale, in the langauge of the
690 * @param objectLocale must be from getAvailableLocales
691 * @param name the fill-in parameter of the return value
692 * @return name of the object for the desired locale in the default language
695 static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
696 UnicodeString& name);
699 * Get the set of Locales for which Collations are installed.
701 * <p>Note this does not include locales supported by registered collators.
702 * If collators might have been registered, use the overload of getAvailableLocales
703 * that returns a StringEnumeration.</p>
705 * @param count the output parameter of number of elements in the locale list
706 * @return the list of available locales for which collations are installed
709 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
712 * Return a StringEnumeration over the locales available at the time of the call,
713 * including registered locales. If a severe error occurs (such as out of memory
714 * condition) this will return null. If there is no locale data, an empty enumeration
716 * @return a StringEnumeration over the locales available at the time of the call
719 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
722 * Create a string enumerator of all possible keywords that are relevant to
723 * collation. At this point, the only recognized keyword for this
724 * service is "collation".
725 * @param status input-output error code
726 * @return a string enumeration over locale strings. The caller is
727 * responsible for closing the result.
730 static StringEnumeration* U_EXPORT2 getKeywords(UErrorCode& status);
733 * Given a keyword, create a string enumeration of all values
734 * for that keyword that are currently in use.
735 * @param keyword a particular keyword as enumerated by
736 * ucol_getKeywords. If any other keyword is passed in, status is set
737 * to U_ILLEGAL_ARGUMENT_ERROR.
738 * @param status input-output error code
739 * @return a string enumeration over collation keyword values, or NULL
740 * upon error. The caller is responsible for deleting the result.
743 static StringEnumeration* U_EXPORT2 getKeywordValues(const char *keyword, UErrorCode& status);
746 * Given a key and a locale, returns an array of string values in a preferred
747 * order that would make a difference. These are all and only those values where
748 * the open (creation) of the service with the locale formed from the input locale
749 * plus input keyword and that value has different behavior than creation with the
750 * input locale alone.
751 * @param keyword one of the keys supported by this service. For now, only
752 * "collation" is supported.
753 * @param locale the locale
754 * @param commonlyUsed if set to true it will return only commonly used values
755 * with the given locale in preferred order. Otherwise,
756 * it will return all the available values for the locale.
757 * @param status ICU status
758 * @return a string enumeration over keyword values for the given key and the locale.
761 static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* keyword, const Locale& locale,
762 UBool commonlyUsed, UErrorCode& status);
765 * Return the functionally equivalent locale for the given
766 * requested locale, with respect to given keyword, for the
767 * collation service. If two locales return the same result, then
768 * collators instantiated for these locales will behave
769 * equivalently. The converse is not always true; two collators
770 * may in fact be equivalent, but return different results, due to
771 * internal details. The return result has no other meaning than
772 * that stated above, and implies nothing as to the relationship
773 * between the two locales. This is intended for use by
774 * applications who wish to cache collators, or otherwise reuse
775 * collators when possible. The functional equivalent may change
776 * over time. For more information, please see the <a
777 * href="http://userguide.icu-project.org/locale#TOC-Locales-and-Services">
778 * Locales and Services</a> section of the ICU User Guide.
779 * @param keyword a particular keyword as enumerated by
781 * @param locale the requested locale
782 * @param isAvailable reference to a fillin parameter that
783 * indicates whether the requested locale was 'available' to the
784 * collation service. A locale is defined as 'available' if it
785 * physically exists within the collation locale data.
786 * @param status reference to input-output error code
787 * @return the functionally equivalent collation locale, or the root
791 static Locale U_EXPORT2 getFunctionalEquivalent(const char* keyword, const Locale& locale,
792 UBool& isAvailable, UErrorCode& status);
794 #if !UCONFIG_NO_SERVICE
796 * Register a new Collator. The collator will be adopted.
797 * Because ICU may choose to cache collators internally, this must be
798 * called at application startup, prior to any calls to
799 * Collator::createInstance to avoid undefined behavior.
800 * @param toAdopt the Collator instance to be adopted
801 * @param locale the locale with which the collator will be associated
802 * @param status the in/out status code, no special meanings are assigned
803 * @return a registry key that can be used to unregister this collator
806 static URegistryKey U_EXPORT2 registerInstance(Collator* toAdopt, const Locale& locale, UErrorCode& status);
809 * Register a new CollatorFactory. The factory will be adopted.
810 * Because ICU may choose to cache collators internally, this must be
811 * called at application startup, prior to any calls to
812 * Collator::createInstance to avoid undefined behavior.
813 * @param toAdopt the CollatorFactory instance to be adopted
814 * @param status the in/out status code, no special meanings are assigned
815 * @return a registry key that can be used to unregister this collator
818 static URegistryKey U_EXPORT2 registerFactory(CollatorFactory* toAdopt, UErrorCode& status);
821 * Unregister a previously-registered Collator or CollatorFactory
822 * using the key returned from the register call. Key becomes
823 * invalid after a successful call and should not be used again.
824 * The object corresponding to the key will be deleted.
825 * Because ICU may choose to cache collators internally, this should
826 * be called during application shutdown, after all calls to
827 * Collator::createInstance to avoid undefined behavior.
828 * @param key the registry key returned by a previous call to registerInstance
829 * @param status the in/out status code, no special meanings are assigned
830 * @return TRUE if the collator for the key was successfully unregistered
833 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
834 #endif /* UCONFIG_NO_SERVICE */
837 * Gets the version information for a Collator.
838 * @param info the version # information, the result will be filled in
841 virtual void getVersion(UVersionInfo info) const = 0;
844 * Returns a unique class ID POLYMORPHICALLY. Pure virtual method.
845 * This method is to implement a simple version of RTTI, since not all C++
846 * compilers support genuine RTTI. Polymorphic operator==() and clone()
847 * methods call this method.
848 * @return The class ID for this object. All objects of a given class have
849 * the same class ID. Objects of other classes have different class
853 virtual UClassID getDynamicClassID(void) const = 0;
856 * Universal attribute setter
857 * @param attr attribute type
858 * @param value attribute value
859 * @param status to indicate whether the operation went on smoothly or
863 virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
864 UErrorCode &status) = 0;
867 * Universal attribute getter
868 * @param attr attribute type
869 * @param status to indicate whether the operation went on smoothly or
871 * @return attribute value
874 virtual UColAttributeValue getAttribute(UColAttribute attr,
875 UErrorCode &status) const = 0;
878 * Sets the variable top to the top of the specified reordering group.
879 * The variable top determines the highest-sorting character
880 * which is affected by UCOL_ALTERNATE_HANDLING.
881 * If that attribute is set to UCOL_NON_IGNORABLE, then the variable top has no effect.
883 * The base class implementation sets U_UNSUPPORTED_ERROR.
884 * @param group one of UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION,
885 * UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_CURRENCY;
886 * or UCOL_REORDER_CODE_DEFAULT to restore the default max variable group
887 * @param errorCode Standard ICU error code. Its input value must
888 * pass the U_SUCCESS() test, or else the function returns
889 * immediately. Check for U_FAILURE() on output or use with
890 * function chaining. (See User Guide for details.)
892 * @see getMaxVariable
895 virtual Collator &setMaxVariable(UColReorderCode group, UErrorCode &errorCode);
898 * Returns the maximum reordering group whose characters are affected by UCOL_ALTERNATE_HANDLING.
900 * The base class implementation returns UCOL_REORDER_CODE_PUNCTUATION.
901 * @return the maximum variable reordering group.
902 * @see setMaxVariable
905 virtual UColReorderCode getMaxVariable() const;
908 * Sets the variable top to the primary weight of the specified string.
910 * Beginning with ICU 53, the variable top is pinned to
911 * the top of one of the supported reordering groups,
912 * and it must not be beyond the last of those groups.
913 * See setMaxVariable().
914 * @param varTop one or more (if contraction) UChars to which the variable top should be set
915 * @param len length of variable top string. If -1 it is considered to be zero terminated.
916 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
917 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
918 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
919 * the last reordering group supported by setMaxVariable()
920 * @return variable top primary weight
921 * @deprecated ICU 53 Call setMaxVariable() instead.
923 virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, UErrorCode &status) = 0;
926 * Sets the variable top to the primary weight of the specified string.
928 * Beginning with ICU 53, the variable top is pinned to
929 * the top of one of the supported reordering groups,
930 * and it must not be beyond the last of those groups.
931 * See setMaxVariable().
932 * @param varTop a UnicodeString size 1 or more (if contraction) of UChars to which the variable top should be set
933 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
934 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
935 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
936 * the last reordering group supported by setMaxVariable()
937 * @return variable top primary weight
938 * @deprecated ICU 53 Call setMaxVariable() instead.
940 virtual uint32_t setVariableTop(const UnicodeString &varTop, UErrorCode &status) = 0;
943 * Sets the variable top to the specified primary weight.
945 * Beginning with ICU 53, the variable top is pinned to
946 * the top of one of the supported reordering groups,
947 * and it must not be beyond the last of those groups.
948 * See setMaxVariable().
949 * @param varTop primary weight, as returned by setVariableTop or ucol_getVariableTop
950 * @param status error code
951 * @deprecated ICU 53 Call setMaxVariable() instead.
953 virtual void setVariableTop(uint32_t varTop, UErrorCode &status) = 0;
956 * Gets the variable top value of a Collator.
957 * @param status error code (not changed by function). If error code is set, the return value is undefined.
958 * @return the variable top primary weight
959 * @see getMaxVariable
962 virtual uint32_t getVariableTop(UErrorCode &status) const = 0;
965 * Get a UnicodeSet that contains all the characters and sequences
966 * tailored in this collator.
967 * @param status error code of the operation
968 * @return a pointer to a UnicodeSet object containing all the
969 * code points and sequences that may sort differently than
970 * in the root collator. The object must be disposed of by using delete
973 virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
977 * The base class implementation simply calls clone().
978 * @return a copy of this object, owned by the caller
980 * @deprecated ICU 50 no need to have two methods for cloning
982 virtual Collator* safeClone(void) const;
985 * Get the sort key as an array of bytes from a UnicodeString.
986 * Sort key byte arrays are zero-terminated and can be compared using
989 * Note that sort keys are often less efficient than simply doing comparison.
990 * For more details, see the ICU User Guide.
992 * @param source string to be processed.
993 * @param result buffer to store result in. If NULL, number of bytes needed
995 * @param resultLength length of the result buffer. If if not enough the
996 * buffer will be filled to capacity.
997 * @return Number of bytes needed for storing the sort key
1000 virtual int32_t getSortKey(const UnicodeString& source,
1002 int32_t resultLength) const = 0;
1005 * Get the sort key as an array of bytes from a UChar buffer.
1006 * Sort key byte arrays are zero-terminated and can be compared using
1009 * Note that sort keys are often less efficient than simply doing comparison.
1010 * For more details, see the ICU User Guide.
1012 * @param source string to be processed.
1013 * @param sourceLength length of string to be processed.
1014 * If -1, the string is 0 terminated and length will be decided by the
1016 * @param result buffer to store result in. If NULL, number of bytes needed
1018 * @param resultLength length of the result buffer. If if not enough the
1019 * buffer will be filled to capacity.
1020 * @return Number of bytes needed for storing the sort key
1023 virtual int32_t getSortKey(const UChar*source, int32_t sourceLength,
1024 uint8_t*result, int32_t resultLength) const = 0;
1027 * Produce a bound for a given sortkey and a number of levels.
1028 * Return value is always the number of bytes needed, regardless of
1029 * whether the result buffer was big enough or even valid.<br>
1030 * Resulting bounds can be used to produce a range of strings that are
1031 * between upper and lower bounds. For example, if bounds are produced
1032 * for a sortkey of string "smith", strings between upper and lower
1033 * bounds with one level would include "Smith", "SMITH", "sMiTh".<br>
1034 * There are two upper bounds that can be produced. If UCOL_BOUND_UPPER
1035 * is produced, strings matched would be as above. However, if bound
1036 * produced using UCOL_BOUND_UPPER_LONG is used, the above example will
1037 * also match "Smithsonian" and similar.<br>
1038 * For more on usage, see example in cintltst/capitst.c in procedure
1040 * Sort keys may be compared using <TT>strcmp</TT>.
1041 * @param source The source sortkey.
1042 * @param sourceLength The length of source, or -1 if null-terminated.
1043 * (If an unmodified sortkey is passed, it is always null
1045 * @param boundType Type of bound required. It can be UCOL_BOUND_LOWER, which
1046 * produces a lower inclusive bound, UCOL_BOUND_UPPER, that
1047 * produces upper bound that matches strings of the same length
1048 * or UCOL_BOUND_UPPER_LONG that matches strings that have the
1049 * same starting substring as the source string.
1050 * @param noOfLevels Number of levels required in the resulting bound (for most
1051 * uses, the recommended value is 1). See users guide for
1052 * explanation on number of levels a sortkey can have.
1053 * @param result A pointer to a buffer to receive the resulting sortkey.
1054 * @param resultLength The maximum size of result.
1055 * @param status Used for returning error code if something went wrong. If the
1056 * number of levels requested is higher than the number of levels
1057 * in the source key, a warning (U_SORT_KEY_TOO_SHORT_WARNING) is
1059 * @return The size needed to fully store the bound.
1060 * @see ucol_keyHashCode
1063 static int32_t U_EXPORT2 getBound(const uint8_t *source,
1064 int32_t sourceLength,
1065 UColBoundMode boundType,
1066 uint32_t noOfLevels,
1068 int32_t resultLength,
1069 UErrorCode &status);
1074 // Collator protected constructors -------------------------------------
1077 * Default constructor.
1078 * Constructor is different from the old default Collator constructor.
1079 * The task for determing the default collation strength and normalization
1080 * mode is left to the child class.
1085 #ifndef U_HIDE_DEPRECATED_API
1088 * Empty constructor, does not handle the arguments.
1089 * This constructor is done for backward compatibility with 1.7 and 1.8.
1090 * The task for handling the argument collation strength and normalization
1091 * mode is left to the child class.
1092 * @param collationStrength collation strength
1093 * @param decompositionMode
1094 * @deprecated ICU 2.4. Subclasses should use the default constructor
1095 * instead and handle the strength and normalization mode themselves.
1097 Collator(UCollationStrength collationStrength,
1098 UNormalizationMode decompositionMode);
1099 #endif /* U_HIDE_DEPRECATED_API */
1103 * @param other Collator object to be copied from
1106 Collator(const Collator& other);
1110 * Used internally by registration to define the requested and valid locales.
1111 * @param requestedLocale the requested locale
1112 * @param validLocale the valid locale
1113 * @param actualLocale the actual locale
1116 virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
1118 /** Get the short definition string for a collator. This internal API harvests the collator's
1119 * locale and the attribute set and produces a string that can be used for opening
1120 * a collator with the same attributes using the ucol_openFromShortString API.
1121 * This string will be normalized.
1122 * The structure and the syntax of the string is defined in the "Naming collators"
1123 * section of the users guide:
1124 * http://userguide.icu-project.org/collation/concepts#TOC-Collator-naming-scheme
1125 * This function supports preflighting.
1127 * This is internal, and intended to be used with delegate converters.
1129 * @param locale a locale that will appear as a collators locale in the resulting
1130 * short string definition. If NULL, the locale will be harvested
1131 * from the collator.
1132 * @param buffer space to hold the resulting string
1133 * @param capacity capacity of the buffer
1134 * @param status for returning errors. All the preflighting errors are featured
1135 * @return length of the resulting string
1136 * @see ucol_openFromShortString
1137 * @see ucol_normalizeShortDefinitionString
1138 * @see ucol_getShortDefinitionString
1141 virtual int32_t internalGetShortDefinitionString(const char *locale,
1144 UErrorCode &status) const;
1147 * Implements ucol_strcollUTF8().
1150 virtual UCollationResult internalCompareUTF8(
1151 const char *left, int32_t leftLength,
1152 const char *right, int32_t rightLength,
1153 UErrorCode &errorCode) const;
1156 * Implements ucol_nextSortKeyPart().
1160 internalNextSortKeyPart(
1161 UCharIterator *iter, uint32_t state[2],
1162 uint8_t *dest, int32_t count, UErrorCode &errorCode) const;
1164 #ifndef U_HIDE_INTERNAL_API
1166 static inline Collator *fromUCollator(UCollator *uc) {
1167 return reinterpret_cast<Collator *>(uc);
1170 static inline const Collator *fromUCollator(const UCollator *uc) {
1171 return reinterpret_cast<const Collator *>(uc);
1174 inline UCollator *toUCollator() {
1175 return reinterpret_cast<UCollator *>(this);
1178 inline const UCollator *toUCollator() const {
1179 return reinterpret_cast<const UCollator *>(this);
1181 #endif // U_HIDE_INTERNAL_API
1185 * Assignment operator. Private for now.
1187 Collator& operator=(const Collator& other);
1189 friend class CFactory;
1190 friend class SimpleCFactory;
1191 friend class ICUCollatorFactory;
1192 friend class ICUCollatorService;
1193 static Collator* makeInstance(const Locale& desiredLocale,
1194 UErrorCode& status);
1197 #if !UCONFIG_NO_SERVICE
1199 * A factory, used with registerFactory, the creates multiple collators and provides
1200 * display names for them. A factory supports some number of locales-- these are the
1201 * locales for which it can create collators. The factory can be visible, in which
1202 * case the supported locales will be enumerated by getAvailableLocales, or invisible,
1203 * in which they are not. Invisible locales are still supported, they are just not
1204 * listed by getAvailableLocales.
1206 * If standard locale display names are sufficient, Collator instances can
1207 * be registered using registerInstance instead.</p>
1209 * Note: if the collators are to be used from C APIs, they must be instances
1210 * of RuleBasedCollator.</p>
1214 class U_I18N_API CollatorFactory : public UObject {
1221 virtual ~CollatorFactory();
1224 * Return true if this factory is visible. Default is true.
1225 * If not visible, the locales supported by this factory will not
1226 * be listed by getAvailableLocales.
1227 * @return true if the factory is visible.
1230 virtual UBool visible(void) const;
1233 * Return a collator for the provided locale. If the locale
1234 * is not supported, return NULL.
1235 * @param loc the locale identifying the collator to be created.
1236 * @return a new collator if the locale is supported, otherwise NULL.
1239 virtual Collator* createCollator(const Locale& loc) = 0;
1242 * Return the name of the collator for the objectLocale, localized for the displayLocale.
1243 * If objectLocale is not supported, or the factory is not visible, set the result string
1245 * @param objectLocale the locale identifying the collator
1246 * @param displayLocale the locale for which the display name of the collator should be localized
1247 * @param result an output parameter for the display name, set to bogus if not supported.
1248 * @return the display name
1251 virtual UnicodeString& getDisplayName(const Locale& objectLocale,
1252 const Locale& displayLocale,
1253 UnicodeString& result);
1256 * Return an array of all the locale names directly supported by this factory.
1257 * The number of names is returned in count. This array is owned by the factory.
1258 * Its contents must never change.
1259 * @param count output parameter for the number of locales supported by the factory
1260 * @param status the in/out error code
1261 * @return a pointer to an array of count UnicodeStrings.
1264 virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) = 0;
1266 #endif /* UCONFIG_NO_SERVICE */
1268 // Collator inline methods -----------------------------------------------
1272 #endif /* #if !UCONFIG_NO_COLLATION */