coll.h 56 KB

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