ulocbuilder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. // © 2023 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #ifndef __ULOCBUILDER_H__
  4. #define __ULOCBUILDER_H__
  5. #include "unicode/localpointer.h"
  6. #include "unicode/ulocale.h"
  7. #include "unicode/utypes.h"
  8. /**
  9. * \file
  10. * \brief C API: Builder API for Locale
  11. */
  12. #ifndef U_HIDE_DRAFT_API
  13. /**
  14. * Opaque C service object type for the locale builder API
  15. * @draft ICU 74
  16. */
  17. struct ULocaleBuilder;
  18. /**
  19. * C typedef for struct ULocaleBuilder.
  20. * @draft ICU 74
  21. */
  22. typedef struct ULocaleBuilder ULocaleBuilder;
  23. /**
  24. * <code>ULocaleBuilder</code> is used to build valid <code>locale</code> id
  25. * string or IETF BCP 47 language tag from values configured by the setters.
  26. * The <code>ULocaleBuilder</code> checks if a value configured by a
  27. * setter satisfies the syntax requirements defined by the <code>Locale</code>
  28. * class. A string of Locale created by a <code>ULocaleBuilder</code> is
  29. * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
  30. * without losing information.
  31. *
  32. * <p>The following example shows how to create a <code>locale</code> string
  33. * with the <code>ULocaleBuilder</code>.
  34. * <blockquote>
  35. * <pre>
  36. * UErrorCode err = U_ZERO_ERROR;
  37. * char buffer[ULOC_FULLNAME_CAPACITY];
  38. * ULocaleBuilder* builder = ulocbld_open();
  39. * ulocbld_setLanguage(builder, "sr", -1);
  40. * ulocbld_setScript(builder, "Latn", -1);
  41. * ulocbld_setRegion(builder, "RS", -1);
  42. * int32_t length = ulocbld_buildLocaleID(
  43. * builder, buffer, ULOC_FULLNAME_CAPACITY, &error);
  44. * ulocbld_close(builder);
  45. * </pre>
  46. * </blockquote>
  47. *
  48. * <p>ULocaleBuilders can be reused; <code>ulocbld_clear()</code> resets all
  49. * fields to their default values.
  50. *
  51. * <p>ULocaleBuilder tracks errors in an internal UErrorCode. For all setters,
  52. * except ulocbld_setLanguageTag and ulocbld_setLocale, ULocaleBuilder will return immediately
  53. * if the internal UErrorCode is in error state.
  54. * To reset internal state and error code, call clear method.
  55. * The ulocbld_setLanguageTag and setLocale method will first clear the internal
  56. * UErrorCode, then track the error of the validation of the input parameter
  57. * into the internal UErrorCode.
  58. *
  59. * @draft ICU 74
  60. */
  61. /**
  62. * Constructs an empty ULocaleBuilder. The default value of all
  63. * fields, extensions, and private use information is the
  64. * empty string. The created builder should be destroyed by calling
  65. * ulocbld_close();
  66. *
  67. * @draft ICU 74
  68. */
  69. U_CAPI ULocaleBuilder* U_EXPORT2
  70. ulocbld_open(void);
  71. /**
  72. * Close the builder and destroy it's internal states.
  73. * @param builder the builder
  74. * @draft ICU 74
  75. */
  76. U_CAPI void U_EXPORT2
  77. ulocbld_close(ULocaleBuilder* builder);
  78. /**
  79. * Resets the <code>ULocaleBuilder</code> to match the provided
  80. * <code>locale</code>. Existing state is discarded.
  81. *
  82. * <p>All fields of the locale must be well-formed.
  83. * <p>This method clears the internal UErrorCode.
  84. *
  85. * @param builder the builder
  86. * @param locale the locale, a const char * pointer (need not be terminated when
  87. * the length is non-negative)
  88. * @param length the length of the locale; if negative, then the locale need to be
  89. * null terminated,
  90. *
  91. * @draft ICU 74
  92. */
  93. U_CAPI void U_EXPORT2
  94. ulocbld_setLocale(ULocaleBuilder* builder, const char* locale, int32_t length);
  95. /**
  96. * Resets the <code>ULocaleBuilder</code> to match the provided
  97. * <code>ULocale</code>. Existing state is discarded.
  98. *
  99. * <p>The locale must be not bogus.
  100. * <p>This method clears the internal UErrorCode.
  101. *
  102. * @param builder the builder.
  103. * @param locale the locale, a ULocale* pointer. The builder adopts the locale
  104. * after the call and the client must not delete it.
  105. *
  106. * @draft ICU 74
  107. */
  108. U_CAPI void U_EXPORT2
  109. ulocbld_adoptULocale(ULocaleBuilder* builder, ULocale* locale);
  110. /**
  111. * Resets the ULocaleBuilder to match the provided IETF BCP 47 language tag.
  112. * Discards the existing state.
  113. * The empty string causes the builder to be reset, like {@link #ulocbld_clear}.
  114. * Legacy language tags (marked as “Type: grandfathered” in BCP 47)
  115. * are converted to their canonical form before being processed.
  116. * Otherwise, the <code>language tag</code> must be well-formed,
  117. * or else the ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods
  118. * will later report an U_ILLEGAL_ARGUMENT_ERROR.
  119. *
  120. * <p>This method clears the internal UErrorCode.
  121. *
  122. * @param builder the builder
  123. * @param tag the language tag, defined as IETF BCP 47 language tag, a
  124. * const char * pointer (need not be terminated when
  125. * the length is non-negative)
  126. * @param length the length of the tag; if negative, then the tag need to be
  127. * null terminated,
  128. * @draft ICU 74
  129. */
  130. U_CAPI void U_EXPORT2
  131. ulocbld_setLanguageTag(ULocaleBuilder* builder, const char* tag, int32_t length);
  132. /**
  133. * Sets the language. If <code>language</code> is the empty string, the
  134. * language in this <code>ULocaleBuilder</code> is removed. Otherwise, the
  135. * <code>language</code> must be well-formed, or else the ulocbld_buildLocaleID()
  136. * and ulocbld_buildLanguageTag() methods will
  137. * later report an U_ILLEGAL_ARGUMENT_ERROR.
  138. *
  139. * <p>The syntax of language value is defined as
  140. * [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag).
  141. *
  142. * @param builder the builder
  143. * @param language the language, a const char * pointer (need not be terminated when
  144. * the length is non-negative)
  145. * @param length the length of the language; if negative, then the language need to be
  146. * null terminated,
  147. * @draft ICU 74
  148. */
  149. U_CAPI void U_EXPORT2
  150. ulocbld_setLanguage(ULocaleBuilder* builder, const char* language, int32_t length);
  151. /**
  152. * Sets the script. If <code>script</code> is the empty string, the script in
  153. * this <code>ULocaleBuilder</code> is removed.
  154. * Otherwise, the <code>script</code> must be well-formed, or else the
  155. * ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods will later
  156. * report an U_ILLEGAL_ARGUMENT_ERROR.
  157. *
  158. * <p>The script value is a four-letter script code as
  159. * [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag)
  160. * defined by ISO 15924
  161. *
  162. * @param builder the builder
  163. * @param script the script, a const char * pointer (need not be terminated when
  164. * the length is non-negative)
  165. * @param length the length of the script; if negative, then the script need to be
  166. * null terminated,
  167. * @draft ICU 74
  168. */
  169. U_CAPI void U_EXPORT2
  170. ulocbld_setScript(ULocaleBuilder* builder, const char* script, int32_t length);
  171. /**
  172. * Sets the region. If region is the empty string, the region in this
  173. * <code>ULocaleBuilder</code> is removed. Otherwise, the <code>region</code>
  174. * must be well-formed, or else the ulocbld_buildLocaleID() and
  175. * ulocbld_buildLanguageTag() methods will later report an
  176. * U_ILLEGAL_ARGUMENT_ERROR.
  177. *
  178. * <p>The region value is defined by
  179. * [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag)
  180. * as a two-letter ISO 3166 code or a three-digit UN M.49 area code.
  181. *
  182. * <p>The region value in the <code>Locale</code> created by the
  183. * <code>ULocaleBuilder</code> is always normalized to upper case.
  184. *
  185. * @param builder the builder
  186. * @param region the region, a const char * pointer (need not be terminated when
  187. * the length is non-negative)
  188. * @param length the length of the region; if negative, then the region need to be
  189. * null terminated,
  190. * @draft ICU 74
  191. */
  192. U_CAPI void U_EXPORT2
  193. ulocbld_setRegion(ULocaleBuilder* builder, const char* region, int32_t length);
  194. /**
  195. * Sets the variant. If variant is the empty string, the variant in this
  196. * <code>ULocaleBuilder</code> is removed. Otherwise, the <code>variant</code>
  197. * must be well-formed, or else the ulocbld_buildLocaleID() and
  198. * ulocbld_buildLanguageTag() methods will later report an
  199. * U_ILLEGAL_ARGUMENT_ERROR.
  200. *
  201. * <p><b>Note:</b> This method checks if <code>variant</code>
  202. * satisfies the
  203. * [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag)
  204. * syntax requirements, and normalizes the value to lowercase letters. However,
  205. * the <code>Locale</code> class does not impose any syntactic
  206. * restriction on variant. To set an ill-formed variant, use a Locale constructor.
  207. * If there are multiple unicode_variant_subtag, the caller must concatenate
  208. * them with '-' as separator (ex: "foobar-fibar").
  209. *
  210. * @param builder the builder
  211. * @param variant the variant, a const char * pointer (need not be terminated when
  212. * the length is non-negative)
  213. * @param length the length of the variant; if negative, then the variant need to be
  214. * null terminated,
  215. * @draft ICU 74
  216. */
  217. U_CAPI void U_EXPORT2
  218. ulocbld_setVariant(ULocaleBuilder* builder, const char* variant, int32_t length);
  219. /**
  220. * Sets the extension for the given key. If the value is the empty string,
  221. * the extension is removed. Otherwise, the <code>key</code> and
  222. * <code>value</code> must be well-formed, or else the ulocbld_buildLocaleID()
  223. * and ulocbld_buildLanguageTag() methods will
  224. * later report an U_ILLEGAL_ARGUMENT_ERROR.
  225. *
  226. * <p><b>Note:</b> The key ('u') is used for the Unicode locale extension.
  227. * Setting a value for this key replaces any existing Unicode locale key/type
  228. * pairs with those defined in the extension.
  229. *
  230. * <p><b>Note:</b> The key ('x') is used for the private use code. To be
  231. * well-formed, the value for this key needs only to have subtags of one to
  232. * eight alphanumeric characters, not two to eight as in the general case.
  233. *
  234. * @param builder the builder
  235. * @param key the extension key
  236. * @param value the value, a const char * pointer (need not be terminated when
  237. * the length is non-negative)
  238. * @param length the length of the value; if negative, then the value need to be
  239. * null terminated,
  240. * @draft ICU 74
  241. */
  242. U_CAPI void U_EXPORT2
  243. ulocbld_setExtension(ULocaleBuilder* builder, char key, const char* value, int32_t length);
  244. /**
  245. * Sets the Unicode locale keyword type for the given key. If the type
  246. * StringPiece is constructed with a nullptr, the keyword is removed.
  247. * If the type is the empty string, the keyword is set without type subtags.
  248. * Otherwise, the key and type must be well-formed, or else the
  249. * ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods will later
  250. * report an U_ILLEGAL_ARGUMENT_ERROR.
  251. *
  252. * <p>Keys and types are converted to lower case.
  253. *
  254. * <p><b>Note</b>:Setting the 'u' extension via {@link #ulocbld_setExtension}
  255. * replaces all Unicode locale keywords with those defined in the
  256. * extension.
  257. *
  258. * @param builder the builder
  259. * @param key the Unicode locale key, a const char * pointer (need not be
  260. * terminated when the length is non-negative)
  261. * @param keyLength the length of the key; if negative, then the key need to be
  262. * null terminated,
  263. * @param type the Unicode locale type, a const char * pointer (need not be
  264. * terminated when the length is non-negative)
  265. * @param typeLength the length of the type; if negative, then the type need to
  266. * be null terminated,
  267. * @return This builder.
  268. * @draft ICU 74
  269. */
  270. U_CAPI void U_EXPORT2
  271. ulocbld_setUnicodeLocaleKeyword(ULocaleBuilder* builder,
  272. const char* key, int32_t keyLength, const char* type, int32_t typeLength);
  273. /**
  274. * Adds a unicode locale attribute, if not already present, otherwise
  275. * has no effect. The attribute must not be empty string and must be
  276. * well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status
  277. * during the ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() calls.
  278. *
  279. * @param builder the builder
  280. * @param attribute the attribute, a const char * pointer (need not be
  281. * terminated when the length is non-negative)
  282. * @param length the length of the attribute; if negative, then the attribute
  283. * need to be null terminated,
  284. * @draft ICU 74
  285. */
  286. U_CAPI void U_EXPORT2
  287. ulocbld_addUnicodeLocaleAttribute(
  288. ULocaleBuilder* builder, const char* attribute, int32_t length);
  289. /**
  290. * Removes a unicode locale attribute, if present, otherwise has no
  291. * effect. The attribute must not be empty string and must be well-formed
  292. * or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the ulocbld_buildLocaleID()
  293. * and ulocbld_buildLanguageTag() calls.
  294. *
  295. * <p>Attribute comparison for removal is case-insensitive.
  296. *
  297. * @param builder the builder
  298. * @param attribute the attribute, a const char * pointer (need not be
  299. * terminated when the length is non-negative)
  300. * @param length the length of the attribute; if negative, then the attribute
  301. * need to be null terminated,
  302. * @draft ICU 74
  303. */
  304. U_CAPI void U_EXPORT2
  305. ulocbld_removeUnicodeLocaleAttribute(
  306. ULocaleBuilder* builder, const char* attribute, int32_t length);
  307. /**
  308. * Resets the builder to its initial, empty state.
  309. * <p>This method clears the internal UErrorCode.
  310. *
  311. * @param builder the builder
  312. * @draft ICU 74
  313. */
  314. U_CAPI void U_EXPORT2
  315. ulocbld_clear(ULocaleBuilder* builder);
  316. /**
  317. * Resets the extensions to their initial, empty state.
  318. * Language, script, region and variant are unchanged.
  319. *
  320. * @param builder the builder
  321. * @draft ICU 74
  322. */
  323. U_CAPI void U_EXPORT2
  324. ulocbld_clearExtensions(ULocaleBuilder* builder);
  325. /**
  326. * Build the LocaleID string from the fields set on this builder.
  327. * If any set methods or during the ulocbld_buildLocaleID() call require memory
  328. * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
  329. * If any of the fields set by the setters are not well-formed, the status
  330. * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
  331. * not change after the ulocbld_buildLocaleID() call and the caller is
  332. * free to keep using the same builder to build more locales.
  333. *
  334. * @param builder the builder
  335. * @param locale the locale id
  336. * @param localeCapacity the size of the locale buffer to store the locale id
  337. * @param err the error code
  338. * @return the length of the locale id in buffer
  339. * @draft ICU 74
  340. */
  341. U_CAPI int32_t U_EXPORT2
  342. ulocbld_buildLocaleID(ULocaleBuilder* builder, char* locale,
  343. int32_t localeCapacity, UErrorCode* err);
  344. /**
  345. * Build the ULocale object from the fields set on this builder.
  346. * If any set methods or during the ulocbld_buildULocale() call require memory
  347. * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
  348. * If any of the fields set by the setters are not well-formed, the status
  349. * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
  350. * not change after the ulocbld_buildULocale() call and the caller is
  351. * free to keep using the same builder to build more locales.
  352. *
  353. * @param builder the builder.
  354. * @param err the error code.
  355. * @return the locale, a ULocale* pointer. The created ULocale must be
  356. * destroyed by calling {@link ulocale_close}.
  357. * @draft ICU 74
  358. */
  359. U_CAPI ULocale* U_EXPORT2
  360. ulocbld_buildULocale(ULocaleBuilder* builder, UErrorCode* err);
  361. /**
  362. * Build the IETF BCP 47 language tag string from the fields set on this builder.
  363. * If any set methods or during the ulocbld_buildLanguageTag() call require memory
  364. * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status.
  365. * If any of the fields set by the setters are not well-formed, the status
  366. * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will
  367. * not change after the ulocbld_buildLanguageTag() call and the caller is free
  368. * to keep using the same builder to build more locales.
  369. *
  370. * @param builder the builder
  371. * @param language the language tag
  372. * @param languageCapacity the size of the language buffer to store the language
  373. * tag
  374. * @param err the error code
  375. * @return the length of the language tag in buffer
  376. * @draft ICU 74
  377. */
  378. U_CAPI int32_t U_EXPORT2
  379. ulocbld_buildLanguageTag(ULocaleBuilder* builder, char* language,
  380. int32_t languageCapacity, UErrorCode* err);
  381. /**
  382. * Sets the UErrorCode if an error occurred while recording sets.
  383. * Preserves older error codes in the outErrorCode.
  384. *
  385. * @param builder the builder
  386. * @param outErrorCode Set to an error code that occurred while setting subtags.
  387. * Unchanged if there is no such error or if outErrorCode
  388. * already contained an error.
  389. * @return true if U_FAILURE(*outErrorCode)
  390. * @draft ICU 74
  391. */
  392. U_CAPI UBool U_EXPORT2
  393. ulocbld_copyErrorTo(const ULocaleBuilder* builder, UErrorCode *outErrorCode);
  394. #if U_SHOW_CPLUSPLUS_API
  395. U_NAMESPACE_BEGIN
  396. /**
  397. * \class LocalULocaleBuilderPointer
  398. * "Smart pointer" class, closes a ULocaleBuilder via ulocbld_close().
  399. * For most methods see the LocalPointerBase base class.
  400. *
  401. * @see LocalPointerBase
  402. * @see LocalPointer
  403. * @draft ICU 74
  404. */
  405. U_DEFINE_LOCAL_OPEN_POINTER(LocalULocaleBuilderPointer, ULocaleBuilder, ulocbld_close);
  406. U_NAMESPACE_END
  407. #endif /* U_SHOW_CPLUSPLUS_API */
  408. #endif /* U_HIDE_DRAFT_API */
  409. #endif // __ULOCBUILDER_H__