2 ******************************************************************************
3 * Copyright (C) 1996-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
10 * \brief C++ API: Collation Service.
16 * Created by: Helena Shih
18 * Modification History:
20 * Date Name Description
21 * 02/5/97 aliu Modified createDefault to load collation data from
22 * binary files when possible. Added related methods
23 * createCollationFromFile, chopLocale, createPathName.
24 * 02/11/97 aliu Added members addToCache, findInCache, and fgCache.
25 * 02/12/97 aliu Modified to create objects from RuleBasedCollator cache.
26 * Moved cache out of Collation class.
27 * 02/13/97 aliu Moved several methods out of this class and into
28 * RuleBasedCollator, with modifications. Modified
29 * createDefault() to call new RuleBasedCollator(Locale&)
30 * constructor. General clean up and documentation.
31 * 02/20/97 helena Added clone, operator==, operator!=, operator=, copy
32 * constructor and getDynamicClassID.
33 * 03/25/97 helena Updated with platform independent data types.
34 * 05/06/97 helena Added memory allocation error detection.
35 * 06/20/97 helena Java class name change.
36 * 09/03/97 helena Added createCollationKeyValues().
37 * 02/10/98 damiba Added compare() with length as parameter.
38 * 04/23/99 stephen Removed EDecompositionMode, merged with
40 * 11/02/99 helena Collator performance enhancements. Eliminates the
41 * UnicodeString construction and special case for NO_OP.
42 * 11/23/99 srl More performance enhancements. Inlining of
44 * 05/15/00 helena Added version information API.
45 * 01/29/01 synwee Modified into a C++ wrapper which calls C apis
47 * 2012-2014 markus Rewritten in C++ again.
53 #include "unicode/utypes.h"
55 #if !UCONFIG_NO_COLLATION
57 #include "unicode/uobject.h"
58 #include "unicode/ucol.h"
59 #include "unicode/normlzr.h"
60 #include "unicode/locid.h"
61 #include "unicode/uniset.h"
62 #include "unicode/umisc.h"
63 #include "unicode/uiter.h"
64 #include "unicode/stringpiece.h"
68 class StringEnumeration
;
70 #if !UCONFIG_NO_SERVICE
74 class CollatorFactory
;
83 * The <code>Collator</code> class performs locale-sensitive string
85 * You use this class to build searching and sorting routines for natural
88 * <code>Collator</code> is an abstract base class. Subclasses implement
89 * specific collation strategies. One subclass,
90 * <code>RuleBasedCollator</code>, is currently provided and is applicable
91 * to a wide set of languages. Other subclasses may be created to handle more
94 * Like other locale-sensitive classes, you can use the static factory method,
95 * <code>createInstance</code>, to obtain the appropriate
96 * <code>Collator</code> object for a given locale. You will only need to
97 * look at the subclasses of <code>Collator</code> if you need to
98 * understand the details of a particular collation strategy or if you need to
99 * modify that strategy.
101 * The following example shows how to compare two strings using the
102 * <code>Collator</code> for the default locale.
103 * \htmlonly<blockquote>\endhtmlonly
106 * // Compare two strings in the default locale
107 * UErrorCode success = U_ZERO_ERROR;
108 * Collator* myCollator = Collator::createInstance(success);
109 * if (myCollator->compare("abc", "ABC") < 0)
110 * cout << "abc is less than ABC" << endl;
112 * cout << "abc is greater than or equal to ABC" << endl;
115 * \htmlonly</blockquote>\endhtmlonly
117 * You can set a <code>Collator</code>'s <em>strength</em> attribute to
118 * determine the level of difference considered significant in comparisons.
119 * Five strengths are provided: <code>PRIMARY</code>, <code>SECONDARY</code>,
120 * <code>TERTIARY</code>, <code>QUATERNARY</code> and <code>IDENTICAL</code>.
121 * The exact assignment of strengths to language features is locale dependent.
122 * For example, in Czech, "e" and "f" are considered primary differences,
123 * while "e" and "\u00EA" are secondary differences, "e" and "E" are tertiary
124 * differences and "e" and "e" are identical. The following shows how both case
125 * and accents could be ignored for US English.
126 * \htmlonly<blockquote>\endhtmlonly
129 * //Get the Collator for US English and set its strength to PRIMARY
130 * UErrorCode success = U_ZERO_ERROR;
131 * Collator* usCollator = Collator::createInstance(Locale::getUS(), success);
132 * usCollator->setStrength(Collator::PRIMARY);
133 * if (usCollator->compare("abc", "ABC") == 0)
134 * cout << "'abc' and 'ABC' strings are equivalent with strength PRIMARY" << endl;
137 * \htmlonly</blockquote>\endhtmlonly
139 * The <code>getSortKey</code> methods
140 * convert a string to a series of bytes that can be compared bitwise against
141 * other sort keys using <code>strcmp()</code>. Sort keys are written as
142 * zero-terminated byte strings.
144 * Another set of APIs returns a <code>CollationKey</code> object that wraps
145 * the sort key bytes instead of returning the bytes themselves.
148 * <strong>Note:</strong> <code>Collator</code>s with different Locale,
149 * and CollationStrength settings will return different sort
150 * orders for the same set of strings. Locales have specific collation rules,
151 * and the way in which secondary and tertiary differences are taken into
152 * account, for example, will result in a different sorting order for same
155 * @see RuleBasedCollator
157 * @see CollationElementIterator
160 * @version 2.0 11/15/01
163 class U_I18N_API Collator
: public UObject
{
166 // Collator public enums -----------------------------------------------
169 * Base letter represents a primary difference. Set comparison level to
170 * PRIMARY to ignore secondary and tertiary differences.<br>
171 * Use this to set the strength of a Collator object.<br>
172 * Example of primary difference, "abc" < "abd"
174 * Diacritical differences on the same base letter represent a secondary
175 * difference. Set comparison level to SECONDARY to ignore tertiary
176 * differences. Use this to set the strength of a Collator object.<br>
177 * Example of secondary difference, "ä" >> "a".
179 * Uppercase and lowercase versions of the same character represents a
180 * tertiary difference. Set comparison level to TERTIARY to include all
181 * comparison differences. Use this to set the strength of a Collator
183 * Example of tertiary difference, "abc" <<< "ABC".
185 * Two characters are considered "identical" when they have the same unicode
187 * For example, "ä" == "ä".
189 * UCollationStrength is also used to determine the strength of sort keys
190 * generated from Collator objects.
193 enum ECollationStrength
195 PRIMARY
= UCOL_PRIMARY
, // 0
196 SECONDARY
= UCOL_SECONDARY
, // 1
197 TERTIARY
= UCOL_TERTIARY
, // 2
198 QUATERNARY
= UCOL_QUATERNARY
, // 3
199 IDENTICAL
= UCOL_IDENTICAL
// 15
203 // Cannot use #ifndef U_HIDE_DEPRECATED_API for the following, it is
204 // used by virtual methods that cannot have that conditional.
206 * LESS is returned if source string is compared to be less than target
207 * string in the compare() method.
208 * EQUAL is returned if source string is compared to be equal to target
209 * string in the compare() method.
210 * GREATER is returned if source string is compared to be greater than
211 * target string in the compare() method.
212 * @see Collator#compare
213 * @deprecated ICU 2.6. Use C enum UCollationResult defined in ucol.h
215 enum EComparisonResult
217 LESS
= UCOL_LESS
, // -1
218 EQUAL
= UCOL_EQUAL
, // 0
219 GREATER
= UCOL_GREATER
// 1
222 // Collator public destructor -----------------------------------------
230 // Collator public methods --------------------------------------------
233 * Returns TRUE if "other" is the same as "this".
235 * The base class implementation returns TRUE if "other" has the same type/class as "this":
236 * <code>typeid(*this) == typeid(other)</code>.
238 * Subclass implementations should do something like the following:
240 * if (this == &other) { return TRUE; }
241 * if (!Collator::operator==(other)) { return FALSE; } // not the same class
243 * const MyCollator &o = (const MyCollator&)other;
244 * (compare this vs. o's subclass fields)
246 * @param other Collator object to be compared
247 * @return TRUE if other is the same as this.
250 virtual UBool
operator==(const Collator
& other
) const;
253 * Returns true if "other" is not the same as "this".
254 * Calls ! operator==(const Collator&) const which works for all subclasses.
255 * @param other Collator object to be compared
256 * @return TRUE if other is not the same as this.
259 virtual UBool
operator!=(const Collator
& other
) const;
262 * Makes a copy of this object.
263 * @return a copy of this object, owned by the caller
266 virtual Collator
* clone(void) const = 0;
269 * Creates the Collator object for the current default locale.
270 * The default locale is determined by Locale::getDefault.
271 * The UErrorCode& err parameter is used to return status information to the user.
272 * To check whether the construction succeeded or not, you should check the
273 * value of U_SUCCESS(err). If you wish more detailed information, you can
274 * check for informational error results which still indicate success.
275 * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
276 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
277 * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
278 * used; neither the requested locale nor any of its fall back locales
280 * The caller owns the returned object and is responsible for deleting it.
282 * @param err the error code status.
283 * @return the collation object of the default locale.(for example, en_US)
284 * @see Locale#getDefault
287 static Collator
* U_EXPORT2
createInstance(UErrorCode
& err
);
290 * Gets the collation object for the desired locale. The
291 * resource of the desired locale will be loaded.
293 * Locale::getRoot() is the base collation table and all other languages are
294 * built on top of it with additional language-specific modifications.
296 * For some languages, multiple collation types are available;
297 * for example, "de@collation=phonebook".
298 * Starting with ICU 54, collation attributes can be specified via locale keywords as well,
299 * in the old locale extension syntax ("el@colCaseFirst=upper")
300 * or in language tag syntax ("el-u-kf-upper").
301 * See <a href="http://userguide.icu-project.org/collation/api">User Guide: Collation API</a>.
303 * The UErrorCode& err parameter is used to return status information to the user.
304 * To check whether the construction succeeded or not, you should check
305 * the value of U_SUCCESS(err). If you wish more detailed information, you
306 * can check for informational error results which still indicate success.
307 * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
308 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
309 * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
310 * used; neither the requested locale nor any of its fall back locales
313 * The caller owns the returned object and is responsible for deleting it.
314 * @param loc The locale ID for which to open a collator.
315 * @param err the error code status.
316 * @return the created table-based collation object based on the desired
319 * @see ResourceLoader
322 static Collator
* U_EXPORT2
createInstance(const Locale
& loc
, UErrorCode
& err
);
325 * The comparison function compares the character data stored in two
326 * different strings. Returns information about whether a string is less
327 * than, greater than or equal to another string.
328 * @param source the source string to be compared with.
329 * @param target the string that is to be compared with the source string.
330 * @return Returns a byte value. GREATER if source is greater
331 * than target; EQUAL if source is equal to target; LESS if source is less
333 * @deprecated ICU 2.6 use the overload with UErrorCode &
335 virtual EComparisonResult
compare(const UnicodeString
& source
,
336 const UnicodeString
& target
) const;
339 * The comparison function compares the character data stored in two
340 * different strings. Returns information about whether a string is less
341 * than, greater than or equal to another string.
342 * @param source the source string to be compared with.
343 * @param target the string that is to be compared with the source string.
344 * @param status possible error code
345 * @return Returns an enum value. UCOL_GREATER if source is greater
346 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
350 virtual UCollationResult
compare(const UnicodeString
& source
,
351 const UnicodeString
& target
,
352 UErrorCode
&status
) const = 0;
355 * Does the same thing as compare but limits the comparison to a specified
357 * @param source the source string to be compared with.
358 * @param target the string that is to be compared with the source string.
359 * @param length the length the comparison is limited to
360 * @return Returns a byte value. GREATER if source (up to the specified
361 * length) is greater than target; EQUAL if source (up to specified
362 * length) is equal to target; LESS if source (up to the specified
363 * length) is less than target.
364 * @deprecated ICU 2.6 use the overload with UErrorCode &
366 virtual EComparisonResult
compare(const UnicodeString
& source
,
367 const UnicodeString
& target
,
368 int32_t length
) const;
371 * Does the same thing as compare but limits the comparison to a specified
373 * @param source the source string to be compared with.
374 * @param target the string that is to be compared with the source string.
375 * @param length the length the comparison is limited to
376 * @param status possible error code
377 * @return Returns an enum value. UCOL_GREATER if source (up to the specified
378 * length) is greater than target; UCOL_EQUAL if source (up to specified
379 * length) is equal to target; UCOL_LESS if source (up to the specified
380 * length) is less than target.
383 virtual UCollationResult
compare(const UnicodeString
& source
,
384 const UnicodeString
& target
,
386 UErrorCode
&status
) const = 0;
389 * The comparison function compares the character data stored in two
390 * different string arrays. Returns information about whether a string array
391 * is less than, greater than or equal to another string array.
394 * . UChar ABC[] = {0x41, 0x42, 0x43, 0}; // = "ABC"
395 * . UChar abc[] = {0x61, 0x62, 0x63, 0}; // = "abc"
396 * . UErrorCode status = U_ZERO_ERROR;
397 * . Collator *myCollation =
398 * . Collator::createInstance(Locale::getUS(), status);
399 * . if (U_FAILURE(status)) return;
400 * . myCollation->setStrength(Collator::PRIMARY);
401 * . // result would be Collator::EQUAL ("abc" == "ABC")
402 * . // (no primary difference between "abc" and "ABC")
403 * . Collator::EComparisonResult result =
404 * . myCollation->compare(abc, 3, ABC, 3);
405 * . myCollation->setStrength(Collator::TERTIARY);
406 * . // result would be Collator::LESS ("abc" <<< "ABC")
407 * . // (with tertiary difference between "abc" and "ABC")
408 * . result = myCollation->compare(abc, 3, ABC, 3);
410 * @param source the source string array to be compared with.
411 * @param sourceLength the length of the source string array. If this value
412 * is equal to -1, the string array is null-terminated.
413 * @param target the string that is to be compared with the source string.
414 * @param targetLength the length of the target string array. If this value
415 * is equal to -1, the string array is null-terminated.
416 * @return Returns a byte value. GREATER if source is greater than target;
417 * EQUAL if source is equal to target; LESS if source is less than
419 * @deprecated ICU 2.6 use the overload with UErrorCode &
421 virtual EComparisonResult
compare(const UChar
* source
, int32_t sourceLength
,
422 const UChar
* target
, int32_t targetLength
)
426 * The comparison function compares the character data stored in two
427 * different string arrays. Returns information about whether a string array
428 * is less than, greater than or equal to another string array.
429 * @param source the source string array to be compared with.
430 * @param sourceLength the length of the source string array. If this value
431 * is equal to -1, the string array is null-terminated.
432 * @param target the string that is to be compared with the source string.
433 * @param targetLength the length of the target string array. If this value
434 * is equal to -1, the string array is null-terminated.
435 * @param status possible error code
436 * @return Returns an enum value. UCOL_GREATER if source is greater
437 * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
441 virtual UCollationResult
compare(const UChar
* source
, int32_t sourceLength
,
442 const UChar
* target
, int32_t targetLength
,
443 UErrorCode
&status
) const = 0;
446 * Compares two strings using the Collator.
447 * Returns whether the first one compares less than/equal to/greater than
449 * This version takes UCharIterator input.
450 * @param sIter the first ("source") string iterator
451 * @param tIter the second ("target") string iterator
452 * @param status ICU status
453 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
456 virtual UCollationResult
compare(UCharIterator
&sIter
,
457 UCharIterator
&tIter
,
458 UErrorCode
&status
) const;
461 * Compares two UTF-8 strings using the Collator.
462 * Returns whether the first one compares less than/equal to/greater than
464 * This version takes UTF-8 input.
465 * Note that a StringPiece can be implicitly constructed
466 * from a std::string or a NUL-terminated const char * string.
467 * @param source the first UTF-8 string
468 * @param target the second UTF-8 string
469 * @param status ICU status
470 * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
473 virtual UCollationResult
compareUTF8(const StringPiece
&source
,
474 const StringPiece
&target
,
475 UErrorCode
&status
) const;
478 * Transforms the string into a series of characters that can be compared
479 * with CollationKey::compareTo. It is not possible to restore the original
480 * string from the chars in the sort key.
481 * <p>Use CollationKey::equals or CollationKey::compare to compare the
482 * generated sort keys.
483 * If the source string is null, a null collation key will be returned.
485 * Note that sort keys are often less efficient than simply doing comparison.
486 * For more details, see the ICU User Guide.
488 * @param source the source string to be transformed into a sort key.
489 * @param key the collation key to be filled in
490 * @param status the error code status.
491 * @return the collation key of the string based on the collation rules.
492 * @see CollationKey#compare
495 virtual CollationKey
& getCollationKey(const UnicodeString
& source
,
497 UErrorCode
& status
) const = 0;
500 * Transforms the string into a series of characters that can be compared
501 * with CollationKey::compareTo. It is not possible to restore the original
502 * string from the chars in the sort key.
503 * <p>Use CollationKey::equals or CollationKey::compare to compare the
504 * generated sort keys.
505 * <p>If the source string is null, a null collation key will be returned.
507 * Note that sort keys are often less efficient than simply doing comparison.
508 * For more details, see the ICU User Guide.
510 * @param source the source string to be transformed into a sort key.
511 * @param sourceLength length of the collation key
512 * @param key the collation key to be filled in
513 * @param status the error code status.
514 * @return the collation key of the string based on the collation rules.
515 * @see CollationKey#compare
518 virtual CollationKey
& getCollationKey(const UChar
*source
,
519 int32_t sourceLength
,
521 UErrorCode
& status
) const = 0;
523 * Generates the hash code for the collation object
526 virtual int32_t hashCode(void) const = 0;
529 * Gets the locale of the Collator
531 * @param type can be either requested, valid or actual locale. For more
532 * information see the definition of ULocDataLocaleType in
534 * @param status the error code status.
535 * @return locale where the collation data lives. If the collator
536 * was instantiated from rules, locale is empty.
537 * @deprecated ICU 2.8 This API is under consideration for revision
540 virtual Locale
getLocale(ULocDataLocaleType type
, UErrorCode
& status
) const = 0;
543 * Convenience method for comparing two strings based on the collation rules.
544 * @param source the source string to be compared with.
545 * @param target the target string to be compared with.
546 * @return true if the first string is greater than the second one,
547 * according to the collation rules. false, otherwise.
548 * @see Collator#compare
551 UBool
greater(const UnicodeString
& source
, const UnicodeString
& target
)
555 * Convenience method for comparing two strings based on the collation rules.
556 * @param source the source string to be compared with.
557 * @param target the target string to be compared with.
558 * @return true if the first string is greater than or equal to the second
559 * one, according to the collation rules. false, otherwise.
560 * @see Collator#compare
563 UBool
greaterOrEqual(const UnicodeString
& source
,
564 const UnicodeString
& target
) const;
567 * Convenience method for comparing two strings based on the collation rules.
568 * @param source the source string to be compared with.
569 * @param target the target string to be compared with.
570 * @return true if the strings are equal according to the collation rules.
572 * @see Collator#compare
575 UBool
equals(const UnicodeString
& source
, const UnicodeString
& target
) const;
578 * Determines the minimum strength that will be used in comparison or
580 * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
581 * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
583 * @return the current comparison level.
584 * @see Collator#setStrength
585 * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
587 virtual ECollationStrength
getStrength(void) const;
590 * Sets the minimum strength to be used in comparison or transformation.
594 * UErrorCode status = U_ZERO_ERROR;
595 * Collator*myCollation = Collator::createInstance(Locale::getUS(), status);
596 * if (U_FAILURE(status)) return;
597 * myCollation->setStrength(Collator::PRIMARY);
598 * // result will be "abc" == "ABC"
599 * // tertiary differences will be ignored
600 * Collator::ComparisonResult result = myCollation->compare("abc", "ABC");
603 * @see Collator#getStrength
604 * @param newStrength the new comparison level.
605 * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
607 virtual void setStrength(ECollationStrength newStrength
);
610 * Retrieves the reordering codes for this collator.
611 * @param dest The array to fill with the script ordering.
612 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the function
613 * will only return the length of the result without writing any codes (pre-flighting).
614 * @param status A reference to an error code value, which must not indicate
615 * a failure before the function call.
616 * @return The length of the script ordering array.
617 * @see ucol_setReorderCodes
618 * @see Collator#getEquivalentReorderCodes
619 * @see Collator#setReorderCodes
621 * @see UColReorderCode
624 virtual int32_t getReorderCodes(int32_t *dest
,
625 int32_t destCapacity
,
626 UErrorCode
& status
) const;
629 * Sets the ordering of scripts for this collator.
631 * <p>The reordering codes are a combination of script codes and reorder codes.
632 * @param reorderCodes An array of script codes in the new order. This can be NULL if the
633 * length is also set to 0. An empty array will clear any reordering codes on the collator.
634 * @param reorderCodesLength The length of reorderCodes.
635 * @param status error code
636 * @see ucol_setReorderCodes
637 * @see Collator#getReorderCodes
638 * @see Collator#getEquivalentReorderCodes
640 * @see UColReorderCode
643 virtual void setReorderCodes(const int32_t* reorderCodes
,
644 int32_t reorderCodesLength
,
645 UErrorCode
& status
) ;
648 * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
649 * codes will be grouped and must reorder together.
650 * Beginning with ICU 55, scripts only reorder together if they are primary-equal,
651 * for example Hiragana and Katakana.
653 * @param reorderCode The reorder code to determine equivalence for.
654 * @param dest The array to fill with the script equivalence reordering codes.
655 * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the
656 * function will only return the length of the result without writing any codes (pre-flighting).
657 * @param status A reference to an error code value, which must not indicate
658 * a failure before the function call.
659 * @return The length of the of the reordering code equivalence array.
660 * @see ucol_setReorderCodes
661 * @see Collator#getReorderCodes
662 * @see Collator#setReorderCodes
664 * @see UColReorderCode
667 static int32_t U_EXPORT2
getEquivalentReorderCodes(int32_t reorderCode
,
669 int32_t destCapacity
,
673 * Get name of the object for the desired Locale, in the desired langauge
674 * @param objectLocale must be from getAvailableLocales
675 * @param displayLocale specifies the desired locale for output
676 * @param name the fill-in parameter of the return value
677 * @return display-able name of the object for the object locale in the
681 static UnicodeString
& U_EXPORT2
getDisplayName(const Locale
& objectLocale
,
682 const Locale
& displayLocale
,
683 UnicodeString
& name
);
686 * Get name of the object for the desired Locale, in the langauge of the
688 * @param objectLocale must be from getAvailableLocales
689 * @param name the fill-in parameter of the return value
690 * @return name of the object for the desired locale in the default language
693 static UnicodeString
& U_EXPORT2
getDisplayName(const Locale
& objectLocale
,
694 UnicodeString
& name
);
697 * Get the set of Locales for which Collations are installed.
699 * <p>Note this does not include locales supported by registered collators.
700 * If collators might have been registered, use the overload of getAvailableLocales
701 * that returns a StringEnumeration.</p>
703 * @param count the output parameter of number of elements in the locale list
704 * @return the list of available locales for which collations are installed
707 static const Locale
* U_EXPORT2
getAvailableLocales(int32_t& count
);
710 * Return a StringEnumeration over the locales available at the time of the call,
711 * including registered locales. If a severe error occurs (such as out of memory
712 * condition) this will return null. If there is no locale data, an empty enumeration
714 * @return a StringEnumeration over the locales available at the time of the call
717 static StringEnumeration
* U_EXPORT2
getAvailableLocales(void);
720 * Create a string enumerator of all possible keywords that are relevant to
721 * collation. At this point, the only recognized keyword for this
722 * service is "collation".
723 * @param status input-output error code
724 * @return a string enumeration over locale strings. The caller is
725 * responsible for closing the result.
728 static StringEnumeration
* U_EXPORT2
getKeywords(UErrorCode
& status
);
731 * Given a keyword, create a string enumeration of all values
732 * for that keyword that are currently in use.
733 * @param keyword a particular keyword as enumerated by
734 * ucol_getKeywords. If any other keyword is passed in, status is set
735 * to U_ILLEGAL_ARGUMENT_ERROR.
736 * @param status input-output error code
737 * @return a string enumeration over collation keyword values, or NULL
738 * upon error. The caller is responsible for deleting the result.
741 static StringEnumeration
* U_EXPORT2
getKeywordValues(const char *keyword
, UErrorCode
& status
);
744 * Given a key and a locale, returns an array of string values in a preferred
745 * order that would make a difference. These are all and only those values where
746 * the open (creation) of the service with the locale formed from the input locale
747 * plus input keyword and that value has different behavior than creation with the
748 * input locale alone.
749 * @param keyword one of the keys supported by this service. For now, only
750 * "collation" is supported.
751 * @param locale the locale
752 * @param commonlyUsed if set to true it will return only commonly used values
753 * with the given locale in preferred order. Otherwise,
754 * it will return all the available values for the locale.
755 * @param status ICU status
756 * @return a string enumeration over keyword values for the given key and the locale.
759 static StringEnumeration
* U_EXPORT2
getKeywordValuesForLocale(const char* keyword
, const Locale
& locale
,
760 UBool commonlyUsed
, UErrorCode
& status
);
763 * Return the functionally equivalent locale for the given
764 * requested locale, with respect to given keyword, for the
765 * collation service. If two locales return the same result, then
766 * collators instantiated for these locales will behave
767 * equivalently. The converse is not always true; two collators
768 * may in fact be equivalent, but return different results, due to
769 * internal details. The return result has no other meaning than
770 * that stated above, and implies nothing as to the relationship
771 * between the two locales. This is intended for use by
772 * applications who wish to cache collators, or otherwise reuse
773 * collators when possible. The functional equivalent may change
774 * over time. For more information, please see the <a
775 * href="http://userguide.icu-project.org/locale#TOC-Locales-and-Services">
776 * Locales and Services</a> section of the ICU User Guide.
777 * @param keyword a particular keyword as enumerated by
779 * @param locale the requested locale
780 * @param isAvailable reference to a fillin parameter that
781 * indicates whether the requested locale was 'available' to the
782 * collation service. A locale is defined as 'available' if it
783 * physically exists within the collation locale data.
784 * @param status reference to input-output error code
785 * @return the functionally equivalent collation locale, or the root
789 static Locale U_EXPORT2
getFunctionalEquivalent(const char* keyword
, const Locale
& locale
,
790 UBool
& isAvailable
, UErrorCode
& status
);
792 #if !UCONFIG_NO_SERVICE
794 * Register a new Collator. The collator will be adopted.
795 * Because ICU may choose to cache collators internally, this must be
796 * called at application startup, prior to any calls to
797 * Collator::createInstance to avoid undefined behavior.
798 * @param toAdopt the Collator instance to be adopted
799 * @param locale the locale with which the collator will be associated
800 * @param status the in/out status code, no special meanings are assigned
801 * @return a registry key that can be used to unregister this collator
804 static URegistryKey U_EXPORT2
registerInstance(Collator
* toAdopt
, const Locale
& locale
, UErrorCode
& status
);
807 * Register a new CollatorFactory. The factory will be adopted.
808 * Because ICU may choose to cache collators internally, this must be
809 * called at application startup, prior to any calls to
810 * Collator::createInstance to avoid undefined behavior.
811 * @param toAdopt the CollatorFactory instance to be adopted
812 * @param status the in/out status code, no special meanings are assigned
813 * @return a registry key that can be used to unregister this collator
816 static URegistryKey U_EXPORT2
registerFactory(CollatorFactory
* toAdopt
, UErrorCode
& status
);
819 * Unregister a previously-registered Collator or CollatorFactory
820 * using the key returned from the register call. Key becomes
821 * invalid after a successful call and should not be used again.
822 * The object corresponding to the key will be deleted.
823 * Because ICU may choose to cache collators internally, this should
824 * be called during application shutdown, after all calls to
825 * Collator::createInstance to avoid undefined behavior.
826 * @param key the registry key returned by a previous call to registerInstance
827 * @param status the in/out status code, no special meanings are assigned
828 * @return TRUE if the collator for the key was successfully unregistered
831 static UBool U_EXPORT2
unregister(URegistryKey key
, UErrorCode
& status
);
832 #endif /* UCONFIG_NO_SERVICE */
835 * Gets the version information for a Collator.
836 * @param info the version # information, the result will be filled in
839 virtual void getVersion(UVersionInfo info
) const = 0;
842 * Returns a unique class ID POLYMORPHICALLY. Pure virtual method.
843 * This method is to implement a simple version of RTTI, since not all C++
844 * compilers support genuine RTTI. Polymorphic operator==() and clone()
845 * methods call this method.
846 * @return The class ID for this object. All objects of a given class have
847 * the same class ID. Objects of other classes have different class
851 virtual UClassID
getDynamicClassID(void) const = 0;
854 * Universal attribute setter
855 * @param attr attribute type
856 * @param value attribute value
857 * @param status to indicate whether the operation went on smoothly or
861 virtual void setAttribute(UColAttribute attr
, UColAttributeValue value
,
862 UErrorCode
&status
) = 0;
865 * Universal attribute getter
866 * @param attr attribute type
867 * @param status to indicate whether the operation went on smoothly or
869 * @return attribute value
872 virtual UColAttributeValue
getAttribute(UColAttribute attr
,
873 UErrorCode
&status
) const = 0;
876 * Sets the variable top to the top of the specified reordering group.
877 * The variable top determines the highest-sorting character
878 * which is affected by UCOL_ALTERNATE_HANDLING.
879 * If that attribute is set to UCOL_NON_IGNORABLE, then the variable top has no effect.
881 * The base class implementation sets U_UNSUPPORTED_ERROR.
882 * @param group one of UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION,
883 * UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_CURRENCY;
884 * or UCOL_REORDER_CODE_DEFAULT to restore the default max variable group
885 * @param errorCode Standard ICU error code. Its input value must
886 * pass the U_SUCCESS() test, or else the function returns
887 * immediately. Check for U_FAILURE() on output or use with
888 * function chaining. (See User Guide for details.)
890 * @see getMaxVariable
893 virtual Collator
&setMaxVariable(UColReorderCode group
, UErrorCode
&errorCode
);
896 * Returns the maximum reordering group whose characters are affected by UCOL_ALTERNATE_HANDLING.
898 * The base class implementation returns UCOL_REORDER_CODE_PUNCTUATION.
899 * @return the maximum variable reordering group.
900 * @see setMaxVariable
903 virtual UColReorderCode
getMaxVariable() const;
906 * Sets the variable top to the primary weight of the specified string.
908 * Beginning with ICU 53, the variable top is pinned to
909 * the top of one of the supported reordering groups,
910 * and it must not be beyond the last of those groups.
911 * See setMaxVariable().
912 * @param varTop one or more (if contraction) UChars to which the variable top should be set
913 * @param len length of variable top string. If -1 it is considered to be zero terminated.
914 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
915 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
916 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
917 * the last reordering group supported by setMaxVariable()
918 * @return variable top primary weight
919 * @deprecated ICU 53 Call setMaxVariable() instead.
921 virtual uint32_t setVariableTop(const UChar
*varTop
, int32_t len
, UErrorCode
&status
) = 0;
924 * Sets the variable top to the primary weight of the specified string.
926 * Beginning with ICU 53, the variable top is pinned to
927 * the top of one of the supported reordering groups,
928 * and it must not be beyond the last of those groups.
929 * See setMaxVariable().
930 * @param varTop a UnicodeString size 1 or more (if contraction) of UChars to which the variable top should be set
931 * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
932 * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
933 * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
934 * the last reordering group supported by setMaxVariable()
935 * @return variable top primary weight
936 * @deprecated ICU 53 Call setMaxVariable() instead.
938 virtual uint32_t setVariableTop(const UnicodeString
&varTop
, UErrorCode
&status
) = 0;
941 * Sets the variable top to the specified primary weight.
943 * Beginning with ICU 53, the variable top is pinned to
944 * the top of one of the supported reordering groups,
945 * and it must not be beyond the last of those groups.
946 * See setMaxVariable().
947 * @param varTop primary weight, as returned by setVariableTop or ucol_getVariableTop
948 * @param status error code
949 * @deprecated ICU 53 Call setMaxVariable() instead.
951 virtual void setVariableTop(uint32_t varTop
, UErrorCode
&status
) = 0;
954 * Gets the variable top value of a Collator.
955 * @param status error code (not changed by function). If error code is set, the return value is undefined.
956 * @return the variable top primary weight
957 * @see getMaxVariable
960 virtual uint32_t getVariableTop(UErrorCode
&status
) const = 0;
963 * Get a UnicodeSet that contains all the characters and sequences
964 * tailored in this collator.
965 * @param status error code of the operation
966 * @return a pointer to a UnicodeSet object containing all the
967 * code points and sequences that may sort differently than
968 * in the root collator. The object must be disposed of by using delete
971 virtual UnicodeSet
*getTailoredSet(UErrorCode
&status
) const;
975 * The base class implementation simply calls clone().
976 * @return a copy of this object, owned by the caller
978 * @deprecated ICU 50 no need to have two methods for cloning
980 virtual Collator
* safeClone(void) const;
983 * Get the sort key as an array of bytes from a UnicodeString.
984 * Sort key byte arrays are zero-terminated and can be compared using
987 * Note that sort keys are often less efficient than simply doing comparison.
988 * For more details, see the ICU User Guide.
990 * @param source string to be processed.
991 * @param result buffer to store result in. If NULL, number of bytes needed
993 * @param resultLength length of the result buffer. If if not enough the
994 * buffer will be filled to capacity.
995 * @return Number of bytes needed for storing the sort key
998 virtual int32_t getSortKey(const UnicodeString
& source
,
1000 int32_t resultLength
) const = 0;
1003 * Get the sort key as an array of bytes from a UChar buffer.
1004 * Sort key byte arrays are zero-terminated and can be compared using
1007 * Note that sort keys are often less efficient than simply doing comparison.
1008 * For more details, see the ICU User Guide.
1010 * @param source string to be processed.
1011 * @param sourceLength length of string to be processed.
1012 * If -1, the string is 0 terminated and length will be decided by the
1014 * @param result buffer to store result in. If NULL, number of bytes needed
1016 * @param resultLength length of the result buffer. If if not enough the
1017 * buffer will be filled to capacity.
1018 * @return Number of bytes needed for storing the sort key
1021 virtual int32_t getSortKey(const UChar
*source
, int32_t sourceLength
,
1022 uint8_t*result
, int32_t resultLength
) const = 0;
1025 * Produce a bound for a given sortkey and a number of levels.
1026 * Return value is always the number of bytes needed, regardless of
1027 * whether the result buffer was big enough or even valid.<br>
1028 * Resulting bounds can be used to produce a range of strings that are
1029 * between upper and lower bounds. For example, if bounds are produced
1030 * for a sortkey of string "smith", strings between upper and lower
1031 * bounds with one level would include "Smith", "SMITH", "sMiTh".<br>
1032 * There are two upper bounds that can be produced. If UCOL_BOUND_UPPER
1033 * is produced, strings matched would be as above. However, if bound
1034 * produced using UCOL_BOUND_UPPER_LONG is used, the above example will
1035 * also match "Smithsonian" and similar.<br>
1036 * For more on usage, see example in cintltst/capitst.c in procedure
1038 * Sort keys may be compared using <TT>strcmp</TT>.
1039 * @param source The source sortkey.
1040 * @param sourceLength The length of source, or -1 if null-terminated.
1041 * (If an unmodified sortkey is passed, it is always null
1043 * @param boundType Type of bound required. It can be UCOL_BOUND_LOWER, which
1044 * produces a lower inclusive bound, UCOL_BOUND_UPPER, that
1045 * produces upper bound that matches strings of the same length
1046 * or UCOL_BOUND_UPPER_LONG that matches strings that have the
1047 * same starting substring as the source string.
1048 * @param noOfLevels Number of levels required in the resulting bound (for most
1049 * uses, the recommended value is 1). See users guide for
1050 * explanation on number of levels a sortkey can have.
1051 * @param result A pointer to a buffer to receive the resulting sortkey.
1052 * @param resultLength The maximum size of result.
1053 * @param status Used for returning error code if something went wrong. If the
1054 * number of levels requested is higher than the number of levels
1055 * in the source key, a warning (U_SORT_KEY_TOO_SHORT_WARNING) is
1057 * @return The size needed to fully store the bound.
1058 * @see ucol_keyHashCode
1061 static int32_t U_EXPORT2
getBound(const uint8_t *source
,
1062 int32_t sourceLength
,
1063 UColBoundMode boundType
,
1064 uint32_t noOfLevels
,
1066 int32_t resultLength
,
1067 UErrorCode
&status
);
1072 // Collator protected constructors -------------------------------------
1075 * Default constructor.
1076 * Constructor is different from the old default Collator constructor.
1077 * The task for determing the default collation strength and normalization
1078 * mode is left to the child class.
1083 #ifndef U_HIDE_DEPRECATED_API
1086 * Empty constructor, does not handle the arguments.
1087 * This constructor is done for backward compatibility with 1.7 and 1.8.
1088 * The task for handling the argument collation strength and normalization
1089 * mode is left to the child class.
1090 * @param collationStrength collation strength
1091 * @param decompositionMode
1092 * @deprecated ICU 2.4. Subclasses should use the default constructor
1093 * instead and handle the strength and normalization mode themselves.
1095 Collator(UCollationStrength collationStrength
,
1096 UNormalizationMode decompositionMode
);
1097 #endif /* U_HIDE_DEPRECATED_API */
1101 * @param other Collator object to be copied from
1104 Collator(const Collator
& other
);
1108 * Used internally by registration to define the requested and valid locales.
1109 * @param requestedLocale the requested locale
1110 * @param validLocale the valid locale
1111 * @param actualLocale the actual locale
1114 virtual void setLocales(const Locale
& requestedLocale
, const Locale
& validLocale
, const Locale
& actualLocale
);
1116 /** Get the short definition string for a collator. This internal API harvests the collator's
1117 * locale and the attribute set and produces a string that can be used for opening
1118 * a collator with the same attributes using the ucol_openFromShortString API.
1119 * This string will be normalized.
1120 * The structure and the syntax of the string is defined in the "Naming collators"
1121 * section of the users guide:
1122 * http://userguide.icu-project.org/collation/concepts#TOC-Collator-naming-scheme
1123 * This function supports preflighting.
1125 * This is internal, and intended to be used with delegate converters.
1127 * @param locale a locale that will appear as a collators locale in the resulting
1128 * short string definition. If NULL, the locale will be harvested
1129 * from the collator.
1130 * @param buffer space to hold the resulting string
1131 * @param capacity capacity of the buffer
1132 * @param status for returning errors. All the preflighting errors are featured
1133 * @return length of the resulting string
1134 * @see ucol_openFromShortString
1135 * @see ucol_normalizeShortDefinitionString
1136 * @see ucol_getShortDefinitionString
1139 virtual int32_t internalGetShortDefinitionString(const char *locale
,
1142 UErrorCode
&status
) const;
1145 * Implements ucol_strcollUTF8().
1148 virtual UCollationResult
internalCompareUTF8(
1149 const char *left
, int32_t leftLength
,
1150 const char *right
, int32_t rightLength
,
1151 UErrorCode
&errorCode
) const;
1154 * Implements ucol_nextSortKeyPart().
1158 internalNextSortKeyPart(
1159 UCharIterator
*iter
, uint32_t state
[2],
1160 uint8_t *dest
, int32_t count
, UErrorCode
&errorCode
) const;
1162 #ifndef U_HIDE_INTERNAL_API
1164 static inline Collator
*fromUCollator(UCollator
*uc
) {
1165 return reinterpret_cast<Collator
*>(uc
);
1168 static inline const Collator
*fromUCollator(const UCollator
*uc
) {
1169 return reinterpret_cast<const Collator
*>(uc
);
1172 inline UCollator
*toUCollator() {
1173 return reinterpret_cast<UCollator
*>(this);
1176 inline const UCollator
*toUCollator() const {
1177 return reinterpret_cast<const UCollator
*>(this);
1179 #endif // U_HIDE_INTERNAL_API
1183 * Assignment operator. Private for now.
1185 Collator
& operator=(const Collator
& other
);
1187 friend class CFactory
;
1188 friend class SimpleCFactory
;
1189 friend class ICUCollatorFactory
;
1190 friend class ICUCollatorService
;
1191 static Collator
* makeInstance(const Locale
& desiredLocale
,
1192 UErrorCode
& status
);
1195 #if !UCONFIG_NO_SERVICE
1197 * A factory, used with registerFactory, the creates multiple collators and provides
1198 * display names for them. A factory supports some number of locales-- these are the
1199 * locales for which it can create collators. The factory can be visible, in which
1200 * case the supported locales will be enumerated by getAvailableLocales, or invisible,
1201 * in which they are not. Invisible locales are still supported, they are just not
1202 * listed by getAvailableLocales.
1204 * If standard locale display names are sufficient, Collator instances can
1205 * be registered using registerInstance instead.</p>
1207 * Note: if the collators are to be used from C APIs, they must be instances
1208 * of RuleBasedCollator.</p>
1212 class U_I18N_API CollatorFactory
: public UObject
{
1219 virtual ~CollatorFactory();
1222 * Return true if this factory is visible. Default is true.
1223 * If not visible, the locales supported by this factory will not
1224 * be listed by getAvailableLocales.
1225 * @return true if the factory is visible.
1228 virtual UBool
visible(void) const;
1231 * Return a collator for the provided locale. If the locale
1232 * is not supported, return NULL.
1233 * @param loc the locale identifying the collator to be created.
1234 * @return a new collator if the locale is supported, otherwise NULL.
1237 virtual Collator
* createCollator(const Locale
& loc
) = 0;
1240 * Return the name of the collator for the objectLocale, localized for the displayLocale.
1241 * If objectLocale is not supported, or the factory is not visible, set the result string
1243 * @param objectLocale the locale identifying the collator
1244 * @param displayLocale the locale for which the display name of the collator should be localized
1245 * @param result an output parameter for the display name, set to bogus if not supported.
1246 * @return the display name
1249 virtual UnicodeString
& getDisplayName(const Locale
& objectLocale
,
1250 const Locale
& displayLocale
,
1251 UnicodeString
& result
);
1254 * Return an array of all the locale names directly supported by this factory.
1255 * The number of names is returned in count. This array is owned by the factory.
1256 * Its contents must never change.
1257 * @param count output parameter for the number of locales supported by the factory
1258 * @param status the in/out error code
1259 * @return a pointer to an array of count UnicodeStrings.
1262 virtual const UnicodeString
* getSupportedIDs(int32_t &count
, UErrorCode
& status
) = 0;
1264 #endif /* UCONFIG_NO_SERVICE */
1266 // Collator inline methods -----------------------------------------------
1270 #endif /* #if !UCONFIG_NO_COLLATION */