ucpmap.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // ucpmap.h
  4. // created: 2018sep03 Markus W. Scherer
  5. #ifndef __UCPMAP_H__
  6. #define __UCPMAP_H__
  7. #include "unicode/utypes.h"
  8. U_CDECL_BEGIN
  9. /**
  10. * \file
  11. * \brief C API: This file defines an abstract map from Unicode code points to integer values.
  12. *
  13. * @see UCPMap
  14. * @see UCPTrie
  15. * @see UMutableCPTrie
  16. */
  17. /**
  18. * Abstract map from Unicode code points (U+0000..U+10FFFF) to integer values.
  19. *
  20. * @see UCPTrie
  21. * @see UMutableCPTrie
  22. * @stable ICU 63
  23. */
  24. typedef struct UCPMap UCPMap;
  25. /**
  26. * Selectors for how ucpmap_getRange() etc. should report value ranges overlapping with surrogates.
  27. * Most users should use UCPMAP_RANGE_NORMAL.
  28. *
  29. * @see ucpmap_getRange
  30. * @see ucptrie_getRange
  31. * @see umutablecptrie_getRange
  32. * @stable ICU 63
  33. */
  34. enum UCPMapRangeOption {
  35. /**
  36. * ucpmap_getRange() enumerates all same-value ranges as stored in the map.
  37. * Most users should use this option.
  38. * @stable ICU 63
  39. */
  40. UCPMAP_RANGE_NORMAL,
  41. /**
  42. * ucpmap_getRange() enumerates all same-value ranges as stored in the map,
  43. * except that lead surrogates (U+D800..U+DBFF) are treated as having the
  44. * surrogateValue, which is passed to getRange() as a separate parameter.
  45. * The surrogateValue is not transformed via filter().
  46. * See U_IS_LEAD(c).
  47. *
  48. * Most users should use UCPMAP_RANGE_NORMAL instead.
  49. *
  50. * This option is useful for maps that map surrogate code *units* to
  51. * special values optimized for UTF-16 string processing
  52. * or for special error behavior for unpaired surrogates,
  53. * but those values are not to be associated with the lead surrogate code *points*.
  54. * @stable ICU 63
  55. */
  56. UCPMAP_RANGE_FIXED_LEAD_SURROGATES,
  57. /**
  58. * ucpmap_getRange() enumerates all same-value ranges as stored in the map,
  59. * except that all surrogates (U+D800..U+DFFF) are treated as having the
  60. * surrogateValue, which is passed to getRange() as a separate parameter.
  61. * The surrogateValue is not transformed via filter().
  62. * See U_IS_SURROGATE(c).
  63. *
  64. * Most users should use UCPMAP_RANGE_NORMAL instead.
  65. *
  66. * This option is useful for maps that map surrogate code *units* to
  67. * special values optimized for UTF-16 string processing
  68. * or for special error behavior for unpaired surrogates,
  69. * but those values are not to be associated with the lead surrogate code *points*.
  70. * @stable ICU 63
  71. */
  72. UCPMAP_RANGE_FIXED_ALL_SURROGATES
  73. };
  74. #ifndef U_IN_DOXYGEN
  75. typedef enum UCPMapRangeOption UCPMapRangeOption;
  76. #endif
  77. /**
  78. * Returns the value for a code point as stored in the map, with range checking.
  79. * Returns an implementation-defined error value if c is not in the range 0..U+10FFFF.
  80. *
  81. * @param map the map
  82. * @param c the code point
  83. * @return the map value,
  84. * or an implementation-defined error value if the code point is not in the range 0..U+10FFFF
  85. * @stable ICU 63
  86. */
  87. U_CAPI uint32_t U_EXPORT2
  88. ucpmap_get(const UCPMap *map, UChar32 c);
  89. /**
  90. * Callback function type: Modifies a map value.
  91. * Optionally called by ucpmap_getRange()/ucptrie_getRange()/umutablecptrie_getRange().
  92. * The modified value will be returned by the getRange function.
  93. *
  94. * Can be used to ignore some of the value bits,
  95. * make a filter for one of several values,
  96. * return a value index computed from the map value, etc.
  97. *
  98. * @param context an opaque pointer, as passed into the getRange function
  99. * @param value a value from the map
  100. * @return the modified value
  101. * @stable ICU 63
  102. */
  103. typedef uint32_t U_CALLCONV
  104. UCPMapValueFilter(const void *context, uint32_t value);
  105. /**
  106. * Returns the last code point such that all those from start to there have the same value.
  107. * Can be used to efficiently iterate over all same-value ranges in a map.
  108. * (This is normally faster than iterating over code points and get()ting each value,
  109. * but much slower than a data structure that stores ranges directly.)
  110. *
  111. * If the UCPMapValueFilter function pointer is not NULL, then
  112. * the value to be delivered is passed through that function, and the return value is the end
  113. * of the range where all values are modified to the same actual value.
  114. * The value is unchanged if that function pointer is NULL.
  115. *
  116. * Example:
  117. * \code
  118. * UChar32 start = 0, end;
  119. * uint32_t value;
  120. * while ((end = ucpmap_getRange(map, start, UCPMAP_RANGE_NORMAL, 0,
  121. * NULL, NULL, &value)) >= 0) {
  122. * // Work with the range start..end and its value.
  123. * start = end + 1;
  124. * }
  125. * \endcode
  126. *
  127. * @param map the map
  128. * @param start range start
  129. * @param option defines whether surrogates are treated normally,
  130. * or as having the surrogateValue; usually UCPMAP_RANGE_NORMAL
  131. * @param surrogateValue value for surrogates; ignored if option==UCPMAP_RANGE_NORMAL
  132. * @param filter a pointer to a function that may modify the map data value,
  133. * or NULL if the values from the map are to be used unmodified
  134. * @param context an opaque pointer that is passed on to the filter function
  135. * @param pValue if not NULL, receives the value that every code point start..end has;
  136. * may have been modified by filter(context, map value)
  137. * if that function pointer is not NULL
  138. * @return the range end code point, or -1 if start is not a valid code point
  139. * @stable ICU 63
  140. */
  141. U_CAPI UChar32 U_EXPORT2
  142. ucpmap_getRange(const UCPMap *map, UChar32 start,
  143. UCPMapRangeOption option, uint32_t surrogateValue,
  144. UCPMapValueFilter *filter, const void *context, uint32_t *pValue);
  145. U_CDECL_END
  146. #endif