ubrk.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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) 1996-2015, International Business Machines Corporation and others.
  6. * All Rights Reserved.
  7. ******************************************************************************
  8. */
  9. #ifndef UBRK_H
  10. #define UBRK_H
  11. #include "unicode/utypes.h"
  12. #include "unicode/uloc.h"
  13. #include "unicode/utext.h"
  14. #include "unicode/localpointer.h"
  15. /**
  16. * A text-break iterator.
  17. * For usage in C programs.
  18. */
  19. #ifndef UBRK_TYPEDEF_UBREAK_ITERATOR
  20. # define UBRK_TYPEDEF_UBREAK_ITERATOR
  21. /**
  22. * Opaque type representing an ICU Break iterator object.
  23. * @stable ICU 2.0
  24. */
  25. typedef struct UBreakIterator UBreakIterator;
  26. #endif
  27. #if !UCONFIG_NO_BREAK_ITERATION
  28. #include "unicode/parseerr.h"
  29. /**
  30. * \file
  31. * \brief C API: BreakIterator
  32. *
  33. * <h2> BreakIterator C API </h2>
  34. *
  35. * The BreakIterator C API defines methods for finding the location
  36. * of boundaries in text. Pointer to a UBreakIterator maintain a
  37. * current position and scan over text returning the index of characters
  38. * where boundaries occur.
  39. * <p>
  40. * Line boundary analysis determines where a text string can be broken
  41. * when line-wrapping. The mechanism correctly handles punctuation and
  42. * hyphenated words.
  43. * <p>
  44. * Note: The locale keyword "lb" can be used to modify line break
  45. * behavior according to the CSS level 3 line-break options, see
  46. * <http://dev.w3.org/csswg/css-text/#line-breaking>. For example:
  47. * "ja@lb=strict", "zh@lb=loose".
  48. * <p>
  49. * Sentence boundary analysis allows selection with correct
  50. * interpretation of periods within numbers and abbreviations, and
  51. * trailing punctuation marks such as quotation marks and parentheses.
  52. * <p>
  53. * Note: The locale keyword "ss" can be used to enable use of
  54. * segmentation suppression data (preventing breaks in English after
  55. * abbreviations such as "Mr." or "Est.", for example), as follows:
  56. * "en@ss=standard".
  57. * <p>
  58. * Word boundary analysis is used by search and replace functions, as
  59. * well as within text editing applications that allow the user to
  60. * select words with a double click. Word selection provides correct
  61. * interpretation of punctuation marks within and following
  62. * words. Characters that are not part of a word, such as symbols or
  63. * punctuation marks, have word-breaks on both sides.
  64. * <p>
  65. * Character boundary analysis identifies the boundaries of
  66. * "Extended Grapheme Clusters", which are groupings of codepoints
  67. * that should be treated as character-like units for many text operations.
  68. * Please see Unicode Standard Annex #29, Unicode Text Segmentation,
  69. * http://www.unicode.org/reports/tr29/ for additional information
  70. * on grapheme clusters and guidelines on their use.
  71. * <p>
  72. * Title boundary analysis locates all positions,
  73. * typically starts of words, that should be set to Title Case
  74. * when title casing the text.
  75. * <p>
  76. * The text boundary positions are found according to the rules
  77. * described in Unicode Standard Annex #29, Text Boundaries, and
  78. * Unicode Standard Annex #14, Line Breaking Properties. These
  79. * are available at http://www.unicode.org/reports/tr14/ and
  80. * http://www.unicode.org/reports/tr29/.
  81. * <p>
  82. * In addition to the plain C API defined in this header file, an
  83. * object oriented C++ API with equivalent functionality is defined in the
  84. * file brkiter.h.
  85. * <p>
  86. * Code snippets illustrating the use of the Break Iterator APIs
  87. * are available in the ICU User Guide,
  88. * http://icu-project.org/userguide/boundaryAnalysis.html
  89. * and in the sample program icu/source/samples/break/break.cpp
  90. */
  91. /** The possible types of text boundaries. @stable ICU 2.0 */
  92. typedef enum UBreakIteratorType {
  93. /** Character breaks @stable ICU 2.0 */
  94. UBRK_CHARACTER = 0,
  95. /** Word breaks @stable ICU 2.0 */
  96. UBRK_WORD = 1,
  97. /** Line breaks @stable ICU 2.0 */
  98. UBRK_LINE = 2,
  99. /** Sentence breaks @stable ICU 2.0 */
  100. UBRK_SENTENCE = 3,
  101. #ifndef U_HIDE_DEPRECATED_API
  102. /**
  103. * Title Case breaks
  104. * The iterator created using this type locates title boundaries as described for
  105. * Unicode 3.2 only. For Unicode 4.0 and above title boundary iteration,
  106. * please use Word Boundary iterator.
  107. *
  108. * @deprecated ICU 2.8 Use the word break iterator for titlecasing for Unicode 4 and later.
  109. */
  110. UBRK_TITLE = 4,
  111. /**
  112. * One more than the highest normal UBreakIteratorType value.
  113. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
  114. */
  115. UBRK_COUNT = 5
  116. #endif // U_HIDE_DEPRECATED_API
  117. } UBreakIteratorType;
  118. /** Value indicating all text boundaries have been returned.
  119. * @stable ICU 2.0
  120. */
  121. #define UBRK_DONE ((int32_t) -1)
  122. /**
  123. * Enum constants for the word break tags returned by
  124. * getRuleStatus(). A range of values is defined for each category of
  125. * word, to allow for further subdivisions of a category in future releases.
  126. * Applications should check for tag values falling within the range, rather
  127. * than for single individual values.
  128. *
  129. * The numeric values of all of these constants are stable (will not change).
  130. *
  131. * @stable ICU 2.2
  132. */
  133. typedef enum UWordBreak {
  134. /** Tag value for "words" that do not fit into any of other categories.
  135. * Includes spaces and most punctuation. */
  136. UBRK_WORD_NONE = 0,
  137. /** Upper bound for tags for uncategorized words. */
  138. UBRK_WORD_NONE_LIMIT = 100,
  139. /** Tag value for words that appear to be numbers, lower limit. */
  140. UBRK_WORD_NUMBER = 100,
  141. /** Tag value for words that appear to be numbers, upper limit. */
  142. UBRK_WORD_NUMBER_LIMIT = 200,
  143. /** Tag value for words that contain letters, excluding
  144. * hiragana, katakana or ideographic characters, lower limit. */
  145. UBRK_WORD_LETTER = 200,
  146. /** Tag value for words containing letters, upper limit */
  147. UBRK_WORD_LETTER_LIMIT = 300,
  148. /** Tag value for words containing kana characters, lower limit */
  149. UBRK_WORD_KANA = 300,
  150. /** Tag value for words containing kana characters, upper limit */
  151. UBRK_WORD_KANA_LIMIT = 400,
  152. /** Tag value for words containing ideographic characters, lower limit */
  153. UBRK_WORD_IDEO = 400,
  154. /** Tag value for words containing ideographic characters, upper limit */
  155. UBRK_WORD_IDEO_LIMIT = 500
  156. } UWordBreak;
  157. /**
  158. * Enum constants for the line break tags returned by getRuleStatus().
  159. * A range of values is defined for each category of
  160. * word, to allow for further subdivisions of a category in future releases.
  161. * Applications should check for tag values falling within the range, rather
  162. * than for single individual values.
  163. *
  164. * The numeric values of all of these constants are stable (will not change).
  165. *
  166. * @stable ICU 2.8
  167. */
  168. typedef enum ULineBreakTag {
  169. /** Tag value for soft line breaks, positions at which a line break
  170. * is acceptable but not required */
  171. UBRK_LINE_SOFT = 0,
  172. /** Upper bound for soft line breaks. */
  173. UBRK_LINE_SOFT_LIMIT = 100,
  174. /** Tag value for a hard, or mandatory line break */
  175. UBRK_LINE_HARD = 100,
  176. /** Upper bound for hard line breaks. */
  177. UBRK_LINE_HARD_LIMIT = 200
  178. } ULineBreakTag;
  179. /**
  180. * Enum constants for the sentence break tags returned by getRuleStatus().
  181. * A range of values is defined for each category of
  182. * sentence, to allow for further subdivisions of a category in future releases.
  183. * Applications should check for tag values falling within the range, rather
  184. * than for single individual values.
  185. *
  186. * The numeric values of all of these constants are stable (will not change).
  187. *
  188. * @stable ICU 2.8
  189. */
  190. typedef enum USentenceBreakTag {
  191. /** Tag value for for sentences ending with a sentence terminator
  192. * ('.', '?', '!', etc.) character, possibly followed by a
  193. * hard separator (CR, LF, PS, etc.)
  194. */
  195. UBRK_SENTENCE_TERM = 0,
  196. /** Upper bound for tags for sentences ended by sentence terminators. */
  197. UBRK_SENTENCE_TERM_LIMIT = 100,
  198. /** Tag value for for sentences that do not contain an ending
  199. * sentence terminator ('.', '?', '!', etc.) character, but
  200. * are ended only by a hard separator (CR, LF, PS, etc.) or end of input.
  201. */
  202. UBRK_SENTENCE_SEP = 100,
  203. /** Upper bound for tags for sentences ended by a separator. */
  204. UBRK_SENTENCE_SEP_LIMIT = 200
  205. /** Tag value for a hard, or mandatory line break */
  206. } USentenceBreakTag;
  207. /**
  208. * Open a new UBreakIterator for locating text boundaries for a specified locale.
  209. * A UBreakIterator may be used for detecting character, line, word,
  210. * and sentence breaks in text.
  211. * @param type The type of UBreakIterator to open: one of UBRK_CHARACTER, UBRK_WORD,
  212. * UBRK_LINE, UBRK_SENTENCE
  213. * @param locale The locale specifying the text-breaking conventions. Note that
  214. * locale keys such as "lb" and "ss" may be used to modify text break behavior,
  215. * see general discussion of BreakIterator C API.
  216. * @param text The text to be iterated over.
  217. * @param textLength The number of characters in text, or -1 if null-terminated.
  218. * @param status A UErrorCode to receive any errors.
  219. * @return A UBreakIterator for the specified locale.
  220. * @see ubrk_openRules
  221. * @stable ICU 2.0
  222. */
  223. U_STABLE UBreakIterator* U_EXPORT2
  224. ubrk_open(UBreakIteratorType type,
  225. const char *locale,
  226. const UChar *text,
  227. int32_t textLength,
  228. UErrorCode *status);
  229. /**
  230. * Open a new UBreakIterator for locating text boundaries using specified breaking rules.
  231. * The rule syntax is ... (TBD)
  232. * @param rules A set of rules specifying the text breaking conventions.
  233. * @param rulesLength The number of characters in rules, or -1 if null-terminated.
  234. * @param text The text to be iterated over. May be null, in which case ubrk_setText() is
  235. * used to specify the text to be iterated.
  236. * @param textLength The number of characters in text, or -1 if null-terminated.
  237. * @param parseErr Receives position and context information for any syntax errors
  238. * detected while parsing the rules.
  239. * @param status A UErrorCode to receive any errors.
  240. * @return A UBreakIterator for the specified rules.
  241. * @see ubrk_open
  242. * @stable ICU 2.2
  243. */
  244. U_STABLE UBreakIterator* U_EXPORT2
  245. ubrk_openRules(const UChar *rules,
  246. int32_t rulesLength,
  247. const UChar *text,
  248. int32_t textLength,
  249. UParseError *parseErr,
  250. UErrorCode *status);
  251. /**
  252. * Thread safe cloning operation
  253. * @param bi iterator to be cloned
  254. * @param stackBuffer <em>Deprecated functionality as of ICU 52, use NULL.</em><br>
  255. * user allocated space for the new clone. If NULL new memory will be allocated.
  256. * If buffer is not large enough, new memory will be allocated.
  257. * Clients can use the U_BRK_SAFECLONE_BUFFERSIZE.
  258. * @param pBufferSize <em>Deprecated functionality as of ICU 52, use NULL or 1.</em><br>
  259. * pointer to size of allocated space.
  260. * If *pBufferSize == 0, a sufficient size for use in cloning will
  261. * be returned ('pre-flighting')
  262. * If *pBufferSize is not enough for a stack-based safe clone,
  263. * new memory will be allocated.
  264. * @param status to indicate whether the operation went on smoothly or there were errors
  265. * An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were necessary.
  266. * @return pointer to the new clone
  267. * @stable ICU 2.0
  268. */
  269. U_STABLE UBreakIterator * U_EXPORT2
  270. ubrk_safeClone(
  271. const UBreakIterator *bi,
  272. void *stackBuffer,
  273. int32_t *pBufferSize,
  274. UErrorCode *status);
  275. #ifndef U_HIDE_DEPRECATED_API
  276. /**
  277. * A recommended size (in bytes) for the memory buffer to be passed to ubrk_saveClone().
  278. * @deprecated ICU 52. Do not rely on ubrk_safeClone() cloning into any provided buffer.
  279. */
  280. #define U_BRK_SAFECLONE_BUFFERSIZE 1
  281. #endif /* U_HIDE_DEPRECATED_API */
  282. /**
  283. * Close a UBreakIterator.
  284. * Once closed, a UBreakIterator may no longer be used.
  285. * @param bi The break iterator to close.
  286. * @stable ICU 2.0
  287. */
  288. U_STABLE void U_EXPORT2
  289. ubrk_close(UBreakIterator *bi);
  290. #if U_SHOW_CPLUSPLUS_API
  291. U_NAMESPACE_BEGIN
  292. /**
  293. * \class LocalUBreakIteratorPointer
  294. * "Smart pointer" class, closes a UBreakIterator via ubrk_close().
  295. * For most methods see the LocalPointerBase base class.
  296. *
  297. * @see LocalPointerBase
  298. * @see LocalPointer
  299. * @stable ICU 4.4
  300. */
  301. U_DEFINE_LOCAL_OPEN_POINTER(LocalUBreakIteratorPointer, UBreakIterator, ubrk_close);
  302. U_NAMESPACE_END
  303. #endif
  304. /**
  305. * Sets an existing iterator to point to a new piece of text.
  306. * The break iterator retains a pointer to the supplied text.
  307. * The caller must not modify or delete the text while the BreakIterator
  308. * retains the reference.
  309. *
  310. * @param bi The iterator to use
  311. * @param text The text to be set
  312. * @param textLength The length of the text
  313. * @param status The error code
  314. * @stable ICU 2.0
  315. */
  316. U_STABLE void U_EXPORT2
  317. ubrk_setText(UBreakIterator* bi,
  318. const UChar* text,
  319. int32_t textLength,
  320. UErrorCode* status);
  321. /**
  322. * Sets an existing iterator to point to a new piece of text.
  323. *
  324. * All index positions returned by break iterator functions are
  325. * native indices from the UText. For example, when breaking UTF-8
  326. * encoded text, the break positions returned by \ref ubrk_next, \ref ubrk_previous, etc.
  327. * will be UTF-8 string indices, not UTF-16 positions.
  328. *
  329. * @param bi The iterator to use
  330. * @param text The text to be set.
  331. * This function makes a shallow clone of the supplied UText. This means
  332. * that the caller is free to immediately close or otherwise reuse the
  333. * UText that was passed as a parameter, but that the underlying text itself
  334. * must not be altered while being referenced by the break iterator.
  335. * @param status The error code
  336. * @stable ICU 3.4
  337. */
  338. U_STABLE void U_EXPORT2
  339. ubrk_setUText(UBreakIterator* bi,
  340. UText* text,
  341. UErrorCode* status);
  342. /**
  343. * Determine the most recently-returned text boundary.
  344. *
  345. * @param bi The break iterator to use.
  346. * @return The character index most recently returned by \ref ubrk_next, \ref ubrk_previous,
  347. * \ref ubrk_first, or \ref ubrk_last.
  348. * @stable ICU 2.0
  349. */
  350. U_STABLE int32_t U_EXPORT2
  351. ubrk_current(const UBreakIterator *bi);
  352. /**
  353. * Advance the iterator to the boundary following the current boundary.
  354. *
  355. * @param bi The break iterator to use.
  356. * @return The character index of the next text boundary, or UBRK_DONE
  357. * if all text boundaries have been returned.
  358. * @see ubrk_previous
  359. * @stable ICU 2.0
  360. */
  361. U_STABLE int32_t U_EXPORT2
  362. ubrk_next(UBreakIterator *bi);
  363. /**
  364. * Set the iterator position to the boundary preceding the current boundary.
  365. *
  366. * @param bi The break iterator to use.
  367. * @return The character index of the preceding text boundary, or UBRK_DONE
  368. * if all text boundaries have been returned.
  369. * @see ubrk_next
  370. * @stable ICU 2.0
  371. */
  372. U_STABLE int32_t U_EXPORT2
  373. ubrk_previous(UBreakIterator *bi);
  374. /**
  375. * Set the iterator position to zero, the start of the text being scanned.
  376. * @param bi The break iterator to use.
  377. * @return The new iterator position (zero).
  378. * @see ubrk_last
  379. * @stable ICU 2.0
  380. */
  381. U_STABLE int32_t U_EXPORT2
  382. ubrk_first(UBreakIterator *bi);
  383. /**
  384. * Set the iterator position to the index immediately <EM>beyond</EM> the last character in the text being scanned.
  385. * This is not the same as the last character.
  386. * @param bi The break iterator to use.
  387. * @return The character offset immediately <EM>beyond</EM> the last character in the
  388. * text being scanned.
  389. * @see ubrk_first
  390. * @stable ICU 2.0
  391. */
  392. U_STABLE int32_t U_EXPORT2
  393. ubrk_last(UBreakIterator *bi);
  394. /**
  395. * Set the iterator position to the first boundary preceding the specified offset.
  396. * The new position is always smaller than offset, or UBRK_DONE.
  397. * @param bi The break iterator to use.
  398. * @param offset The offset to begin scanning.
  399. * @return The text boundary preceding offset, or UBRK_DONE.
  400. * @see ubrk_following
  401. * @stable ICU 2.0
  402. */
  403. U_STABLE int32_t U_EXPORT2
  404. ubrk_preceding(UBreakIterator *bi,
  405. int32_t offset);
  406. /**
  407. * Advance the iterator to the first boundary following the specified offset.
  408. * The value returned is always greater than offset, or UBRK_DONE.
  409. * @param bi The break iterator to use.
  410. * @param offset The offset to begin scanning.
  411. * @return The text boundary following offset, or UBRK_DONE.
  412. * @see ubrk_preceding
  413. * @stable ICU 2.0
  414. */
  415. U_STABLE int32_t U_EXPORT2
  416. ubrk_following(UBreakIterator *bi,
  417. int32_t offset);
  418. /**
  419. * Get a locale for which text breaking information is available.
  420. * A UBreakIterator in a locale returned by this function will perform the correct
  421. * text breaking for the locale.
  422. * @param index The index of the desired locale.
  423. * @return A locale for which number text breaking information is available, or 0 if none.
  424. * @see ubrk_countAvailable
  425. * @stable ICU 2.0
  426. */
  427. U_STABLE const char* U_EXPORT2
  428. ubrk_getAvailable(int32_t index);
  429. /**
  430. * Determine how many locales have text breaking information available.
  431. * This function is most useful as determining the loop ending condition for
  432. * calls to \ref ubrk_getAvailable.
  433. * @return The number of locales for which text breaking information is available.
  434. * @see ubrk_getAvailable
  435. * @stable ICU 2.0
  436. */
  437. U_STABLE int32_t U_EXPORT2
  438. ubrk_countAvailable(void);
  439. /**
  440. * Returns true if the specfied position is a boundary position. As a side
  441. * effect, leaves the iterator pointing to the first boundary position at
  442. * or after "offset".
  443. * @param bi The break iterator to use.
  444. * @param offset the offset to check.
  445. * @return True if "offset" is a boundary position.
  446. * @stable ICU 2.0
  447. */
  448. U_STABLE UBool U_EXPORT2
  449. ubrk_isBoundary(UBreakIterator *bi, int32_t offset);
  450. /**
  451. * Return the status from the break rule that determined the most recently
  452. * returned break position. The values appear in the rule source
  453. * within brackets, {123}, for example. For rules that do not specify a
  454. * status, a default value of 0 is returned.
  455. * <p>
  456. * For word break iterators, the possible values are defined in enum UWordBreak.
  457. * @stable ICU 2.2
  458. */
  459. U_STABLE int32_t U_EXPORT2
  460. ubrk_getRuleStatus(UBreakIterator *bi);
  461. /**
  462. * Get the statuses from the break rules that determined the most recently
  463. * returned break position. The values appear in the rule source
  464. * within brackets, {123}, for example. The default status value for rules
  465. * that do not explicitly provide one is zero.
  466. * <p>
  467. * For word break iterators, the possible values are defined in enum UWordBreak.
  468. * @param bi The break iterator to use
  469. * @param fillInVec an array to be filled in with the status values.
  470. * @param capacity the length of the supplied vector. A length of zero causes
  471. * the function to return the number of status values, in the
  472. * normal way, without attemtping to store any values.
  473. * @param status receives error codes.
  474. * @return The number of rule status values from rules that determined
  475. * the most recent boundary returned by the break iterator.
  476. * @stable ICU 3.0
  477. */
  478. U_STABLE int32_t U_EXPORT2
  479. ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status);
  480. /**
  481. * Return the locale of the break iterator. You can choose between the valid and
  482. * the actual locale.
  483. * @param bi break iterator
  484. * @param type locale type (valid or actual)
  485. * @param status error code
  486. * @return locale string
  487. * @stable ICU 2.8
  488. */
  489. U_STABLE const char* U_EXPORT2
  490. ubrk_getLocaleByType(const UBreakIterator *bi, ULocDataLocaleType type, UErrorCode* status);
  491. /**
  492. * Set the subject text string upon which the break iterator is operating
  493. * without changing any other aspect of the state.
  494. * The new and previous text strings must have the same content.
  495. *
  496. * This function is intended for use in environments where ICU is operating on
  497. * strings that may move around in memory. It provides a mechanism for notifying
  498. * ICU that the string has been relocated, and providing a new UText to access the
  499. * string in its new position.
  500. *
  501. * Note that the break iterator never copies the underlying text
  502. * of a string being processed, but always operates directly on the original text
  503. * provided by the user. Refreshing simply drops the references to the old text
  504. * and replaces them with references to the new.
  505. *
  506. * Caution: this function is normally used only by very specialized
  507. * system-level code. One example use case is with garbage collection
  508. * that moves the text in memory.
  509. *
  510. * @param bi The break iterator.
  511. * @param text The new (moved) text string.
  512. * @param status Receives errors detected by this function.
  513. *
  514. * @stable ICU 49
  515. */
  516. U_STABLE void U_EXPORT2
  517. ubrk_refreshUText(UBreakIterator *bi,
  518. UText *text,
  519. UErrorCode *status);
  520. #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
  521. #endif