ulocbuilder.h 17 KB

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