brkeng.h 9.7 KB

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