1 // © 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/unorm.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"
68 #if U_SHOW_CPLUSPLUS_API
71 class StringEnumeration
;
73 #if !UCONFIG_NO_SERVICE
77 class CollatorFactory
;
86 * The <code>Collator</code> class performs locale-sensitive string
88 * You use this class to build searching and sorting routines for natural
91 * <code>Collator</code> is an abstract base class. Subclasses implement
92 * specific collation strategies. One subclass,
93 * <code>RuleBasedCollator</code>, is currently provided and is applicable
94 * to a wide set of languages. Other subclasses may be created to handle more
97 * Like other locale-sensitive classes, you can use the static factory method,
98 * <code>createInstance</code>, to obtain the appropriate
99 * <code>Collator</code> object for a given locale. You will only need to
100 * look at the subclasses of <code>Collator</code> if you need to
101 * understand the details of a particular collation strategy or if you need to
102 * modify that strategy.
104 * The following example shows how to compare two strings using the
105 * <code>Collator</code> for the default locale.
106 * \htmlonly<blockquote>\endhtmlonly
109 * // Compare two strings in the default locale
110 * UErrorCode success = U_ZERO_ERROR;
111 * Collator* myCollator = Collator::createInstance(success);
112 * if (myCollator->compare("abc", "ABC") < 0)
113 * cout << "abc is less than ABC" << endl;
115 * cout << "abc is greater than or equal to ABC" << endl;
118 * \htmlonly</blockquote>\endhtmlonly
120 * You can set a <code>Collator</code>'s <em>strength</em> attribute to
121 * determine the level of difference considered significant in comparisons.
122 * Five strengths are provided: <code>PRIMARY</code>, <code>SECONDARY</code>,
123 * <code>TERTIARY</code>, <code>QUATERNARY</code> and <code>IDENTICAL</code>.
124 * The exact assignment of strengths to language features is locale dependent.
125 * For example, in Czech, "e" and "f" are considered primary differences,
126 * while "e" and "\u00EA" are secondary differences, "e" and "E" are tertiary
127 * differences and "e" and "e" are identical. The following shows how both case
128 * and accents could be ignored for US English.
129 * \htmlonly<blockquote>\endhtmlonly
132 * //Get the Collator for US English and set its strength to PRIMARY
133 * UErrorCode success = U_ZERO_ERROR;
134 * Collator* usCollator = Collator::createInstance(Locale::getUS(), success);
135 * usCollator->setStrength(Collator::PRIMARY);
136 * if (usCollator->compare("abc", "ABC") == 0)
137 * cout << "'abc' and 'ABC' strings are equivalent with strength PRIMARY" << endl;
140 * \htmlonly</blockquote>\endhtmlonly
142 * The <code>getSortKey</code> methods
143 * convert a string to a series of bytes that can be compared bitwise against
144 * other sort keys using <code>strcmp()</code>. Sort keys are written as
145 * zero-terminated byte strings.
147 * Another set of APIs returns a <code>CollationKey</code> object that wraps
148 * the sort key bytes instead of returning the bytes themselves.
151 * <strong>Note:</strong> <code>Collator</code>s with different Locale,
152 * and CollationStrength settings will return different sort
153 * orders for the same set of strings. Locales have specific collation rules,
154 * and the way in which secondary and tertiary differences are taken into
155 * account, for example, will result in a different sorting order for same
158 * @see RuleBasedCollator
160 * @see CollationElementIterator
163 * @version 2.0 11/15/01
166 class U_I18N_API Collator
: public UObject
{
169 // Collator public enums -----------------------------------------------
172 * Base letter represents a primary difference. Set comparison level to
173 * PRIMARY to ignore secondary and tertiary differences.<br>
174 * Use this to set the strength of a Collator object.<br>
175 * Example of primary difference, "abc" < "abd"
177 * Diacritical differences on the same base letter represent a secondary
178 * difference. Set comparison level to SECONDARY to ignore tertiary
179 * differences. Use this to set the strength of a Collator object.<br>
180 * Example of secondary difference, "ä" >> "a".
182 * Uppercase and lowercase versions of the same character represents a
183 * tertiary difference. Set comparison level to TERTIARY to include all
184 * comparison differences. Use this to set the strength of a Collator
186 * Example of tertiary difference, "abc" <<< "ABC".
188 * Two characters are considered "identical" when they have the same unicode
190 * For example, "ä" == "ä".
192 * UCollationStrength is also used to determine the strength of sort keys
193 * generated from Collator objects.
196 enum ECollationStrength
198 PRIMARY
= UCOL_PRIMARY
, // 0
199 SECONDARY
= UCOL_SECONDARY
, // 1
200 TERTIARY
= UCOL_TERTIARY
, // 2
201 QUATERNARY
= UCOL_QUATERNARY
, // 3
202 IDENTICAL
= UCOL_IDENTICAL
// 15
206 // Cannot use #ifndef U_HIDE_DEPRECATED_API for the following, it is
207 // used by virtual methods that cannot have that conditional.
209 * LESS is returned if source string is compared to be less than target
210 * string in the compare() method.
211 * EQUAL is returned if source string is compared to be equal to target
212 * string in the compare() method.
213 * GREATER is returned if source string is compared to be greater than
214 * target string in the compare() method.
215 * @see Collator#compare
216 * @deprecated ICU 2.6. Use C enum UCollationResult defined in ucol.h
218 enum EComparisonResult
220 LESS
= UCOL_LESS
, // -1
221 EQUAL
= UCOL_EQUAL
, // 0
222 GREATER
= UCOL_GREATER
// 1
225 // Collator public destructor -----------------------------------------
233 // Collator public methods --------------------------------------------
236 * Returns TRUE if "other" is the same as "this".
238 * The base class implementation returns TRUE if "other" has the same type/class as "this":
239 * <code>typeid(*this) == typeid(other)</code>.
241 * Subclass implementations should do something like the following:
243 * if (this == &other) { return TRUE; }
244 * if (!Collator::operator==(other)) { return FALSE; } // not the same class
246 * const MyCollator &o = (const MyCollator&)other;
247 * (compare this vs. o's subclass fields)
249 * @param other Collator object to be compared
250 * @return TRUE if other is the same as this.
253 virtual UBool
operator==(const Collator
& other
) const;
256 * Returns true if "other" is not the same as "this".
257 * Calls ! operator==(const Collator&) const which works for all subclasses.
258 * @param other Collator object to be compared
259 * @return TRUE if other is not the same as this.
262 virtual UBool
operator!=(const Collator
& other
) const;
265 * Makes a copy of this object.
266 * @return a copy of this object, owned by the caller
269 virtual Collator
* clone(void) const = 0;
272 * Creates the Collator object for the current default locale.
273 * The default locale is determined by Locale::getDefault.
274 * The UErrorCode& err parameter is used to return status information to the user.
275 * To check whether the construction succeeded or not, you should check the
276 * value of U_SUCCESS(err). If you wish more detailed information, you can
277 * check for informational error results which still indicate success.
278 * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
279 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
280 * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
281 * used; neither the requested locale nor any of its fall back locales
283 * The caller owns the returned object and is responsible for deleting it.
285 * @param err the error code status.
286 * @return the collation object of the default locale.(for example, en_US)
287 * @see Locale#getDefault
290 static Collator
* U_EXPORT2
createInstance(UErrorCode
& err
);
293 * Gets the collation object for the desired locale. The
294 * resource of the desired locale will be loaded.
296 * Locale::getRoot() is the base collation table and all other languages are
297 * built on top of it with additional language-specific modifications.
299 * For some languages, multiple collation types are available;
300 * for example, "de@collation=phonebook".
301 * Starting with ICU 54, collation attributes can be specified via locale keywords as well,
302 * in the old locale extension syntax ("el@colCaseFirst=upper")
303 * or in language tag syntax ("el-u-kf-upper").
304 * See <a href="http://userguide.icu-project.org/collation/api">User Guide: Collation API</a>.
306 * The UErrorCode& err parameter is used to return status information to the user.
307 * To check whether the construction succeeded or not, you should check
308 * the value of U_SUCCESS(err). If you wish more detailed information, you
309 * can check for informational error results which still indicate success.
310 * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
311 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
312 * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
313 * used; neither the requested locale nor any of its fall back locales
316 * The caller owns the returned object and is responsible for deleting it.
317 * @param loc The locale ID for which to open a collator.
318 * @param err the error code status.
319 * @return the created table-based collation object based on the desired
322 * @see ResourceLoader
325 static Collator
* U_EXPORT2
createInstance(const Locale
& loc
, UErrorCode
& err
);
328 * The comparison function compares the character data stored in two
329 * different strings. Returns information about whether a string is less
330 * than, greater than or equal to another string.
331 * @param source the source string to be compared with.
332 * @param target the string that is to be compared with the source string.
333 * @return Returns a byte value. GREATER if source is greater
334 * than target; EQUAL if source is equal to target; LESS if source is less
336 * @deprecated ICU 2.6 use the overload with UErrorCode &
338 virtual EComparisonResult
compare(const UnicodeString
& source
,
339 const UnicodeString
& target
) const;
342 * The comparison function compares the character data stored in two
343 * different strings. Returns information about whether a string is less
344 * than, greater than or equal to another string.
345 * @param source the source string to be compared with.
346 * @param target the string that is to be compared with the source string.
347 * @param status possible error code
348 * @return Returns an enum value. UCOL_GREATER if source is greater
349 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
353 virtual UCollationResult
compare(const UnicodeString
& source
,
354 const UnicodeString
& target
,
355 UErrorCode
&status
) const = 0;
358 * Does the same thing as compare but limits the comparison to a specified
360 * @param source the source string to be compared with.
361 * @param target the string that is to be compared with the source string.
362 * @param length the length the comparison is limited to
363 * @return Returns a byte value. GREATER if source (up to the specified
364 * length) is greater than target; EQUAL if source (up to specified
365 * length) is equal to target; LESS if source (up to the specified
366 * length) is less than target.
367 * @deprecated ICU 2.6 use the overload with UErrorCode &
369 virtual EComparisonResult
compare(const UnicodeString
& source
,
370 const UnicodeString
& target
,
371 int32_t length
) const;
374 * Does the same thing as compare but limits the comparison to a specified
376 * @param source the source string to be compared with.
377 * @param target the string that is to be compared with the source string.
378 * @param length the length the comparison is limited to
379 * @param status possible error code
380 * @return Returns an enum value. UCOL_GREATER if source (up to the specified
381 * length) is greater than target; UCOL_EQUAL if source (up to specified
382 * length) is equal to target; UCOL_LESS if source (up to the specified
383 * length) is less than target.
386 virtual UCollationResult
compare(const UnicodeString
& source
,
387 const UnicodeString
& target
,
389 UErrorCode
&status
) const = 0;
392 * The comparison function compares the character data stored in two
393 * different string arrays. Returns information about whether a string array
394 * is less than, greater than or equal to another string array.
397 * . char16_t ABC[] = {0x41, 0x42, 0x43, 0}; // = "ABC"
398 * . char16_t abc[] = {0x61, 0x62, 0x63, 0}; // = "abc"
399 * . UErrorCode status = U_ZERO_ERROR;
400 * . Collator *myCollation =
401 * . Collator::createInstance(Locale::getUS(), status);
402 * . if (U_FAILURE(status)) return;
403 * . myCollation->setStrength(Collator::PRIMARY);
404 * . // result would be Collator::EQUAL ("abc" == "ABC")
405 * . // (no primary difference between "abc" and "ABC")
406 * . Collator::EComparisonResult result =
407 * . myCollation->compare(abc, 3, ABC, 3);
408 * . myCollation->setStrength(Collator::TERTIARY);
409 * . // result would be Collator::LESS ("abc" <<< "ABC")
410 * . // (with tertiary difference between "abc" and "ABC")
411 * . result = myCollation->compare(abc, 3, ABC, 3);
413 * @param source the source string array to be compared with.
414 * @param sourceLength the length of the source string array. If this value
415 * is equal to -1, the string array is null-terminated.
416 * @param target the string that is to be compared with the source string.
417 * @param targetLength the length of the target string array. If this value
418 * is equal to -1, the string array is null-terminated.
419 * @return Returns a byte value. GREATER if source is greater than target;
420 * EQUAL if source is equal to target; LESS if source is less than
422 * @deprecated ICU 2.6 use the overload with UErrorCode &
424 virtual EComparisonResult
compare(const char16_t* source
, int32_t sourceLength
,
425 const char16_t* target
, int32_t targetLength
)
429 * The comparison function compares the character data stored in two
430 * different string arrays. Returns information about whether a string array
431 * is less than, greater than or equal to another string array.
432 * @param source the source string array to be compared with.
433 * @param sourceLength the length of the source string array. If this value
434 * is equal to -1, the string array is null-terminated.
435 * @param target the string that is to be compared with the source string.
436 * @param targetLength the length of the target string array. If this value
437 * is equal to -1, the string array is null-terminated.
438 * @param status possible error code
439 * @return Returns an enum value. UCOL_GREATER if source is greater
440 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
444 virtual UCollationResult
compare(const char16_t* source
, int32_t sourceLength
,
445 const char16_t* target
, int32_t targetLength
,
446 UErrorCode
&status
) const = 0;
449 * Compares two strings using the Collator.
450 * Returns whether the first one compares less than/equal to/greater than
452 * This version takes UCharIterator input.
453 * @param sIter the first ("source") string iterator
454 * @param tIter the second ("target") string iterator
455 * @param status ICU status
456 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
459 virtual UCollationResult
compare(UCharIterator
&sIter
,
460 UCharIterator
&tIter
,
461 UErrorCode
&status
) const;
464 * Compares two UTF-8 strings using the Collator.
465 * Returns whether the first one compares less than/equal to/greater than
467 * This version takes UTF-8 input.
468 * Note that a StringPiece can be implicitly constructed
469 * from a std::string or a NUL-terminated const char * string.
470 * @param source the first UTF-8 string
471 * @param target the second UTF-8 string
472 * @param status ICU status
473 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
476 virtual UCollationResult
compareUTF8(const StringPiece
&source
,
477 const StringPiece
&target
,
478 UErrorCode
&status
) const;
481 * Transforms the string into a series of characters that can be compared
482 * with CollationKey::compareTo. It is not possible to restore the original
483 * string from the chars in the sort key.
484 * <p>Use CollationKey::equals or CollationKey::compare to compare the
485 * generated sort keys.
486 * If the source string is null, a null collation key will be returned.
488 * Note that sort keys are often less efficient than simply doing comparison.
489 * For more details, see the ICU User Guide.
491 * @param source the source string to be transformed into a sort key.
492 * @param key the collation key to be filled in
493 * @param status the error code status.
494 * @return the collation key of the string based on the collation rules.
495 * @see CollationKey#compare
498 virtual CollationKey
& getCollationKey(const UnicodeString
& source
,
500 UErrorCode
& status
) const = 0;
503 * Transforms the string into a series of characters that can be compared
504 * with CollationKey::compareTo. It is not possible to restore the original
505 * string from the chars in the sort key.
506 * <p>Use CollationKey::equals or CollationKey::compare to compare the
507 * generated sort keys.
508 * <p>If the source string is null, a null collation key will be returned.
510 * Note that sort keys are often less efficient than simply doing comparison.
511 * For more details, see the ICU User Guide.
513 * @param source the source string to be transformed into a sort key.
514 * @param sourceLength length of the collation key
515 * @param key the collation key to be filled in
516 * @param status the error code status.
517 * @return the collation key of the string based on the collation rules.
518 * @see CollationKey#compare
521 virtual CollationKey
& getCollationKey(const char16_t*source
,
522 int32_t sourceLength
,
524 UErrorCode
& status
) const = 0;
526 * Generates the hash code for the collation object
529 virtual int32_t hashCode(void) const = 0;
532 * Gets the locale of the Collator
534 * @param type can be either requested, valid or actual locale. For more
535 * information see the definition of ULocDataLocaleType in
537 * @param status the error code status.
538 * @return locale where the collation data lives. If the collator
539 * was instantiated from rules, locale is empty.
540 * @deprecated ICU 2.8 This API is under consideration for revision
543 virtual Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const = 0;
546 * Convenience method for comparing two strings based on the collation rules.
547 * @param source the source string to be compared with.
548 * @param target the target string to be compared with.
549 * @return true if the first string is greater than the second one,
550 * according to the collation rules. false, otherwise.
551 * @see Collator#compare
554 UBool
greater(const UnicodeString
& source
, const UnicodeString
& target
)
558 * Convenience method for comparing two strings based on the collation rules.
559 * @param source the source string to be compared with.
560 * @param target the target string to be compared with.
561 * @return true if the first string is greater than or equal to the second
562 * one, according to the collation rules. false, otherwise.
563 * @see Collator#compare
566 UBool
greaterOrEqual(const UnicodeString
& source
,
567 const UnicodeString
& target
) const;
570 * Convenience method for comparing two strings based on the collation rules.
571 * @param source the source string to be compared with.
572 * @param target the target string to be compared with.
573 * @return true if the strings are equal according to the collation rules.
575 * @see Collator#compare
578 UBool
equals(const UnicodeString
& source
, const UnicodeString
& target
) const;
581 * Determines the minimum strength that will be used in comparison or
583 * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
584 * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
586 * @return the current comparison level.
587 * @see Collator#setStrength
588 * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
590 virtual ECollationStrength
getStrength(void) const;
593 * Sets the minimum strength to be used in comparison or transformation.
597 * UErrorCode status = U_ZERO_ERROR;
598 * Collator*myCollation = Collator::createInstance(Locale::getUS(), status);
599 * if (U_FAILURE(status)) return;
600 * myCollation->setStrength(Collator::PRIMARY);
601 * // result will be "abc" == "ABC"
602 * // tertiary differences will be ignored
603 * Collator::ComparisonResult result = myCollation->compare("abc", "ABC");
606 * @see Collator#getStrength
607 * @param newStrength the new comparison level.
608 * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
610 virtual void setStrength(ECollationStrength newStrength
);
613 * Retrieves the reordering codes for this collator.
614 * @param dest The array to fill with the script ordering.
615 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the function
616 * will only return the length of the result without writing any codes (pre-flighting).
617 * @param status A reference to an error code value, which must not indicate
618 * a failure before the function call.
619 * @return The length of the script ordering array.
620 * @see ucol_setReorderCodes
621 * @see Collator#getEquivalentReorderCodes
622 * @see Collator#setReorderCodes
624 * @see UColReorderCode
627 virtual int32_t getReorderCodes(int32_t *dest
,
628 int32_t destCapacity
,
629 UErrorCode
& status
) const;
632 * Sets the ordering of scripts for this collator.
634 * <p>The reordering codes are a combination of script codes and reorder codes.
635 * @param reorderCodes An array of script codes in the new order. This can be NULL if the
636 * length is also set to 0. An empty array will clear any reordering codes on the collator.
637 * @param reorderCodesLength The length of reorderCodes.
638 * @param status error code
639 * @see ucol_setReorderCodes
640 * @see Collator#getReorderCodes
641 * @see Collator#getEquivalentReorderCodes
643 * @see UColReorderCode
646 virtual void setReorderCodes(const int32_t* reorderCodes
,
647 int32_t reorderCodesLength
,
648 UErrorCode
& status
) ;
651 * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
652 * codes will be grouped and must reorder together.
653 * Beginning with ICU 55, scripts only reorder together if they are primary-equal,
654 * for example Hiragana and Katakana.
656 * @param reorderCode The reorder code to determine equivalence for.
657 * @param dest The array to fill with the script equivalence reordering codes.
658 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the
659 * function will only return the length of the result without writing any codes (pre-flighting).
660 * @param status A reference to an error code value, which must not indicate
661 * a failure before the function call.
662 * @return The length of the of the reordering code equivalence array.
663 * @see ucol_setReorderCodes
664 * @see Collator#getReorderCodes
665 * @see Collator#setReorderCodes
667 * @see UColReorderCode
670 static int32_t U_EXPORT2
getEquivalentReorderCodes(int32_t reorderCode
,
672 int32_t destCapacity
,
676 * Get name of the object for the desired Locale, in the desired langauge
677 * @param objectLocale must be from getAvailableLocales
678 * @param displayLocale specifies the desired locale for output
679 * @param name the fill-in parameter of the return value
680 * @return display-able name of the object for the object locale in the
684 static UnicodeString
& U_EXPORT2
getDisplayName(const Locale
& objectLocale
,
685 const Locale
& displayLocale
,
686 UnicodeString
& name
);
689 * Get name of the object for the desired Locale, in the langauge of the
691 * @param objectLocale must be from getAvailableLocales
692 * @param name the fill-in parameter of the return value
693 * @return name of the object for the desired locale in the default language
696 static UnicodeString
& U_EXPORT2
getDisplayName(const Locale
& objectLocale
,
697 UnicodeString
& name
);
700 * Get the set of Locales for which Collations are installed.
702 * <p>Note this does not include locales supported by registered collators.
703 * If collators might have been registered, use the overload of getAvailableLocales
704 * that returns a StringEnumeration.</p>
706 * @param count the output parameter of number of elements in the locale list
707 * @return the list of available locales for which collations are installed
710 static const Locale
* U_EXPORT2
getAvailableLocales(int32_t& count
);
713 * Return a StringEnumeration over the locales available at the time of the call,
714 * including registered locales. If a severe error occurs (such as out of memory
715 * condition) this will return null. If there is no locale data, an empty enumeration
717 * @return a StringEnumeration over the locales available at the time of the call
720 static StringEnumeration
* U_EXPORT2
getAvailableLocales(void);
723 * Create a string enumerator of all possible keywords that are relevant to
724 * collation. At this point, the only recognized keyword for this
725 * service is "collation".
726 * @param status input-output error code
727 * @return a string enumeration over locale strings. The caller is
728 * responsible for closing the result.
731 static StringEnumeration
* U_EXPORT2
getKeywords(UErrorCode
& status
);
734 * Given a keyword, create a string enumeration of all values
735 * for that keyword that are currently in use.
736 * @param keyword a particular keyword as enumerated by
737 * ucol_getKeywords. If any other keyword is passed in, status is set
738 * to U_ILLEGAL_ARGUMENT_ERROR.
739 * @param status input-output error code
740 * @return a string enumeration over collation keyword values, or NULL
741 * upon error. The caller is responsible for deleting the result.
744 static StringEnumeration
* U_EXPORT2
getKeywordValues(const char *keyword
, UErrorCode
& status
);
747 * Given a key and a locale, returns an array of string values in a preferred
748 * order that would make a difference. These are all and only those values where
749 * the open (creation) of the service with the locale formed from the input locale
750 * plus input keyword and that value has different behavior than creation with the
751 * input locale alone.
752 * @param keyword one of the keys supported by this service. For now, only
753 * "collation" is supported.
754 * @param locale the locale
755 * @param commonlyUsed if set to true it will return only commonly used values
756 * with the given locale in preferred order. Otherwise,
757 * it will return all the available values for the locale.
758 * @param status ICU status
759 * @return a string enumeration over keyword values for the given key and the locale.
762 static StringEnumeration
* U_EXPORT2
getKeywordValuesForLocale(const char* keyword
, const Locale
& locale
,
763 UBool commonlyUsed
, UErrorCode
& status
);
766 * Return the functionally equivalent locale for the given
767 * requested locale, with respect to given keyword, for the
768 * collation service. If two locales return the same result, then
769 * collators instantiated for these locales will behave
770 * equivalently. The converse is not always true; two collators
771 * may in fact be equivalent, but return different results, due to
772 * internal details. The return result has no other meaning than
773 * that stated above, and implies nothing as to the relationship
774 * between the two locales. This is intended for use by
775 * applications who wish to cache collators, or otherwise reuse
776 * collators when possible. The functional equivalent may change
777 * over time. For more information, please see the <a
778 * href="http://userguide.icu-project.org/locale#TOC-Locales-and-Services">
779 * Locales and Services</a> section of the ICU User Guide.
780 * @param keyword a particular keyword as enumerated by
782 * @param locale the requested locale
783 * @param isAvailable reference to a fillin parameter that
784 * indicates whether the requested locale was 'available' to the
785 * collation service. A locale is defined as 'available' if it
786 * physically exists within the collation locale data.
787 * @param status reference to input-output error code
788 * @return the functionally equivalent collation locale, or the root
792 static Locale U_EXPORT2
getFunctionalEquivalent(const char* keyword
, const Locale
& locale
,
793 UBool
& isAvailable
, UErrorCode
& status
);
795 #if !UCONFIG_NO_SERVICE
797 * Register a new Collator. The collator will be adopted.
798 * Because ICU may choose to cache collators internally, this must be
799 * called at application startup, prior to any calls to
800 * Collator::createInstance to avoid undefined behavior.
801 * @param toAdopt the Collator instance to be adopted
802 * @param locale the locale with which the collator will be associated
803 * @param status the in/out status code, no special meanings are assigned
804 * @return a registry key that can be used to unregister this collator
807 static URegistryKey U_EXPORT2
registerInstance(Collator
* toAdopt
, const Locale
& locale
, UErrorCode
& status
);
810 * Register a new CollatorFactory. The factory will be adopted.
811 * Because ICU may choose to cache collators internally, this must be
812 * called at application startup, prior to any calls to
813 * Collator::createInstance to avoid undefined behavior.
814 * @param toAdopt the CollatorFactory instance to be adopted
815 * @param status the in/out status code, no special meanings are assigned
816 * @return a registry key that can be used to unregister this collator
819 static URegistryKey U_EXPORT2
registerFactory(CollatorFactory
* toAdopt
, UErrorCode
& status
);
822 * Unregister a previously-registered Collator or CollatorFactory
823 * using the key returned from the register call. Key becomes
824 * invalid after a successful call and should not be used again.
825 * The object corresponding to the key will be deleted.
826 * Because ICU may choose to cache collators internally, this should
827 * be called during application shutdown, after all calls to
828 * Collator::createInstance to avoid undefined behavior.
829 * @param key the registry key returned by a previous call to registerInstance
830 * @param status the in/out status code, no special meanings are assigned
831 * @return TRUE if the collator for the key was successfully unregistered
834 static UBool U_EXPORT2
unregister(URegistryKey key
, UErrorCode
& status
);
835 #endif /* UCONFIG_NO_SERVICE */
838 * Gets the version information for a Collator.
839 * @param info the version # information, the result will be filled in
842 virtual void getVersion(UVersionInfo info
) const = 0;
845 * Returns a unique class ID POLYMORPHICALLY. Pure virtual method.
846 * This method is to implement a simple version of RTTI, since not all C++
847 * compilers support genuine RTTI. Polymorphic operator==() and clone()
848 * methods call this method.
849 * @return The class ID for this object. All objects of a given class have
850 * the same class ID. Objects of other classes have different class
854 virtual UClassID
getDynamicClassID(void) const = 0;
857 * Universal attribute setter
858 * @param attr attribute type
859 * @param value attribute value
860 * @param status to indicate whether the operation went on smoothly or
864 virtual void setAttribute(UColAttribute attr
, UColAttributeValue value
,
865 UErrorCode
&status
) = 0;
868 * Universal attribute getter
869 * @param attr attribute type
870 * @param status to indicate whether the operation went on smoothly or
872 * @return attribute value
875 virtual UColAttributeValue
getAttribute(UColAttribute attr
,
876 UErrorCode
&status
) const = 0;
879 * Sets the variable top to the top of the specified reordering group.
880 * The variable top determines the highest-sorting character
881 * which is affected by UCOL_ALTERNATE_HANDLING.
882 * If that attribute is set to UCOL_NON_IGNORABLE, then the variable top has no effect.
884 * The base class implementation sets U_UNSUPPORTED_ERROR.
885 * @param group one of UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION,
886 * UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_CURRENCY;
887 * or UCOL_REORDER_CODE_DEFAULT to restore the default max variable group
888 * @param errorCode Standard ICU error code. Its input value must
889 * pass the U_SUCCESS() test, or else the function returns
890 * immediately. Check for U_FAILURE() on output or use with
891 * function chaining. (See User Guide for details.)
893 * @see getMaxVariable
896 virtual Collator
&setMaxVariable(UColReorderCode group
, UErrorCode
&errorCode
);
899 * Returns the maximum reordering group whose characters are affected by UCOL_ALTERNATE_HANDLING.
901 * The base class implementation returns UCOL_REORDER_CODE_PUNCTUATION.
902 * @return the maximum variable reordering group.
903 * @see setMaxVariable
906 virtual UColReorderCode
getMaxVariable() const;
909 * Sets the variable top to the primary weight of the specified string.
911 * Beginning with ICU 53, the variable top is pinned to
912 * the top of one of the supported reordering groups,
913 * and it must not be beyond the last of those groups.
914 * See setMaxVariable().
915 * @param varTop one or more (if contraction) char16_ts to which the variable top should be set
916 * @param len length of variable top string. If -1 it is considered to be zero terminated.
917 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
918 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
919 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
920 * the last reordering group supported by setMaxVariable()
921 * @return variable top primary weight
922 * @deprecated ICU 53 Call setMaxVariable() instead.
924 virtual uint32_t setVariableTop(const char16_t *varTop
, int32_t len
, UErrorCode
&status
) = 0;
927 * Sets the variable top to the primary weight of the specified string.
929 * Beginning with ICU 53, the variable top is pinned to
930 * the top of one of the supported reordering groups,
931 * and it must not be beyond the last of those groups.
932 * See setMaxVariable().
933 * @param varTop a UnicodeString size 1 or more (if contraction) of char16_ts to which the variable top should be set
934 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
935 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
936 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
937 * the last reordering group supported by setMaxVariable()
938 * @return variable top primary weight
939 * @deprecated ICU 53 Call setMaxVariable() instead.
941 virtual uint32_t setVariableTop(const UnicodeString
&varTop
, UErrorCode
&status
) = 0;
944 * Sets the variable top to the specified primary weight.
946 * Beginning with ICU 53, the variable top is pinned to
947 * the top of one of the supported reordering groups,
948 * and it must not be beyond the last of those groups.
949 * See setMaxVariable().
950 * @param varTop primary weight, as returned by setVariableTop or ucol_getVariableTop
951 * @param status error code
952 * @deprecated ICU 53 Call setMaxVariable() instead.
954 virtual void setVariableTop(uint32_t varTop
, UErrorCode
&status
) = 0;
957 * Gets the variable top value of a Collator.
958 * @param status error code (not changed by function). If error code is set, the return value is undefined.
959 * @return the variable top primary weight
960 * @see getMaxVariable
963 virtual uint32_t getVariableTop(UErrorCode
&status
) const = 0;
966 * Get a UnicodeSet that contains all the characters and sequences
967 * tailored in this collator.
968 * @param status error code of the operation
969 * @return a pointer to a UnicodeSet object containing all the
970 * code points and sequences that may sort differently than
971 * in the root collator. The object must be disposed of by using delete
974 virtual UnicodeSet
*getTailoredSet(UErrorCode
&status
) const;
978 * The base class implementation simply calls clone().
979 * @return a copy of this object, owned by the caller
981 * @deprecated ICU 50 no need to have two methods for cloning
983 virtual Collator
* safeClone(void) const;
986 * Get the sort key as an array of bytes from a UnicodeString.
987 * Sort key byte arrays are zero-terminated and can be compared using
990 * Note that sort keys are often less efficient than simply doing comparison.
991 * For more details, see the ICU User Guide.
993 * @param source string to be processed.
994 * @param result buffer to store result in. If NULL, number of bytes needed
996 * @param resultLength length of the result buffer. If if not enough the
997 * buffer will be filled to capacity.
998 * @return Number of bytes needed for storing the sort key
1001 virtual int32_t getSortKey(const UnicodeString
& source
,
1003 int32_t resultLength
) const = 0;
1006 * Get the sort key as an array of bytes from a char16_t buffer.
1007 * Sort key byte arrays are zero-terminated and can be compared using
1010 * Note that sort keys are often less efficient than simply doing comparison.
1011 * For more details, see the ICU User Guide.
1013 * @param source string to be processed.
1014 * @param sourceLength length of string to be processed.
1015 * If -1, the string is 0 terminated and length will be decided by the
1017 * @param result buffer to store result in. If NULL, number of bytes needed
1019 * @param resultLength length of the result buffer. If if not enough the
1020 * buffer will be filled to capacity.
1021 * @return Number of bytes needed for storing the sort key
1024 virtual int32_t getSortKey(const char16_t*source
, int32_t sourceLength
,
1025 uint8_t*result
, int32_t resultLength
) const = 0;
1028 * Produce a bound for a given sortkey and a number of levels.
1029 * Return value is always the number of bytes needed, regardless of
1030 * whether the result buffer was big enough or even valid.<br>
1031 * Resulting bounds can be used to produce a range of strings that are
1032 * between upper and lower bounds. For example, if bounds are produced
1033 * for a sortkey of string "smith", strings between upper and lower
1034 * bounds with one level would include "Smith", "SMITH", "sMiTh".<br>
1035 * There are two upper bounds that can be produced. If UCOL_BOUND_UPPER
1036 * is produced, strings matched would be as above. However, if bound
1037 * produced using UCOL_BOUND_UPPER_LONG is used, the above example will
1038 * also match "Smithsonian" and similar.<br>
1039 * For more on usage, see example in cintltst/capitst.c in procedure
1041 * Sort keys may be compared using <TT>strcmp</TT>.
1042 * @param source The source sortkey.
1043 * @param sourceLength The length of source, or -1 if null-terminated.
1044 * (If an unmodified sortkey is passed, it is always null
1046 * @param boundType Type of bound required. It can be UCOL_BOUND_LOWER, which
1047 * produces a lower inclusive bound, UCOL_BOUND_UPPER, that
1048 * produces upper bound that matches strings of the same length
1049 * or UCOL_BOUND_UPPER_LONG that matches strings that have the
1050 * same starting substring as the source string.
1051 * @param noOfLevels Number of levels required in the resulting bound (for most
1052 * uses, the recommended value is 1). See users guide for
1053 * explanation on number of levels a sortkey can have.
1054 * @param result A pointer to a buffer to receive the resulting sortkey.
1055 * @param resultLength The maximum size of result.
1056 * @param status Used for returning error code if something went wrong. If the
1057 * number of levels requested is higher than the number of levels
1058 * in the source key, a warning (U_SORT_KEY_TOO_SHORT_WARNING) is
1060 * @return The size needed to fully store the bound.
1061 * @see ucol_keyHashCode
1064 static int32_t U_EXPORT2
getBound(const uint8_t *source
,
1065 int32_t sourceLength
,
1066 UColBoundMode boundType
,
1067 uint32_t noOfLevels
,
1069 int32_t resultLength
,
1070 UErrorCode
&status
);
1075 // Collator protected constructors -------------------------------------
1078 * Default constructor.
1079 * Constructor is different from the old default Collator constructor.
1080 * The task for determing the default collation strength and normalization
1081 * mode is left to the child class.
1086 #ifndef U_HIDE_DEPRECATED_API
1089 * Empty constructor, does not handle the arguments.
1090 * This constructor is done for backward compatibility with 1.7 and 1.8.
1091 * The task for handling the argument collation strength and normalization
1092 * mode is left to the child class.
1093 * @param collationStrength collation strength
1094 * @param decompositionMode
1095 * @deprecated ICU 2.4. Subclasses should use the default constructor
1096 * instead and handle the strength and normalization mode themselves.
1098 Collator(UCollationStrength collationStrength
,
1099 UNormalizationMode decompositionMode
);
1100 #endif /* U_HIDE_DEPRECATED_API */
1104 * @param other Collator object to be copied from
1107 Collator(const Collator
& other
);
1111 * Used internally by registration to define the requested and valid locales.
1112 * @param requestedLocale the requested locale
1113 * @param validLocale the valid locale
1114 * @param actualLocale the actual locale
1117 virtual void setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
, const Locale
& actualLocale
);
1119 /** Get the short definition string for a collator. This internal API harvests the collator's
1120 * locale and the attribute set and produces a string that can be used for opening
1121 * a collator with the same attributes using the ucol_openFromShortString API.
1122 * This string will be normalized.
1123 * The structure and the syntax of the string is defined in the "Naming collators"
1124 * section of the users guide:
1125 * http://userguide.icu-project.org/collation/concepts#TOC-Collator-naming-scheme
1126 * This function supports preflighting.
1128 * This is internal, and intended to be used with delegate converters.
1130 * @param locale a locale that will appear as a collators locale in the resulting
1131 * short string definition. If NULL, the locale will be harvested
1132 * from the collator.
1133 * @param buffer space to hold the resulting string
1134 * @param capacity capacity of the buffer
1135 * @param status for returning errors. All the preflighting errors are featured
1136 * @return length of the resulting string
1137 * @see ucol_openFromShortString
1138 * @see ucol_normalizeShortDefinitionString
1139 * @see ucol_getShortDefinitionString
1142 virtual int32_t internalGetShortDefinitionString(const char *locale
,
1145 UErrorCode
&status
) const;
1148 * Implements ucol_strcollUTF8().
1151 virtual UCollationResult
internalCompareUTF8(
1152 const char *left
, int32_t leftLength
,
1153 const char *right
, int32_t rightLength
,
1154 UErrorCode
&errorCode
) const;
1157 * Implements ucol_nextSortKeyPart().
1161 internalNextSortKeyPart(
1162 UCharIterator
*iter
, uint32_t state
[2],
1163 uint8_t *dest
, int32_t count
, UErrorCode
&errorCode
) const;
1165 #ifndef U_HIDE_INTERNAL_API
1167 static inline Collator
*fromUCollator(UCollator
*uc
) {
1168 return reinterpret_cast<Collator
*>(uc
);
1171 static inline const Collator
*fromUCollator(const UCollator
*uc
) {
1172 return reinterpret_cast<const Collator
*>(uc
);
1175 inline UCollator
*toUCollator() {
1176 return reinterpret_cast<UCollator
*>(this);
1179 inline const UCollator
*toUCollator() const {
1180 return reinterpret_cast<const UCollator
*>(this);
1182 #endif // U_HIDE_INTERNAL_API
1186 * Assignment operator. Private for now.
1188 Collator
& operator=(const Collator
& other
);
1190 friend class CFactory
;
1191 friend class SimpleCFactory
;
1192 friend class ICUCollatorFactory
;
1193 friend class ICUCollatorService
;
1194 static Collator
* makeInstance(const Locale
& desiredLocale
,
1195 UErrorCode
& status
);
1198 #if !UCONFIG_NO_SERVICE
1200 * A factory, used with registerFactory, the creates multiple collators and provides
1201 * display names for them. A factory supports some number of locales-- these are the
1202 * locales for which it can create collators. The factory can be visible, in which
1203 * case the supported locales will be enumerated by getAvailableLocales, or invisible,
1204 * in which they are not. Invisible locales are still supported, they are just not
1205 * listed by getAvailableLocales.
1207 * If standard locale display names are sufficient, Collator instances can
1208 * be registered using registerInstance instead.</p>
1210 * Note: if the collators are to be used from C APIs, they must be instances
1211 * of RuleBasedCollator.</p>
1215 class U_I18N_API CollatorFactory
: public UObject
{
1222 virtual ~CollatorFactory();
1225 * Return true if this factory is visible. Default is true.
1226 * If not visible, the locales supported by this factory will not
1227 * be listed by getAvailableLocales.
1228 * @return true if the factory is visible.
1231 virtual UBool
visible(void) const;
1234 * Return a collator for the provided locale. If the locale
1235 * is not supported, return NULL.
1236 * @param loc the locale identifying the collator to be created.
1237 * @return a new collator if the locale is supported, otherwise NULL.
1240 virtual Collator
* createCollator(const Locale
& loc
) = 0;
1243 * Return the name of the collator for the objectLocale, localized for the displayLocale.
1244 * If objectLocale is not supported, or the factory is not visible, set the result string
1246 * @param objectLocale the locale identifying the collator
1247 * @param displayLocale the locale for which the display name of the collator should be localized
1248 * @param result an output parameter for the display name, set to bogus if not supported.
1249 * @return the display name
1252 virtual UnicodeString
& getDisplayName(const Locale
& objectLocale
,
1253 const Locale
& displayLocale
,
1254 UnicodeString
& result
);
1257 * Return an array of all the locale names directly supported by this factory.
1258 * The number of names is returned in count. This array is owned by the factory.
1259 * Its contents must never change.
1260 * @param count output parameter for the number of locales supported by the factory
1261 * @param status the in/out error code
1262 * @return a pointer to an array of count UnicodeStrings.
1265 virtual const UnicodeString
* getSupportedIDs(int32_t &count
, UErrorCode
& status
) = 0;
1267 #endif /* UCONFIG_NO_SERVICE */
1269 // Collator inline methods -----------------------------------------------
1272 #endif // U_SHOW_CPLUSPLUS_API
1274 #endif /* #if !UCONFIG_NO_COLLATION */