ucoleitr.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2001-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. *******************************************************************************
  8. *
  9. * File ucoleitr.h
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/15/2001 synwee Modified all methods to process its own function
  15. * instead of calling the equivalent c++ api (coleitr.h)
  16. *******************************************************************************/
  17. #ifndef UCOLEITR_H
  18. #define UCOLEITR_H
  19. #include "unicode/utypes.h"
  20. #if !UCONFIG_NO_COLLATION
  21. /**
  22. * This indicates an error has occurred during processing or if no more CEs is
  23. * to be returned.
  24. * @stable ICU 2.0
  25. */
  26. #define UCOL_NULLORDER ((int32_t)0xFFFFFFFF)
  27. #include "unicode/ucol.h"
  28. /**
  29. * The UCollationElements struct.
  30. * For usage in C programs.
  31. * @stable ICU 2.0
  32. */
  33. typedef struct UCollationElements UCollationElements;
  34. /**
  35. * \file
  36. * \brief C API: UCollationElements
  37. *
  38. * The UCollationElements API is used as an iterator to walk through each
  39. * character of an international string. Use the iterator to return the
  40. * ordering priority of the positioned character. The ordering priority of a
  41. * character, which we refer to as a key, defines how a character is collated
  42. * in the given collation object.
  43. * For example, consider the following in Slovak and in traditional Spanish collation:
  44. * <pre>
  45. * . "ca" -> the first key is key('c') and second key is key('a').
  46. * . "cha" -> the first key is key('ch') and second key is key('a').
  47. * </pre>
  48. * And in German phonebook collation,
  49. * <pre>
  50. * . "<ae ligature>b"-> the first key is key('a'), the second key is key('e'), and
  51. * . the third key is key('b').
  52. * </pre>
  53. * <p>Example of the iterator usage: (without error checking)
  54. * <pre>
  55. * . void CollationElementIterator_Example()
  56. * . {
  57. * . UChar *s;
  58. * . t_int32 order, primaryOrder;
  59. * . UCollationElements *c;
  60. * . UCollatorOld *coll;
  61. * . UErrorCode success = U_ZERO_ERROR;
  62. * . str=(UChar*)malloc(sizeof(UChar) * (strlen("This is a test")+1) );
  63. * . u_uastrcpy(str, "This is a test");
  64. * . coll = ucol_open(NULL, &success);
  65. * . c = ucol_openElements(coll, str, u_strlen(str), &status);
  66. * . order = ucol_next(c, &success);
  67. * . ucol_reset(c);
  68. * . order = ucol_prev(c, &success);
  69. * . free(str);
  70. * . ucol_close(coll);
  71. * . ucol_closeElements(c);
  72. * . }
  73. * </pre>
  74. * <p>
  75. * ucol_next() returns the collation order of the next.
  76. * ucol_prev() returns the collation order of the previous character.
  77. * The Collation Element Iterator moves only in one direction between calls to
  78. * ucol_reset. That is, ucol_next() and ucol_prev can not be inter-used.
  79. * Whenever ucol_prev is to be called after ucol_next() or vice versa,
  80. * ucol_reset has to be called first to reset the status, shifting pointers to
  81. * either the end or the start of the string. Hence at the next call of
  82. * ucol_prev or ucol_next, the first or last collation order will be returned.
  83. * If a change of direction is done without a ucol_reset, the result is
  84. * undefined.
  85. * The result of a forward iterate (ucol_next) and reversed result of the
  86. * backward iterate (ucol_prev) on the same string are equivalent, if
  87. * collation orders with the value 0 are ignored.
  88. * Character based on the comparison level of the collator. A collation order
  89. * consists of primary order, secondary order and tertiary order. The data
  90. * type of the collation order is <strong>int32_t</strong>.
  91. *
  92. * @see UCollator
  93. */
  94. /**
  95. * Open the collation elements for a string.
  96. *
  97. * The UCollationElements retains a pointer to the supplied text.
  98. * The caller must not modify or delete the text while the UCollationElements
  99. * object is used to iterate over this text.
  100. *
  101. * @param coll The collator containing the desired collation rules.
  102. * @param text The text to iterate over.
  103. * @param textLength The number of characters in text, or -1 if null-terminated
  104. * @param status A pointer to a UErrorCode to receive any errors.
  105. * @return a struct containing collation element information
  106. * @stable ICU 2.0
  107. */
  108. U_CAPI UCollationElements* U_EXPORT2
  109. ucol_openElements(const UCollator *coll,
  110. const UChar *text,
  111. int32_t textLength,
  112. UErrorCode *status);
  113. /**
  114. * get a hash code for a key... Not very useful!
  115. * @param key the given key.
  116. * @param length the size of the key array.
  117. * @return the hash code.
  118. * @stable ICU 2.0
  119. */
  120. U_CAPI int32_t U_EXPORT2
  121. ucol_keyHashCode(const uint8_t* key, int32_t length);
  122. /**
  123. * Close a UCollationElements.
  124. * Once closed, a UCollationElements may no longer be used.
  125. * @param elems The UCollationElements to close.
  126. * @stable ICU 2.0
  127. */
  128. U_CAPI void U_EXPORT2
  129. ucol_closeElements(UCollationElements *elems);
  130. /**
  131. * Reset the collation elements to their initial state.
  132. * This will move the 'cursor' to the beginning of the text.
  133. * Property settings for collation will be reset to the current status.
  134. * @param elems The UCollationElements to reset.
  135. * @see ucol_next
  136. * @see ucol_previous
  137. * @stable ICU 2.0
  138. */
  139. U_CAPI void U_EXPORT2
  140. ucol_reset(UCollationElements *elems);
  141. /**
  142. * Get the ordering priority of the next collation element in the text.
  143. * A single character may contain more than one collation element.
  144. * @param elems The UCollationElements containing the text.
  145. * @param status A pointer to a UErrorCode to receive any errors.
  146. * @return The next collation elements ordering, otherwise returns UCOL_NULLORDER
  147. * if an error has occurred or if the end of string has been reached
  148. * @stable ICU 2.0
  149. */
  150. U_CAPI int32_t U_EXPORT2
  151. ucol_next(UCollationElements *elems, UErrorCode *status);
  152. /**
  153. * Get the ordering priority of the previous collation element in the text.
  154. * A single character may contain more than one collation element.
  155. * Note that internally a stack is used to store buffered collation elements.
  156. * @param elems The UCollationElements containing the text.
  157. * @param status A pointer to a UErrorCode to receive any errors. Notably
  158. * a U_BUFFER_OVERFLOW_ERROR is returned if the internal stack
  159. * buffer has been exhausted.
  160. * @return The previous collation elements ordering, otherwise returns
  161. * UCOL_NULLORDER if an error has occurred or if the start of string has
  162. * been reached.
  163. * @stable ICU 2.0
  164. */
  165. U_CAPI int32_t U_EXPORT2
  166. ucol_previous(UCollationElements *elems, UErrorCode *status);
  167. /**
  168. * Get the maximum length of any expansion sequences that end with the
  169. * specified comparison order.
  170. * This is useful for .... ?
  171. * @param elems The UCollationElements containing the text.
  172. * @param order A collation order returned by previous or next.
  173. * @return maximum size of the expansion sequences ending with the collation
  174. * element or 1 if collation element does not occur at the end of any
  175. * expansion sequence
  176. * @stable ICU 2.0
  177. */
  178. U_CAPI int32_t U_EXPORT2
  179. ucol_getMaxExpansion(const UCollationElements *elems, int32_t order);
  180. /**
  181. * Set the text containing the collation elements.
  182. * Property settings for collation will remain the same.
  183. * In order to reset the iterator to the current collation property settings,
  184. * the API reset() has to be called.
  185. *
  186. * The UCollationElements retains a pointer to the supplied text.
  187. * The caller must not modify or delete the text while the UCollationElements
  188. * object is used to iterate over this text.
  189. *
  190. * @param elems The UCollationElements to set.
  191. * @param text The source text containing the collation elements.
  192. * @param textLength The length of text, or -1 if null-terminated.
  193. * @param status A pointer to a UErrorCode to receive any errors.
  194. * @see ucol_getText
  195. * @stable ICU 2.0
  196. */
  197. U_CAPI void U_EXPORT2
  198. ucol_setText( UCollationElements *elems,
  199. const UChar *text,
  200. int32_t textLength,
  201. UErrorCode *status);
  202. /**
  203. * Get the offset of the current source character.
  204. * This is an offset into the text of the character containing the current
  205. * collation elements.
  206. * @param elems The UCollationElements to query.
  207. * @return The offset of the current source character.
  208. * @see ucol_setOffset
  209. * @stable ICU 2.0
  210. */
  211. U_CAPI int32_t U_EXPORT2
  212. ucol_getOffset(const UCollationElements *elems);
  213. /**
  214. * Set the offset of the current source character.
  215. * This is an offset into the text of the character to be processed.
  216. * Property settings for collation will remain the same.
  217. * In order to reset the iterator to the current collation property settings,
  218. * the API reset() has to be called.
  219. * @param elems The UCollationElements to set.
  220. * @param offset The desired character offset.
  221. * @param status A pointer to a UErrorCode to receive any errors.
  222. * @see ucol_getOffset
  223. * @stable ICU 2.0
  224. */
  225. U_CAPI void U_EXPORT2
  226. ucol_setOffset(UCollationElements *elems,
  227. int32_t offset,
  228. UErrorCode *status);
  229. /**
  230. * Get the primary order of a collation order.
  231. * @param order the collation order
  232. * @return the primary order of a collation order.
  233. * @stable ICU 2.6
  234. */
  235. U_CAPI int32_t U_EXPORT2
  236. ucol_primaryOrder (int32_t order);
  237. /**
  238. * Get the secondary order of a collation order.
  239. * @param order the collation order
  240. * @return the secondary order of a collation order.
  241. * @stable ICU 2.6
  242. */
  243. U_CAPI int32_t U_EXPORT2
  244. ucol_secondaryOrder (int32_t order);
  245. /**
  246. * Get the tertiary order of a collation order.
  247. * @param order the collation order
  248. * @return the tertiary order of a collation order.
  249. * @stable ICU 2.6
  250. */
  251. U_CAPI int32_t U_EXPORT2
  252. ucol_tertiaryOrder (int32_t order);
  253. #endif /* #if !UCONFIG_NO_COLLATION */
  254. #endif