ucpmap.h 5.5 KB

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