ubrk.h 19 KB

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