ubrk.h 20 KB

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