coll.h 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. ******************************************************************************
  3. * Copyright (C) 1996-2016, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. ******************************************************************************
  6. */
  7. /**
  8. * \file
  9. * \brief C++ API: Collation Service.
  10. */
  11. /**
  12. * File coll.h
  13. *
  14. * Created by: Helena Shih
  15. *
  16. * Modification History:
  17. *
  18. * Date Name Description
  19. * 02/5/97 aliu Modified createDefault to load collation data from
  20. * binary files when possible. Added related methods
  21. * createCollationFromFile, chopLocale, createPathName.
  22. * 02/11/97 aliu Added members addToCache, findInCache, and fgCache.
  23. * 02/12/97 aliu Modified to create objects from RuleBasedCollator cache.
  24. * Moved cache out of Collation class.
  25. * 02/13/97 aliu Moved several methods out of this class and into
  26. * RuleBasedCollator, with modifications. Modified
  27. * createDefault() to call new RuleBasedCollator(Locale&)
  28. * constructor. General clean up and documentation.
  29. * 02/20/97 helena Added clone, operator==, operator!=, operator=, copy
  30. * constructor and getDynamicClassID.
  31. * 03/25/97 helena Updated with platform independent data types.
  32. * 05/06/97 helena Added memory allocation error detection.
  33. * 06/20/97 helena Java class name change.
  34. * 09/03/97 helena Added createCollationKeyValues().
  35. * 02/10/98 damiba Added compare() with length as parameter.
  36. * 04/23/99 stephen Removed EDecompositionMode, merged with
  37. * Normalizer::EMode.
  38. * 11/02/99 helena Collator performance enhancements. Eliminates the
  39. * UnicodeString construction and special case for NO_OP.
  40. * 11/23/99 srl More performance enhancements. Inlining of
  41. * critical accessors.
  42. * 05/15/00 helena Added version information API.
  43. * 01/29/01 synwee Modified into a C++ wrapper which calls C apis
  44. * (ucol.h).
  45. * 2012-2014 markus Rewritten in C++ again.
  46. */
  47. #ifndef COLL_H
  48. #define COLL_H
  49. #include "unicode/utypes.h"
  50. #if !UCONFIG_NO_COLLATION
  51. #include "unicode/uobject.h"
  52. #include "unicode/ucol.h"
  53. #include "unicode/normlzr.h"
  54. #include "unicode/locid.h"
  55. #include "unicode/uniset.h"
  56. #include "unicode/umisc.h"
  57. #include "unicode/uiter.h"
  58. #include "unicode/stringpiece.h"
  59. U_NAMESPACE_BEGIN
  60. class StringEnumeration;
  61. #if !UCONFIG_NO_SERVICE
  62. /**
  63. * @stable ICU 2.6
  64. */
  65. class CollatorFactory;
  66. #endif
  67. /**
  68. * @stable ICU 2.0
  69. */
  70. class CollationKey;
  71. /**
  72. * The <code>Collator</code> class performs locale-sensitive string
  73. * comparison.<br>
  74. * You use this class to build searching and sorting routines for natural
  75. * language text.
  76. * <p>
  77. * <code>Collator</code> is an abstract base class. Subclasses implement
  78. * specific collation strategies. One subclass,
  79. * <code>RuleBasedCollator</code>, is currently provided and is applicable
  80. * to a wide set of languages. Other subclasses may be created to handle more
  81. * specialized needs.
  82. * <p>
  83. * Like other locale-sensitive classes, you can use the static factory method,
  84. * <code>createInstance</code>, to obtain the appropriate
  85. * <code>Collator</code> object for a given locale. You will only need to
  86. * look at the subclasses of <code>Collator</code> if you need to
  87. * understand the details of a particular collation strategy or if you need to
  88. * modify that strategy.
  89. * <p>
  90. * The following example shows how to compare two strings using the
  91. * <code>Collator</code> for the default locale.
  92. * \htmlonly<blockquote>\endhtmlonly
  93. * <pre>
  94. * \code
  95. * // Compare two strings in the default locale
  96. * UErrorCode success = U_ZERO_ERROR;
  97. * Collator* myCollator = Collator::createInstance(success);
  98. * if (myCollator->compare("abc", "ABC") < 0)
  99. * cout << "abc is less than ABC" << endl;
  100. * else
  101. * cout << "abc is greater than or equal to ABC" << endl;
  102. * \endcode
  103. * </pre>
  104. * \htmlonly</blockquote>\endhtmlonly
  105. * <p>
  106. * You can set a <code>Collator</code>'s <em>strength</em> attribute to
  107. * determine the level of difference considered significant in comparisons.
  108. * Five strengths are provided: <code>PRIMARY</code>, <code>SECONDARY</code>,
  109. * <code>TERTIARY</code>, <code>QUATERNARY</code> and <code>IDENTICAL</code>.
  110. * The exact assignment of strengths to language features is locale dependent.
  111. * For example, in Czech, "e" and "f" are considered primary differences,
  112. * while "e" and "\u00EA" are secondary differences, "e" and "E" are tertiary
  113. * differences and "e" and "e" are identical. The following shows how both case
  114. * and accents could be ignored for US English.
  115. * \htmlonly<blockquote>\endhtmlonly
  116. * <pre>
  117. * \code
  118. * //Get the Collator for US English and set its strength to PRIMARY
  119. * UErrorCode success = U_ZERO_ERROR;
  120. * Collator* usCollator = Collator::createInstance(Locale::getUS(), success);
  121. * usCollator->setStrength(Collator::PRIMARY);
  122. * if (usCollator->compare("abc", "ABC") == 0)
  123. * cout << "'abc' and 'ABC' strings are equivalent with strength PRIMARY" << endl;
  124. * \endcode
  125. * </pre>
  126. * \htmlonly</blockquote>\endhtmlonly
  127. *
  128. * The <code>getSortKey</code> methods
  129. * convert a string to a series of bytes that can be compared bitwise against
  130. * other sort keys using <code>strcmp()</code>. Sort keys are written as
  131. * zero-terminated byte strings.
  132. *
  133. * Another set of APIs returns a <code>CollationKey</code> object that wraps
  134. * the sort key bytes instead of returning the bytes themselves.
  135. * </p>
  136. * <p>
  137. * <strong>Note:</strong> <code>Collator</code>s with different Locale,
  138. * and CollationStrength settings will return different sort
  139. * orders for the same set of strings. Locales have specific collation rules,
  140. * and the way in which secondary and tertiary differences are taken into
  141. * account, for example, will result in a different sorting order for same
  142. * strings.
  143. * </p>
  144. * @see RuleBasedCollator
  145. * @see CollationKey
  146. * @see CollationElementIterator
  147. * @see Locale
  148. * @see Normalizer
  149. * @version 2.0 11/15/01
  150. */
  151. class U_I18N_API Collator : public UObject {
  152. public:
  153. // Collator public enums -----------------------------------------------
  154. /**
  155. * Base letter represents a primary difference. Set comparison level to
  156. * PRIMARY to ignore secondary and tertiary differences.<br>
  157. * Use this to set the strength of a Collator object.<br>
  158. * Example of primary difference, "abc" &lt; "abd"
  159. *
  160. * Diacritical differences on the same base letter represent a secondary
  161. * difference. Set comparison level to SECONDARY to ignore tertiary
  162. * differences. Use this to set the strength of a Collator object.<br>
  163. * Example of secondary difference, "&auml;" >> "a".
  164. *
  165. * Uppercase and lowercase versions of the same character represents a
  166. * tertiary difference. Set comparison level to TERTIARY to include all
  167. * comparison differences. Use this to set the strength of a Collator
  168. * object.<br>
  169. * Example of tertiary difference, "abc" &lt;&lt;&lt; "ABC".
  170. *
  171. * Two characters are considered "identical" when they have the same unicode
  172. * spellings.<br>
  173. * For example, "&auml;" == "&auml;".
  174. *
  175. * UCollationStrength is also used to determine the strength of sort keys
  176. * generated from Collator objects.
  177. * @stable ICU 2.0
  178. */
  179. enum ECollationStrength
  180. {
  181. PRIMARY = UCOL_PRIMARY, // 0
  182. SECONDARY = UCOL_SECONDARY, // 1
  183. TERTIARY = UCOL_TERTIARY, // 2
  184. QUATERNARY = UCOL_QUATERNARY, // 3
  185. IDENTICAL = UCOL_IDENTICAL // 15
  186. };
  187. // Cannot use #ifndef U_HIDE_DEPRECATED_API for the following, it is
  188. // used by virtual methods that cannot have that conditional.
  189. /**
  190. * LESS is returned if source string is compared to be less than target
  191. * string in the compare() method.
  192. * EQUAL is returned if source string is compared to be equal to target
  193. * string in the compare() method.
  194. * GREATER is returned if source string is compared to be greater than
  195. * target string in the compare() method.
  196. * @see Collator#compare
  197. * @deprecated ICU 2.6. Use C enum UCollationResult defined in ucol.h
  198. */
  199. enum EComparisonResult
  200. {
  201. LESS = UCOL_LESS, // -1
  202. EQUAL = UCOL_EQUAL, // 0
  203. GREATER = UCOL_GREATER // 1
  204. };
  205. // Collator public destructor -----------------------------------------
  206. /**
  207. * Destructor
  208. * @stable ICU 2.0
  209. */
  210. virtual ~Collator();
  211. // Collator public methods --------------------------------------------
  212. /**
  213. * Returns TRUE if "other" is the same as "this".
  214. *
  215. * The base class implementation returns TRUE if "other" has the same type/class as "this":
  216. * <code>typeid(*this) == typeid(other)</code>.
  217. *
  218. * Subclass implementations should do something like the following:
  219. * <pre>
  220. * if (this == &other) { return TRUE; }
  221. * if (!Collator::operator==(other)) { return FALSE; } // not the same class
  222. *
  223. * const MyCollator &o = (const MyCollator&)other;
  224. * (compare this vs. o's subclass fields)
  225. * </pre>
  226. * @param other Collator object to be compared
  227. * @return TRUE if other is the same as this.
  228. * @stable ICU 2.0
  229. */
  230. virtual UBool operator==(const Collator& other) const;
  231. /**
  232. * Returns true if "other" is not the same as "this".
  233. * Calls ! operator==(const Collator&) const which works for all subclasses.
  234. * @param other Collator object to be compared
  235. * @return TRUE if other is not the same as this.
  236. * @stable ICU 2.0
  237. */
  238. virtual UBool operator!=(const Collator& other) const;
  239. /**
  240. * Makes a copy of this object.
  241. * @return a copy of this object, owned by the caller
  242. * @stable ICU 2.0
  243. */
  244. virtual Collator* clone(void) const = 0;
  245. /**
  246. * Creates the Collator object for the current default locale.
  247. * The default locale is determined by Locale::getDefault.
  248. * The UErrorCode& err parameter is used to return status information to the user.
  249. * To check whether the construction succeeded or not, you should check the
  250. * value of U_SUCCESS(err). If you wish more detailed information, you can
  251. * check for informational error results which still indicate success.
  252. * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
  253. * example, 'de_CH' was requested, but nothing was found there, so 'de' was
  254. * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
  255. * used; neither the requested locale nor any of its fall back locales
  256. * could be found.
  257. * The caller owns the returned object and is responsible for deleting it.
  258. *
  259. * @param err the error code status.
  260. * @return the collation object of the default locale.(for example, en_US)
  261. * @see Locale#getDefault
  262. * @stable ICU 2.0
  263. */
  264. static Collator* U_EXPORT2 createInstance(UErrorCode& err);
  265. /**
  266. * Gets the collation object for the desired locale. The
  267. * resource of the desired locale will be loaded.
  268. *
  269. * Locale::getRoot() is the base collation table and all other languages are
  270. * built on top of it with additional language-specific modifications.
  271. *
  272. * For some languages, multiple collation types are available;
  273. * for example, "de@collation=phonebook".
  274. * Starting with ICU 54, collation attributes can be specified via locale keywords as well,
  275. * in the old locale extension syntax ("el@colCaseFirst=upper")
  276. * or in language tag syntax ("el-u-kf-upper").
  277. * See <a href="http://userguide.icu-project.org/collation/api">User Guide: Collation API</a>.
  278. *
  279. * The UErrorCode& err parameter is used to return status information to the user.
  280. * To check whether the construction succeeded or not, you should check
  281. * the value of U_SUCCESS(err). If you wish more detailed information, you
  282. * can check for informational error results which still indicate success.
  283. * U_USING_FALLBACK_ERROR indicates that a fall back locale was used. For
  284. * example, 'de_CH' was requested, but nothing was found there, so 'de' was
  285. * used. U_USING_DEFAULT_ERROR indicates that the default locale data was
  286. * used; neither the requested locale nor any of its fall back locales
  287. * could be found.
  288. *
  289. * The caller owns the returned object and is responsible for deleting it.
  290. * @param loc The locale ID for which to open a collator.
  291. * @param err the error code status.
  292. * @return the created table-based collation object based on the desired
  293. * locale.
  294. * @see Locale
  295. * @see ResourceLoader
  296. * @stable ICU 2.2
  297. */
  298. static Collator* U_EXPORT2 createInstance(const Locale& loc, UErrorCode& err);
  299. /**
  300. * The comparison function compares the character data stored in two
  301. * different strings. Returns information about whether a string is less
  302. * than, greater than or equal to another string.
  303. * @param source the source string to be compared with.
  304. * @param target the string that is to be compared with the source string.
  305. * @return Returns a byte value. GREATER if source is greater
  306. * than target; EQUAL if source is equal to target; LESS if source is less
  307. * than target
  308. * @deprecated ICU 2.6 use the overload with UErrorCode &
  309. */
  310. virtual EComparisonResult compare(const UnicodeString& source,
  311. const UnicodeString& target) const;
  312. /**
  313. * The comparison function compares the character data stored in two
  314. * different strings. Returns information about whether a string is less
  315. * than, greater than or equal to another string.
  316. * @param source the source string to be compared with.
  317. * @param target the string that is to be compared with the source string.
  318. * @param status possible error code
  319. * @return Returns an enum value. UCOL_GREATER if source is greater
  320. * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
  321. * than target
  322. * @stable ICU 2.6
  323. */
  324. virtual UCollationResult compare(const UnicodeString& source,
  325. const UnicodeString& target,
  326. UErrorCode &status) const = 0;
  327. /**
  328. * Does the same thing as compare but limits the comparison to a specified
  329. * length
  330. * @param source the source string to be compared with.
  331. * @param target the string that is to be compared with the source string.
  332. * @param length the length the comparison is limited to
  333. * @return Returns a byte value. GREATER if source (up to the specified
  334. * length) is greater than target; EQUAL if source (up to specified
  335. * length) is equal to target; LESS if source (up to the specified
  336. * length) is less than target.
  337. * @deprecated ICU 2.6 use the overload with UErrorCode &
  338. */
  339. virtual EComparisonResult compare(const UnicodeString& source,
  340. const UnicodeString& target,
  341. int32_t length) const;
  342. /**
  343. * Does the same thing as compare but limits the comparison to a specified
  344. * length
  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 length the length the comparison is limited to
  348. * @param status possible error code
  349. * @return Returns an enum value. UCOL_GREATER if source (up to the specified
  350. * length) is greater than target; UCOL_EQUAL if source (up to specified
  351. * length) is equal to target; UCOL_LESS if source (up to the specified
  352. * length) is less than target.
  353. * @stable ICU 2.6
  354. */
  355. virtual UCollationResult compare(const UnicodeString& source,
  356. const UnicodeString& target,
  357. int32_t length,
  358. UErrorCode &status) const = 0;
  359. /**
  360. * The comparison function compares the character data stored in two
  361. * different string arrays. Returns information about whether a string array
  362. * is less than, greater than or equal to another string array.
  363. * <p>Example of use:
  364. * <pre>
  365. * . UChar ABC[] = {0x41, 0x42, 0x43, 0}; // = "ABC"
  366. * . UChar abc[] = {0x61, 0x62, 0x63, 0}; // = "abc"
  367. * . UErrorCode status = U_ZERO_ERROR;
  368. * . Collator *myCollation =
  369. * . Collator::createInstance(Locale::getUS(), status);
  370. * . if (U_FAILURE(status)) return;
  371. * . myCollation->setStrength(Collator::PRIMARY);
  372. * . // result would be Collator::EQUAL ("abc" == "ABC")
  373. * . // (no primary difference between "abc" and "ABC")
  374. * . Collator::EComparisonResult result =
  375. * . myCollation->compare(abc, 3, ABC, 3);
  376. * . myCollation->setStrength(Collator::TERTIARY);
  377. * . // result would be Collator::LESS ("abc" &lt;&lt;&lt; "ABC")
  378. * . // (with tertiary difference between "abc" and "ABC")
  379. * . result = myCollation->compare(abc, 3, ABC, 3);
  380. * </pre>
  381. * @param source the source string array to be compared with.
  382. * @param sourceLength the length of the source string array. If this value
  383. * is equal to -1, the string array is null-terminated.
  384. * @param target the string that is to be compared with the source string.
  385. * @param targetLength the length of the target string array. If this value
  386. * is equal to -1, the string array is null-terminated.
  387. * @return Returns a byte value. GREATER if source is greater than target;
  388. * EQUAL if source is equal to target; LESS if source is less than
  389. * target
  390. * @deprecated ICU 2.6 use the overload with UErrorCode &
  391. */
  392. virtual EComparisonResult compare(const UChar* source, int32_t sourceLength,
  393. const UChar* target, int32_t targetLength)
  394. const;
  395. /**
  396. * The comparison function compares the character data stored in two
  397. * different string arrays. Returns information about whether a string array
  398. * is less than, greater than or equal to another string array.
  399. * @param source the source string array to be compared with.
  400. * @param sourceLength the length of the source string array. If this value
  401. * is equal to -1, the string array is null-terminated.
  402. * @param target the string that is to be compared with the source string.
  403. * @param targetLength the length of the target string array. If this value
  404. * is equal to -1, the string array is null-terminated.
  405. * @param status possible error code
  406. * @return Returns an enum value. UCOL_GREATER if source is greater
  407. * than target; UCOL_EQUAL if source is equal to target; UCOL_LESS if source is less
  408. * than target
  409. * @stable ICU 2.6
  410. */
  411. virtual UCollationResult compare(const UChar* source, int32_t sourceLength,
  412. const UChar* target, int32_t targetLength,
  413. UErrorCode &status) const = 0;
  414. /**
  415. * Compares two strings using the Collator.
  416. * Returns whether the first one compares less than/equal to/greater than
  417. * the second one.
  418. * This version takes UCharIterator input.
  419. * @param sIter the first ("source") string iterator
  420. * @param tIter the second ("target") string iterator
  421. * @param status ICU status
  422. * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
  423. * @stable ICU 4.2
  424. */
  425. virtual UCollationResult compare(UCharIterator &sIter,
  426. UCharIterator &tIter,
  427. UErrorCode &status) const;
  428. /**
  429. * Compares two UTF-8 strings using the Collator.
  430. * Returns whether the first one compares less than/equal to/greater than
  431. * the second one.
  432. * This version takes UTF-8 input.
  433. * Note that a StringPiece can be implicitly constructed
  434. * from a std::string or a NUL-terminated const char * string.
  435. * @param source the first UTF-8 string
  436. * @param target the second UTF-8 string
  437. * @param status ICU status
  438. * @return UCOL_LESS, UCOL_EQUAL or UCOL_GREATER
  439. * @stable ICU 4.2
  440. */
  441. virtual UCollationResult compareUTF8(const StringPiece &source,
  442. const StringPiece &target,
  443. UErrorCode &status) const;
  444. /**
  445. * Transforms the string into a series of characters that can be compared
  446. * with CollationKey::compareTo. It is not possible to restore the original
  447. * string from the chars in the sort key.
  448. * <p>Use CollationKey::equals or CollationKey::compare to compare the
  449. * generated sort keys.
  450. * If the source string is null, a null collation key will be returned.
  451. *
  452. * Note that sort keys are often less efficient than simply doing comparison.
  453. * For more details, see the ICU User Guide.
  454. *
  455. * @param source the source string to be transformed into a sort key.
  456. * @param key the collation key to be filled in
  457. * @param status the error code status.
  458. * @return the collation key of the string based on the collation rules.
  459. * @see CollationKey#compare
  460. * @stable ICU 2.0
  461. */
  462. virtual CollationKey& getCollationKey(const UnicodeString& source,
  463. CollationKey& key,
  464. UErrorCode& status) const = 0;
  465. /**
  466. * Transforms the string into a series of characters that can be compared
  467. * with CollationKey::compareTo. It is not possible to restore the original
  468. * string from the chars in the sort key.
  469. * <p>Use CollationKey::equals or CollationKey::compare to compare the
  470. * generated sort keys.
  471. * <p>If the source string is null, a null collation key will be returned.
  472. *
  473. * Note that sort keys are often less efficient than simply doing comparison.
  474. * For more details, see the ICU User Guide.
  475. *
  476. * @param source the source string to be transformed into a sort key.
  477. * @param sourceLength length of the collation key
  478. * @param key the collation key to be filled in
  479. * @param status the error code status.
  480. * @return the collation key of the string based on the collation rules.
  481. * @see CollationKey#compare
  482. * @stable ICU 2.0
  483. */
  484. virtual CollationKey& getCollationKey(const UChar*source,
  485. int32_t sourceLength,
  486. CollationKey& key,
  487. UErrorCode& status) const = 0;
  488. /**
  489. * Generates the hash code for the collation object
  490. * @stable ICU 2.0
  491. */
  492. virtual int32_t hashCode(void) const = 0;
  493. /**
  494. * Gets the locale of the Collator
  495. *
  496. * @param type can be either requested, valid or actual locale. For more
  497. * information see the definition of ULocDataLocaleType in
  498. * uloc.h
  499. * @param status the error code status.
  500. * @return locale where the collation data lives. If the collator
  501. * was instantiated from rules, locale is empty.
  502. * @deprecated ICU 2.8 This API is under consideration for revision
  503. * in ICU 3.0.
  504. */
  505. virtual Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const = 0;
  506. /**
  507. * Convenience method for comparing two strings based on the collation rules.
  508. * @param source the source string to be compared with.
  509. * @param target the target string to be compared with.
  510. * @return true if the first string is greater than the second one,
  511. * according to the collation rules. false, otherwise.
  512. * @see Collator#compare
  513. * @stable ICU 2.0
  514. */
  515. UBool greater(const UnicodeString& source, const UnicodeString& target)
  516. const;
  517. /**
  518. * Convenience method for comparing two strings based on the collation rules.
  519. * @param source the source string to be compared with.
  520. * @param target the target string to be compared with.
  521. * @return true if the first string is greater than or equal to the second
  522. * one, according to the collation rules. false, otherwise.
  523. * @see Collator#compare
  524. * @stable ICU 2.0
  525. */
  526. UBool greaterOrEqual(const UnicodeString& source,
  527. const UnicodeString& target) const;
  528. /**
  529. * Convenience method for comparing two strings based on the collation rules.
  530. * @param source the source string to be compared with.
  531. * @param target the target string to be compared with.
  532. * @return true if the strings are equal according to the collation rules.
  533. * false, otherwise.
  534. * @see Collator#compare
  535. * @stable ICU 2.0
  536. */
  537. UBool equals(const UnicodeString& source, const UnicodeString& target) const;
  538. /**
  539. * Determines the minimum strength that will be used in comparison or
  540. * transformation.
  541. * <p>E.g. with strength == SECONDARY, the tertiary difference is ignored
  542. * <p>E.g. with strength == PRIMARY, the secondary and tertiary difference
  543. * are ignored.
  544. * @return the current comparison level.
  545. * @see Collator#setStrength
  546. * @deprecated ICU 2.6 Use getAttribute(UCOL_STRENGTH...) instead
  547. */
  548. virtual ECollationStrength getStrength(void) const;
  549. /**
  550. * Sets the minimum strength to be used in comparison or transformation.
  551. * <p>Example of use:
  552. * <pre>
  553. * \code
  554. * UErrorCode status = U_ZERO_ERROR;
  555. * Collator*myCollation = Collator::createInstance(Locale::getUS(), status);
  556. * if (U_FAILURE(status)) return;
  557. * myCollation->setStrength(Collator::PRIMARY);
  558. * // result will be "abc" == "ABC"
  559. * // tertiary differences will be ignored
  560. * Collator::ComparisonResult result = myCollation->compare("abc", "ABC");
  561. * \endcode
  562. * </pre>
  563. * @see Collator#getStrength
  564. * @param newStrength the new comparison level.
  565. * @deprecated ICU 2.6 Use setAttribute(UCOL_STRENGTH...) instead
  566. */
  567. virtual void setStrength(ECollationStrength newStrength);
  568. /**
  569. * Retrieves the reordering codes for this collator.
  570. * @param dest The array to fill with the script ordering.
  571. * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the function
  572. * will only return the length of the result without writing any codes (pre-flighting).
  573. * @param status A reference to an error code value, which must not indicate
  574. * a failure before the function call.
  575. * @return The length of the script ordering array.
  576. * @see ucol_setReorderCodes
  577. * @see Collator#getEquivalentReorderCodes
  578. * @see Collator#setReorderCodes
  579. * @see UScriptCode
  580. * @see UColReorderCode
  581. * @stable ICU 4.8
  582. */
  583. virtual int32_t getReorderCodes(int32_t *dest,
  584. int32_t destCapacity,
  585. UErrorCode& status) const;
  586. /**
  587. * Sets the ordering of scripts for this collator.
  588. *
  589. * <p>The reordering codes are a combination of script codes and reorder codes.
  590. * @param reorderCodes An array of script codes in the new order. This can be NULL if the
  591. * length is also set to 0. An empty array will clear any reordering codes on the collator.
  592. * @param reorderCodesLength The length of reorderCodes.
  593. * @param status error code
  594. * @see ucol_setReorderCodes
  595. * @see Collator#getReorderCodes
  596. * @see Collator#getEquivalentReorderCodes
  597. * @see UScriptCode
  598. * @see UColReorderCode
  599. * @stable ICU 4.8
  600. */
  601. virtual void setReorderCodes(const int32_t* reorderCodes,
  602. int32_t reorderCodesLength,
  603. UErrorCode& status) ;
  604. /**
  605. * Retrieves the reorder codes that are grouped with the given reorder code. Some reorder
  606. * codes will be grouped and must reorder together.
  607. * Beginning with ICU 55, scripts only reorder together if they are primary-equal,
  608. * for example Hiragana and Katakana.
  609. *
  610. * @param reorderCode The reorder code to determine equivalence for.
  611. * @param dest The array to fill with the script equivalence reordering codes.
  612. * @param destCapacity The length of dest. If it is 0, then dest may be NULL and the
  613. * function 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 of the reordering code equivalence array.
  617. * @see ucol_setReorderCodes
  618. * @see Collator#getReorderCodes
  619. * @see Collator#setReorderCodes
  620. * @see UScriptCode
  621. * @see UColReorderCode
  622. * @stable ICU 4.8
  623. */
  624. static int32_t U_EXPORT2 getEquivalentReorderCodes(int32_t reorderCode,
  625. int32_t* dest,
  626. int32_t destCapacity,
  627. UErrorCode& status);
  628. /**
  629. * Get name of the object for the desired Locale, in the desired langauge
  630. * @param objectLocale must be from getAvailableLocales
  631. * @param displayLocale specifies the desired locale for output
  632. * @param name the fill-in parameter of the return value
  633. * @return display-able name of the object for the object locale in the
  634. * desired language
  635. * @stable ICU 2.0
  636. */
  637. static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
  638. const Locale& displayLocale,
  639. UnicodeString& name);
  640. /**
  641. * Get name of the object for the desired Locale, in the langauge of the
  642. * default locale.
  643. * @param objectLocale must be from getAvailableLocales
  644. * @param name the fill-in parameter of the return value
  645. * @return name of the object for the desired locale in the default language
  646. * @stable ICU 2.0
  647. */
  648. static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
  649. UnicodeString& name);
  650. /**
  651. * Get the set of Locales for which Collations are installed.
  652. *
  653. * <p>Note this does not include locales supported by registered collators.
  654. * If collators might have been registered, use the overload of getAvailableLocales
  655. * that returns a StringEnumeration.</p>
  656. *
  657. * @param count the output parameter of number of elements in the locale list
  658. * @return the list of available locales for which collations are installed
  659. * @stable ICU 2.0
  660. */
  661. static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
  662. /**
  663. * Return a StringEnumeration over the locales available at the time of the call,
  664. * including registered locales. If a severe error occurs (such as out of memory
  665. * condition) this will return null. If there is no locale data, an empty enumeration
  666. * will be returned.
  667. * @return a StringEnumeration over the locales available at the time of the call
  668. * @stable ICU 2.6
  669. */
  670. static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
  671. /**
  672. * Create a string enumerator of all possible keywords that are relevant to
  673. * collation. At this point, the only recognized keyword for this
  674. * service is "collation".
  675. * @param status input-output error code
  676. * @return a string enumeration over locale strings. The caller is
  677. * responsible for closing the result.
  678. * @stable ICU 3.0
  679. */
  680. static StringEnumeration* U_EXPORT2 getKeywords(UErrorCode& status);
  681. /**
  682. * Given a keyword, create a string enumeration of all values
  683. * for that keyword that are currently in use.
  684. * @param keyword a particular keyword as enumerated by
  685. * ucol_getKeywords. If any other keyword is passed in, status is set
  686. * to U_ILLEGAL_ARGUMENT_ERROR.
  687. * @param status input-output error code
  688. * @return a string enumeration over collation keyword values, or NULL
  689. * upon error. The caller is responsible for deleting the result.
  690. * @stable ICU 3.0
  691. */
  692. static StringEnumeration* U_EXPORT2 getKeywordValues(const char *keyword, UErrorCode& status);
  693. /**
  694. * Given a key and a locale, returns an array of string values in a preferred
  695. * order that would make a difference. These are all and only those values where
  696. * the open (creation) of the service with the locale formed from the input locale
  697. * plus input keyword and that value has different behavior than creation with the
  698. * input locale alone.
  699. * @param keyword one of the keys supported by this service. For now, only
  700. * "collation" is supported.
  701. * @param locale the locale
  702. * @param commonlyUsed if set to true it will return only commonly used values
  703. * with the given locale in preferred order. Otherwise,
  704. * it will return all the available values for the locale.
  705. * @param status ICU status
  706. * @return a string enumeration over keyword values for the given key and the locale.
  707. * @stable ICU 4.2
  708. */
  709. static StringEnumeration* U_EXPORT2 getKeywordValuesForLocale(const char* keyword, const Locale& locale,
  710. UBool commonlyUsed, UErrorCode& status);
  711. /**
  712. * Return the functionally equivalent locale for the given
  713. * requested locale, with respect to given keyword, for the
  714. * collation service. If two locales return the same result, then
  715. * collators instantiated for these locales will behave
  716. * equivalently. The converse is not always true; two collators
  717. * may in fact be equivalent, but return different results, due to
  718. * internal details. The return result has no other meaning than
  719. * that stated above, and implies nothing as to the relationship
  720. * between the two locales. This is intended for use by
  721. * applications who wish to cache collators, or otherwise reuse
  722. * collators when possible. The functional equivalent may change
  723. * over time. For more information, please see the <a
  724. * href="http://userguide.icu-project.org/locale#TOC-Locales-and-Services">
  725. * Locales and Services</a> section of the ICU User Guide.
  726. * @param keyword a particular keyword as enumerated by
  727. * ucol_getKeywords.
  728. * @param locale the requested locale
  729. * @param isAvailable reference to a fillin parameter that
  730. * indicates whether the requested locale was 'available' to the
  731. * collation service. A locale is defined as 'available' if it
  732. * physically exists within the collation locale data.
  733. * @param status reference to input-output error code
  734. * @return the functionally equivalent collation locale, or the root
  735. * locale upon error.
  736. * @stable ICU 3.0
  737. */
  738. static Locale U_EXPORT2 getFunctionalEquivalent(const char* keyword, const Locale& locale,
  739. UBool& isAvailable, UErrorCode& status);
  740. #if !UCONFIG_NO_SERVICE
  741. /**
  742. * Register a new Collator. The collator will be adopted.
  743. * Because ICU may choose to cache collators internally, this must be
  744. * called at application startup, prior to any calls to
  745. * Collator::createInstance to avoid undefined behavior.
  746. * @param toAdopt the Collator instance to be adopted
  747. * @param locale the locale with which the collator will be associated
  748. * @param status the in/out status code, no special meanings are assigned
  749. * @return a registry key that can be used to unregister this collator
  750. * @stable ICU 2.6
  751. */
  752. static URegistryKey U_EXPORT2 registerInstance(Collator* toAdopt, const Locale& locale, UErrorCode& status);
  753. /**
  754. * Register a new CollatorFactory. The factory will be adopted.
  755. * Because ICU may choose to cache collators internally, this must be
  756. * called at application startup, prior to any calls to
  757. * Collator::createInstance to avoid undefined behavior.
  758. * @param toAdopt the CollatorFactory instance to be adopted
  759. * @param status the in/out status code, no special meanings are assigned
  760. * @return a registry key that can be used to unregister this collator
  761. * @stable ICU 2.6
  762. */
  763. static URegistryKey U_EXPORT2 registerFactory(CollatorFactory* toAdopt, UErrorCode& status);
  764. /**
  765. * Unregister a previously-registered Collator or CollatorFactory
  766. * using the key returned from the register call. Key becomes
  767. * invalid after a successful call and should not be used again.
  768. * The object corresponding to the key will be deleted.
  769. * Because ICU may choose to cache collators internally, this should
  770. * be called during application shutdown, after all calls to
  771. * Collator::createInstance to avoid undefined behavior.
  772. * @param key the registry key returned by a previous call to registerInstance
  773. * @param status the in/out status code, no special meanings are assigned
  774. * @return TRUE if the collator for the key was successfully unregistered
  775. * @stable ICU 2.6
  776. */
  777. static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
  778. #endif /* UCONFIG_NO_SERVICE */
  779. /**
  780. * Gets the version information for a Collator.
  781. * @param info the version # information, the result will be filled in
  782. * @stable ICU 2.0
  783. */
  784. virtual void getVersion(UVersionInfo info) const = 0;
  785. /**
  786. * Returns a unique class ID POLYMORPHICALLY. Pure virtual method.
  787. * This method is to implement a simple version of RTTI, since not all C++
  788. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  789. * methods call this method.
  790. * @return The class ID for this object. All objects of a given class have
  791. * the same class ID. Objects of other classes have different class
  792. * IDs.
  793. * @stable ICU 2.0
  794. */
  795. virtual UClassID getDynamicClassID(void) const = 0;
  796. /**
  797. * Universal attribute setter
  798. * @param attr attribute type
  799. * @param value attribute value
  800. * @param status to indicate whether the operation went on smoothly or
  801. * there were errors
  802. * @stable ICU 2.2
  803. */
  804. virtual void setAttribute(UColAttribute attr, UColAttributeValue value,
  805. UErrorCode &status) = 0;
  806. /**
  807. * Universal attribute getter
  808. * @param attr attribute type
  809. * @param status to indicate whether the operation went on smoothly or
  810. * there were errors
  811. * @return attribute value
  812. * @stable ICU 2.2
  813. */
  814. virtual UColAttributeValue getAttribute(UColAttribute attr,
  815. UErrorCode &status) const = 0;
  816. /**
  817. * Sets the variable top to the top of the specified reordering group.
  818. * The variable top determines the highest-sorting character
  819. * which is affected by UCOL_ALTERNATE_HANDLING.
  820. * If that attribute is set to UCOL_NON_IGNORABLE, then the variable top has no effect.
  821. *
  822. * The base class implementation sets U_UNSUPPORTED_ERROR.
  823. * @param group one of UCOL_REORDER_CODE_SPACE, UCOL_REORDER_CODE_PUNCTUATION,
  824. * UCOL_REORDER_CODE_SYMBOL, UCOL_REORDER_CODE_CURRENCY;
  825. * or UCOL_REORDER_CODE_DEFAULT to restore the default max variable group
  826. * @param errorCode Standard ICU error code. Its input value must
  827. * pass the U_SUCCESS() test, or else the function returns
  828. * immediately. Check for U_FAILURE() on output or use with
  829. * function chaining. (See User Guide for details.)
  830. * @return *this
  831. * @see getMaxVariable
  832. * @stable ICU 53
  833. */
  834. virtual Collator &setMaxVariable(UColReorderCode group, UErrorCode &errorCode);
  835. /**
  836. * Returns the maximum reordering group whose characters are affected by UCOL_ALTERNATE_HANDLING.
  837. *
  838. * The base class implementation returns UCOL_REORDER_CODE_PUNCTUATION.
  839. * @return the maximum variable reordering group.
  840. * @see setMaxVariable
  841. * @stable ICU 53
  842. */
  843. virtual UColReorderCode getMaxVariable() const;
  844. /**
  845. * Sets the variable top to the primary weight of the specified string.
  846. *
  847. * Beginning with ICU 53, the variable top is pinned to
  848. * the top of one of the supported reordering groups,
  849. * and it must not be beyond the last of those groups.
  850. * See setMaxVariable().
  851. * @param varTop one or more (if contraction) UChars to which the variable top should be set
  852. * @param len length of variable top string. If -1 it is considered to be zero terminated.
  853. * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
  854. * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
  855. * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
  856. * the last reordering group supported by setMaxVariable()
  857. * @return variable top primary weight
  858. * @deprecated ICU 53 Call setMaxVariable() instead.
  859. */
  860. virtual uint32_t setVariableTop(const UChar *varTop, int32_t len, UErrorCode &status) = 0;
  861. /**
  862. * Sets the variable top to the primary weight of the specified string.
  863. *
  864. * Beginning with ICU 53, the variable top is pinned to
  865. * the top of one of the supported reordering groups,
  866. * and it must not be beyond the last of those groups.
  867. * See setMaxVariable().
  868. * @param varTop a UnicodeString size 1 or more (if contraction) of UChars to which the variable top should be set
  869. * @param status error code. If error code is set, the return value is undefined. Errors set by this function are: <br>
  870. * U_CE_NOT_FOUND_ERROR if more than one character was passed and there is no such contraction<br>
  871. * U_ILLEGAL_ARGUMENT_ERROR if the variable top is beyond
  872. * the last reordering group supported by setMaxVariable()
  873. * @return variable top primary weight
  874. * @deprecated ICU 53 Call setMaxVariable() instead.
  875. */
  876. virtual uint32_t setVariableTop(const UnicodeString &varTop, UErrorCode &status) = 0;
  877. /**
  878. * Sets the variable top to the specified primary weight.
  879. *
  880. * Beginning with ICU 53, the variable top is pinned to
  881. * the top of one of the supported reordering groups,
  882. * and it must not be beyond the last of those groups.
  883. * See setMaxVariable().
  884. * @param varTop primary weight, as returned by setVariableTop or ucol_getVariableTop
  885. * @param status error code
  886. * @deprecated ICU 53 Call setMaxVariable() instead.
  887. */
  888. virtual void setVariableTop(uint32_t varTop, UErrorCode &status) = 0;
  889. /**
  890. * Gets the variable top value of a Collator.
  891. * @param status error code (not changed by function). If error code is set, the return value is undefined.
  892. * @return the variable top primary weight
  893. * @see getMaxVariable
  894. * @stable ICU 2.0
  895. */
  896. virtual uint32_t getVariableTop(UErrorCode &status) const = 0;
  897. /**
  898. * Get a UnicodeSet that contains all the characters and sequences
  899. * tailored in this collator.
  900. * @param status error code of the operation
  901. * @return a pointer to a UnicodeSet object containing all the
  902. * code points and sequences that may sort differently than
  903. * in the root collator. The object must be disposed of by using delete
  904. * @stable ICU 2.4
  905. */
  906. virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
  907. /**
  908. * Same as clone().
  909. * The base class implementation simply calls clone().
  910. * @return a copy of this object, owned by the caller
  911. * @see clone()
  912. * @deprecated ICU 50 no need to have two methods for cloning
  913. */
  914. virtual Collator* safeClone(void) const;
  915. /**
  916. * Get the sort key as an array of bytes from a UnicodeString.
  917. * Sort key byte arrays are zero-terminated and can be compared using
  918. * strcmp().
  919. *
  920. * Note that sort keys are often less efficient than simply doing comparison.
  921. * For more details, see the ICU User Guide.
  922. *
  923. * @param source string to be processed.
  924. * @param result buffer to store result in. If NULL, number of bytes needed
  925. * will be returned.
  926. * @param resultLength length of the result buffer. If if not enough the
  927. * buffer will be filled to capacity.
  928. * @return Number of bytes needed for storing the sort key
  929. * @stable ICU 2.2
  930. */
  931. virtual int32_t getSortKey(const UnicodeString& source,
  932. uint8_t* result,
  933. int32_t resultLength) const = 0;
  934. /**
  935. * Get the sort key as an array of bytes from a UChar buffer.
  936. * Sort key byte arrays are zero-terminated and can be compared using
  937. * strcmp().
  938. *
  939. * Note that sort keys are often less efficient than simply doing comparison.
  940. * For more details, see the ICU User Guide.
  941. *
  942. * @param source string to be processed.
  943. * @param sourceLength length of string to be processed.
  944. * If -1, the string is 0 terminated and length will be decided by the
  945. * function.
  946. * @param result buffer to store result in. If NULL, number of bytes needed
  947. * will be returned.
  948. * @param resultLength length of the result buffer. If if not enough the
  949. * buffer will be filled to capacity.
  950. * @return Number of bytes needed for storing the sort key
  951. * @stable ICU 2.2
  952. */
  953. virtual int32_t getSortKey(const UChar*source, int32_t sourceLength,
  954. uint8_t*result, int32_t resultLength) const = 0;
  955. /**
  956. * Produce a bound for a given sortkey and a number of levels.
  957. * Return value is always the number of bytes needed, regardless of
  958. * whether the result buffer was big enough or even valid.<br>
  959. * Resulting bounds can be used to produce a range of strings that are
  960. * between upper and lower bounds. For example, if bounds are produced
  961. * for a sortkey of string "smith", strings between upper and lower
  962. * bounds with one level would include "Smith", "SMITH", "sMiTh".<br>
  963. * There are two upper bounds that can be produced. If UCOL_BOUND_UPPER
  964. * is produced, strings matched would be as above. However, if bound
  965. * produced using UCOL_BOUND_UPPER_LONG is used, the above example will
  966. * also match "Smithsonian" and similar.<br>
  967. * For more on usage, see example in cintltst/capitst.c in procedure
  968. * TestBounds.
  969. * Sort keys may be compared using <TT>strcmp</TT>.
  970. * @param source The source sortkey.
  971. * @param sourceLength The length of source, or -1 if null-terminated.
  972. * (If an unmodified sortkey is passed, it is always null
  973. * terminated).
  974. * @param boundType Type of bound required. It can be UCOL_BOUND_LOWER, which
  975. * produces a lower inclusive bound, UCOL_BOUND_UPPER, that
  976. * produces upper bound that matches strings of the same length
  977. * or UCOL_BOUND_UPPER_LONG that matches strings that have the
  978. * same starting substring as the source string.
  979. * @param noOfLevels Number of levels required in the resulting bound (for most
  980. * uses, the recommended value is 1). See users guide for
  981. * explanation on number of levels a sortkey can have.
  982. * @param result A pointer to a buffer to receive the resulting sortkey.
  983. * @param resultLength The maximum size of result.
  984. * @param status Used for returning error code if something went wrong. If the
  985. * number of levels requested is higher than the number of levels
  986. * in the source key, a warning (U_SORT_KEY_TOO_SHORT_WARNING) is
  987. * issued.
  988. * @return The size needed to fully store the bound.
  989. * @see ucol_keyHashCode
  990. * @stable ICU 2.1
  991. */
  992. static int32_t U_EXPORT2 getBound(const uint8_t *source,
  993. int32_t sourceLength,
  994. UColBoundMode boundType,
  995. uint32_t noOfLevels,
  996. uint8_t *result,
  997. int32_t resultLength,
  998. UErrorCode &status);
  999. protected:
  1000. // Collator protected constructors -------------------------------------
  1001. /**
  1002. * Default constructor.
  1003. * Constructor is different from the old default Collator constructor.
  1004. * The task for determing the default collation strength and normalization
  1005. * mode is left to the child class.
  1006. * @stable ICU 2.0
  1007. */
  1008. Collator();
  1009. #ifndef U_HIDE_DEPRECATED_API
  1010. /**
  1011. * Constructor.
  1012. * Empty constructor, does not handle the arguments.
  1013. * This constructor is done for backward compatibility with 1.7 and 1.8.
  1014. * The task for handling the argument collation strength and normalization
  1015. * mode is left to the child class.
  1016. * @param collationStrength collation strength
  1017. * @param decompositionMode
  1018. * @deprecated ICU 2.4. Subclasses should use the default constructor
  1019. * instead and handle the strength and normalization mode themselves.
  1020. */
  1021. Collator(UCollationStrength collationStrength,
  1022. UNormalizationMode decompositionMode);
  1023. #endif /* U_HIDE_DEPRECATED_API */
  1024. /**
  1025. * Copy constructor.
  1026. * @param other Collator object to be copied from
  1027. * @stable ICU 2.0
  1028. */
  1029. Collator(const Collator& other);
  1030. public:
  1031. /**
  1032. * Used internally by registration to define the requested and valid locales.
  1033. * @param requestedLocale the requested locale
  1034. * @param validLocale the valid locale
  1035. * @param actualLocale the actual locale
  1036. * @internal
  1037. */
  1038. virtual void setLocales(const Locale& requestedLocale, const Locale& validLocale, const Locale& actualLocale);
  1039. /** Get the short definition string for a collator. This internal API harvests the collator's
  1040. * locale and the attribute set and produces a string that can be used for opening
  1041. * a collator with the same attributes using the ucol_openFromShortString API.
  1042. * This string will be normalized.
  1043. * The structure and the syntax of the string is defined in the "Naming collators"
  1044. * section of the users guide:
  1045. * http://userguide.icu-project.org/collation/concepts#TOC-Collator-naming-scheme
  1046. * This function supports preflighting.
  1047. *
  1048. * This is internal, and intended to be used with delegate converters.
  1049. *
  1050. * @param locale a locale that will appear as a collators locale in the resulting
  1051. * short string definition. If NULL, the locale will be harvested
  1052. * from the collator.
  1053. * @param buffer space to hold the resulting string
  1054. * @param capacity capacity of the buffer
  1055. * @param status for returning errors. All the preflighting errors are featured
  1056. * @return length of the resulting string
  1057. * @see ucol_openFromShortString
  1058. * @see ucol_normalizeShortDefinitionString
  1059. * @see ucol_getShortDefinitionString
  1060. * @internal
  1061. */
  1062. virtual int32_t internalGetShortDefinitionString(const char *locale,
  1063. char *buffer,
  1064. int32_t capacity,
  1065. UErrorCode &status) const;
  1066. /**
  1067. * Implements ucol_strcollUTF8().
  1068. * @internal
  1069. */
  1070. virtual UCollationResult internalCompareUTF8(
  1071. const char *left, int32_t leftLength,
  1072. const char *right, int32_t rightLength,
  1073. UErrorCode &errorCode) const;
  1074. /**
  1075. * Implements ucol_nextSortKeyPart().
  1076. * @internal
  1077. */
  1078. virtual int32_t
  1079. internalNextSortKeyPart(
  1080. UCharIterator *iter, uint32_t state[2],
  1081. uint8_t *dest, int32_t count, UErrorCode &errorCode) const;
  1082. #ifndef U_HIDE_INTERNAL_API
  1083. /** @internal */
  1084. static inline Collator *fromUCollator(UCollator *uc) {
  1085. return reinterpret_cast<Collator *>(uc);
  1086. }
  1087. /** @internal */
  1088. static inline const Collator *fromUCollator(const UCollator *uc) {
  1089. return reinterpret_cast<const Collator *>(uc);
  1090. }
  1091. /** @internal */
  1092. inline UCollator *toUCollator() {
  1093. return reinterpret_cast<UCollator *>(this);
  1094. }
  1095. /** @internal */
  1096. inline const UCollator *toUCollator() const {
  1097. return reinterpret_cast<const UCollator *>(this);
  1098. }
  1099. #endif // U_HIDE_INTERNAL_API
  1100. private:
  1101. /**
  1102. * Assignment operator. Private for now.
  1103. */
  1104. Collator& operator=(const Collator& other);
  1105. friend class CFactory;
  1106. friend class SimpleCFactory;
  1107. friend class ICUCollatorFactory;
  1108. friend class ICUCollatorService;
  1109. static Collator* makeInstance(const Locale& desiredLocale,
  1110. UErrorCode& status);
  1111. };
  1112. #if !UCONFIG_NO_SERVICE
  1113. /**
  1114. * A factory, used with registerFactory, the creates multiple collators and provides
  1115. * display names for them. A factory supports some number of locales-- these are the
  1116. * locales for which it can create collators. The factory can be visible, in which
  1117. * case the supported locales will be enumerated by getAvailableLocales, or invisible,
  1118. * in which they are not. Invisible locales are still supported, they are just not
  1119. * listed by getAvailableLocales.
  1120. * <p>
  1121. * If standard locale display names are sufficient, Collator instances can
  1122. * be registered using registerInstance instead.</p>
  1123. * <p>
  1124. * Note: if the collators are to be used from C APIs, they must be instances
  1125. * of RuleBasedCollator.</p>
  1126. *
  1127. * @stable ICU 2.6
  1128. */
  1129. class U_I18N_API CollatorFactory : public UObject {
  1130. public:
  1131. /**
  1132. * Destructor
  1133. * @stable ICU 3.0
  1134. */
  1135. virtual ~CollatorFactory();
  1136. /**
  1137. * Return true if this factory is visible. Default is true.
  1138. * If not visible, the locales supported by this factory will not
  1139. * be listed by getAvailableLocales.
  1140. * @return true if the factory is visible.
  1141. * @stable ICU 2.6
  1142. */
  1143. virtual UBool visible(void) const;
  1144. /**
  1145. * Return a collator for the provided locale. If the locale
  1146. * is not supported, return NULL.
  1147. * @param loc the locale identifying the collator to be created.
  1148. * @return a new collator if the locale is supported, otherwise NULL.
  1149. * @stable ICU 2.6
  1150. */
  1151. virtual Collator* createCollator(const Locale& loc) = 0;
  1152. /**
  1153. * Return the name of the collator for the objectLocale, localized for the displayLocale.
  1154. * If objectLocale is not supported, or the factory is not visible, set the result string
  1155. * to bogus.
  1156. * @param objectLocale the locale identifying the collator
  1157. * @param displayLocale the locale for which the display name of the collator should be localized
  1158. * @param result an output parameter for the display name, set to bogus if not supported.
  1159. * @return the display name
  1160. * @stable ICU 2.6
  1161. */
  1162. virtual UnicodeString& getDisplayName(const Locale& objectLocale,
  1163. const Locale& displayLocale,
  1164. UnicodeString& result);
  1165. /**
  1166. * Return an array of all the locale names directly supported by this factory.
  1167. * The number of names is returned in count. This array is owned by the factory.
  1168. * Its contents must never change.
  1169. * @param count output parameter for the number of locales supported by the factory
  1170. * @param status the in/out error code
  1171. * @return a pointer to an array of count UnicodeStrings.
  1172. * @stable ICU 2.6
  1173. */
  1174. virtual const UnicodeString * getSupportedIDs(int32_t &count, UErrorCode& status) = 0;
  1175. };
  1176. #endif /* UCONFIG_NO_SERVICE */
  1177. // Collator inline methods -----------------------------------------------
  1178. U_NAMESPACE_END
  1179. #endif /* #if !UCONFIG_NO_COLLATION */
  1180. #endif