brkeng.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /**
  4. ************************************************************************************
  5. * Copyright (C) 2006-2012, International Business Machines Corporation and others. *
  6. * All Rights Reserved. *
  7. ************************************************************************************
  8. */
  9. #ifndef BRKENG_H
  10. #define BRKENG_H
  11. #include "unicode/utypes.h"
  12. #include "unicode/uobject.h"
  13. #include "unicode/utext.h"
  14. #include "unicode/uscript.h"
  15. U_NAMESPACE_BEGIN
  16. class UnicodeSet;
  17. class UStack;
  18. class UVector32;
  19. class DictionaryMatcher;
  20. /*******************************************************************
  21. * LanguageBreakEngine
  22. */
  23. /**
  24. * <p>LanguageBreakEngines implement language-specific knowledge for
  25. * finding text boundaries within a run of characters belonging to a
  26. * specific set. The boundaries will be of a specific kind, e.g. word,
  27. * line, etc.</p>
  28. *
  29. * <p>LanguageBreakEngines should normally be implemented so as to
  30. * be shared between threads without locking.</p>
  31. */
  32. class LanguageBreakEngine : public UMemory {
  33. public:
  34. /**
  35. * <p>Default constructor.</p>
  36. *
  37. */
  38. LanguageBreakEngine();
  39. /**
  40. * <p>Virtual destructor.</p>
  41. */
  42. virtual ~LanguageBreakEngine();
  43. /**
  44. * <p>Indicate whether this engine handles a particular character for
  45. * a particular kind of break.</p>
  46. *
  47. * @param c A character which begins a run that the engine might handle
  48. * @return true if this engine handles the particular character and break
  49. * type.
  50. */
  51. virtual UBool handles(UChar32 c) const = 0;
  52. /**
  53. * <p>Find any breaks within a run in the supplied text.</p>
  54. *
  55. * @param text A UText representing the text. The
  56. * iterator is left at the end of the run of characters which the engine
  57. * is capable of handling.
  58. * @param startPos The start of the run within the supplied text.
  59. * @param endPos The end of the run within the supplied text.
  60. * @param foundBreaks A Vector of int32_t to receive the breaks.
  61. * @param status Information on any errors encountered.
  62. * @return The number of breaks found.
  63. */
  64. virtual int32_t findBreaks( UText *text,
  65. int32_t startPos,
  66. int32_t endPos,
  67. UVector32 &foundBreaks,
  68. UBool isPhraseBreaking,
  69. UErrorCode &status) const = 0;
  70. };
  71. /*******************************************************************
  72. * LanguageBreakFactory
  73. */
  74. /**
  75. * <p>LanguageBreakFactorys find and return a LanguageBreakEngine
  76. * that can determine breaks for characters in a specific set, if
  77. * such an object can be found.</p>
  78. *
  79. * <p>If a LanguageBreakFactory is to be shared between threads,
  80. * appropriate synchronization must be used; there is none internal
  81. * to the factory.</p>
  82. *
  83. * <p>A LanguageBreakEngine returned by a LanguageBreakFactory can
  84. * normally be shared between threads without synchronization, unless
  85. * the specific subclass of LanguageBreakFactory indicates otherwise.</p>
  86. *
  87. * <p>A LanguageBreakFactory is responsible for deleting any LanguageBreakEngine
  88. * it returns when it itself is deleted, unless the specific subclass of
  89. * LanguageBreakFactory indicates otherwise. Naturally, the factory should
  90. * not be deleted until the LanguageBreakEngines it has returned are no
  91. * longer needed.</p>
  92. */
  93. class LanguageBreakFactory : public UMemory {
  94. public:
  95. /**
  96. * <p>Default constructor.</p>
  97. *
  98. */
  99. LanguageBreakFactory();
  100. /**
  101. * <p>Virtual destructor.</p>
  102. */
  103. virtual ~LanguageBreakFactory();
  104. /**
  105. * <p>Find and return a LanguageBreakEngine that can find the desired
  106. * kind of break for the set of characters to which the supplied
  107. * character belongs. It is up to the set of available engines to
  108. * determine what the sets of characters are.</p>
  109. *
  110. * @param c A character that begins a run for which a LanguageBreakEngine is
  111. * sought.
  112. * @return A LanguageBreakEngine with the desired characteristics, or 0.
  113. */
  114. virtual const LanguageBreakEngine *getEngineFor(UChar32 c) = 0;
  115. };
  116. /*******************************************************************
  117. * UnhandledEngine
  118. */
  119. /**
  120. * <p>UnhandledEngine is a special subclass of LanguageBreakEngine that
  121. * handles characters that no other LanguageBreakEngine is available to
  122. * handle. It is told the character and the type of break; at its
  123. * discretion it may handle more than the specified character (e.g.,
  124. * the entire script to which that character belongs.</p>
  125. *
  126. * <p>UnhandledEngines may not be shared between threads without
  127. * external synchronization.</p>
  128. */
  129. class UnhandledEngine : public LanguageBreakEngine {
  130. private:
  131. /**
  132. * The sets of characters handled.
  133. * @internal
  134. */
  135. UnicodeSet *fHandled;
  136. public:
  137. /**
  138. * <p>Default constructor.</p>
  139. *
  140. */
  141. UnhandledEngine(UErrorCode &status);
  142. /**
  143. * <p>Virtual destructor.</p>
  144. */
  145. virtual ~UnhandledEngine();
  146. /**
  147. * <p>Indicate whether this engine handles a particular character for
  148. * a particular kind of break.</p>
  149. *
  150. * @param c A character which begins a run that the engine might handle
  151. * @return true if this engine handles the particular character and break
  152. * type.
  153. */
  154. virtual UBool handles(UChar32 c) const override;
  155. /**
  156. * <p>Find any breaks within a run in the supplied text.</p>
  157. *
  158. * @param text A UText representing the text (TODO: UText). The
  159. * iterator is left at the end of the run of characters which the engine
  160. * is capable of handling.
  161. * @param startPos The start of the run within the supplied text.
  162. * @param endPos The end of the run within the supplied text.
  163. * @param foundBreaks An allocated C array of the breaks found, if any
  164. * @param status Information on any errors encountered.
  165. * @return The number of breaks found.
  166. */
  167. virtual int32_t findBreaks( UText *text,
  168. int32_t startPos,
  169. int32_t endPos,
  170. UVector32 &foundBreaks,
  171. UBool isPhraseBreaking,
  172. UErrorCode &status) const override;
  173. /**
  174. * <p>Tell the engine to handle a particular character and break type.</p>
  175. *
  176. * @param c A character which the engine should handle
  177. */
  178. virtual void handleCharacter(UChar32 c);
  179. };
  180. /*******************************************************************
  181. * ICULanguageBreakFactory
  182. */
  183. /**
  184. * <p>ICULanguageBreakFactory is the default LanguageBreakFactory for
  185. * ICU. It creates dictionary-based LanguageBreakEngines from dictionary
  186. * data in the ICU data file.</p>
  187. */
  188. class ICULanguageBreakFactory : public LanguageBreakFactory {
  189. private:
  190. /**
  191. * The stack of break engines created by this factory
  192. * @internal
  193. */
  194. UStack *fEngines;
  195. public:
  196. /**
  197. * <p>Standard constructor.</p>
  198. *
  199. */
  200. ICULanguageBreakFactory(UErrorCode &status);
  201. /**
  202. * <p>Virtual destructor.</p>
  203. */
  204. virtual ~ICULanguageBreakFactory();
  205. /**
  206. * <p>Find and return a LanguageBreakEngine that can find the desired
  207. * kind of break for the set of characters to which the supplied
  208. * character belongs. It is up to the set of available engines to
  209. * determine what the sets of characters are.</p>
  210. *
  211. * @param c A character that begins a run for which a LanguageBreakEngine is
  212. * sought.
  213. * @return A LanguageBreakEngine with the desired characteristics, or 0.
  214. */
  215. virtual const LanguageBreakEngine *getEngineFor(UChar32 c) override;
  216. protected:
  217. /**
  218. * <p>Create a LanguageBreakEngine for the set of characters to which
  219. * the supplied character belongs, for the specified break type.</p>
  220. *
  221. * @param c A character that begins a run for which a LanguageBreakEngine is
  222. * sought.
  223. * @return A LanguageBreakEngine with the desired characteristics, or 0.
  224. */
  225. virtual const LanguageBreakEngine *loadEngineFor(UChar32 c);
  226. /**
  227. * <p>Create a DictionaryMatcher for the specified script and break type.</p>
  228. * @param script An ISO 15924 script code that identifies the dictionary to be
  229. * created.
  230. * @return A DictionaryMatcher with the desired characteristics, or nullptr.
  231. */
  232. virtual DictionaryMatcher *loadDictionaryMatcherFor(UScriptCode script);
  233. };
  234. U_NAMESPACE_END
  235. /* BRKENG_H */
  236. #endif