coll.h 55 KB

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