ucoleitr.h 9.3 KB

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