ucoleitr.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright (C) 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 occured 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. * . s=(UChar*)malloc(sizeof(UChar) * (strlen("This is a test")+1) );
  63. * . u_uastrcpy(s, "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(s);
  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. * @param coll The collator containing the desired collation rules.
  98. * @param text The text to iterate over.
  99. * @param textLength The number of characters in text, or -1 if null-terminated
  100. * @param status A pointer to a UErrorCode to receive any errors.
  101. * @return a struct containing collation element information
  102. * @stable ICU 2.0
  103. */
  104. U_STABLE UCollationElements* U_EXPORT2
  105. ucol_openElements(const UCollator *coll,
  106. const UChar *text,
  107. int32_t textLength,
  108. UErrorCode *status);
  109. /**
  110. * get a hash code for a key... Not very useful!
  111. * @param key the given key.
  112. * @param length the size of the key array.
  113. * @return the hash code.
  114. * @stable ICU 2.0
  115. */
  116. U_STABLE int32_t U_EXPORT2
  117. ucol_keyHashCode(const uint8_t* key, int32_t length);
  118. /**
  119. * Close a UCollationElements.
  120. * Once closed, a UCollationElements may no longer be used.
  121. * @param elems The UCollationElements to close.
  122. * @stable ICU 2.0
  123. */
  124. U_STABLE void U_EXPORT2
  125. ucol_closeElements(UCollationElements *elems);
  126. /**
  127. * Reset the collation elements to their initial state.
  128. * This will move the 'cursor' to the beginning of the text.
  129. * Property settings for collation will be reset to the current status.
  130. * @param elems The UCollationElements to reset.
  131. * @see ucol_next
  132. * @see ucol_previous
  133. * @stable ICU 2.0
  134. */
  135. U_STABLE void U_EXPORT2
  136. ucol_reset(UCollationElements *elems);
  137. /**
  138. * Get the ordering priority of the next collation element in the text.
  139. * A single character may contain more than one collation element.
  140. * @param elems The UCollationElements containing the text.
  141. * @param status A pointer to a UErrorCode to receive any errors.
  142. * @return The next collation elements ordering, otherwise returns NULLORDER
  143. * if an error has occured or if the end of string has been reached
  144. * @stable ICU 2.0
  145. */
  146. U_STABLE int32_t U_EXPORT2
  147. ucol_next(UCollationElements *elems, UErrorCode *status);
  148. /**
  149. * Get the ordering priority of the previous collation element in the text.
  150. * A single character may contain more than one collation element.
  151. * Note that internally a stack is used to store buffered collation elements.
  152. * @param elems The UCollationElements containing the text.
  153. * @param status A pointer to a UErrorCode to receive any errors. Noteably
  154. * a U_BUFFER_OVERFLOW_ERROR is returned if the internal stack
  155. * buffer has been exhausted.
  156. * @return The previous collation elements ordering, otherwise returns
  157. * NULLORDER if an error has occured or if the start of string has
  158. * been reached.
  159. * @stable ICU 2.0
  160. */
  161. U_STABLE int32_t U_EXPORT2
  162. ucol_previous(UCollationElements *elems, UErrorCode *status);
  163. /**
  164. * Get the maximum length of any expansion sequences that end with the
  165. * specified comparison order.
  166. * This is useful for .... ?
  167. * @param elems The UCollationElements containing the text.
  168. * @param order A collation order returned by previous or next.
  169. * @return maximum size of the expansion sequences ending with the collation
  170. * element or 1 if collation element does not occur at the end of any
  171. * expansion sequence
  172. * @stable ICU 2.0
  173. */
  174. U_STABLE int32_t U_EXPORT2
  175. ucol_getMaxExpansion(const UCollationElements *elems, int32_t order);
  176. /**
  177. * Set the text containing the collation elements.
  178. * Property settings for collation will remain the same.
  179. * In order to reset the iterator to the current collation property settings,
  180. * the API reset() has to be called.
  181. * @param elems The UCollationElements to set.
  182. * @param text The source text containing the collation elements.
  183. * @param textLength The length of text, or -1 if null-terminated.
  184. * @param status A pointer to a UErrorCode to receive any errors.
  185. * @see ucol_getText
  186. * @stable ICU 2.0
  187. */
  188. U_STABLE void U_EXPORT2
  189. ucol_setText( UCollationElements *elems,
  190. const UChar *text,
  191. int32_t textLength,
  192. UErrorCode *status);
  193. /**
  194. * Get the offset of the current source character.
  195. * This is an offset into the text of the character containing the current
  196. * collation elements.
  197. * @param elems The UCollationElements to query.
  198. * @return The offset of the current source character.
  199. * @see ucol_setOffset
  200. * @stable ICU 2.0
  201. */
  202. U_STABLE int32_t U_EXPORT2
  203. ucol_getOffset(const UCollationElements *elems);
  204. /**
  205. * Set the offset of the current source character.
  206. * This is an offset into the text of the character to be processed.
  207. * Property settings for collation will remain the same.
  208. * In order to reset the iterator to the current collation property settings,
  209. * the API reset() has to be called.
  210. * @param elems The UCollationElements to set.
  211. * @param offset The desired character offset.
  212. * @param status A pointer to a UErrorCode to receive any errors.
  213. * @see ucol_getOffset
  214. * @stable ICU 2.0
  215. */
  216. U_STABLE void U_EXPORT2
  217. ucol_setOffset(UCollationElements *elems,
  218. int32_t offset,
  219. UErrorCode *status);
  220. /**
  221. * Get the primary order of a collation order.
  222. * @param order the collation order
  223. * @return the primary order of a collation order.
  224. * @stable ICU 2.6
  225. */
  226. U_STABLE int32_t U_EXPORT2
  227. ucol_primaryOrder (int32_t order);
  228. /**
  229. * Get the secondary order of a collation order.
  230. * @param order the collation order
  231. * @return the secondary order of a collation order.
  232. * @stable ICU 2.6
  233. */
  234. U_STABLE int32_t U_EXPORT2
  235. ucol_secondaryOrder (int32_t order);
  236. /**
  237. * Get the tertiary order of a collation order.
  238. * @param order the collation order
  239. * @return the tertiary order of a collation order.
  240. * @stable ICU 2.6
  241. */
  242. U_STABLE int32_t U_EXPORT2
  243. ucol_tertiaryOrder (int32_t order);
  244. #endif /* #if !UCONFIG_NO_COLLATION */
  245. #endif