coll.h 60 KB

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