|
|
|||
File indexing completed on 2026-04-17 08:35:11
0001 // © 2023 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 #ifndef __ULOCBUILDER_H__ 0004 #define __ULOCBUILDER_H__ 0005 0006 #include "unicode/localpointer.h" 0007 #include "unicode/ulocale.h" 0008 #include "unicode/utypes.h" 0009 0010 /** 0011 * \file 0012 * \brief C API: Builder API for Locale 0013 */ 0014 0015 /** 0016 * Opaque C service object type for the locale builder API 0017 * @stable ICU 74 0018 */ 0019 struct ULocaleBuilder; 0020 0021 /** 0022 * C typedef for struct ULocaleBuilder. 0023 * @stable ICU 74 0024 */ 0025 typedef struct ULocaleBuilder ULocaleBuilder; 0026 0027 /** 0028 * <code>ULocaleBuilder</code> is used to build valid <code>locale</code> id 0029 * string or IETF BCP 47 language tag from values configured by the setters. 0030 * The <code>ULocaleBuilder</code> checks if a value configured by a 0031 * setter satisfies the syntax requirements defined by the <code>Locale</code> 0032 * class. A string of Locale created by a <code>ULocaleBuilder</code> is 0033 * well-formed and can be transformed to a well-formed IETF BCP 47 language tag 0034 * without losing information. 0035 * 0036 * <p>The following example shows how to create a <code>locale</code> string 0037 * with the <code>ULocaleBuilder</code>. 0038 * <blockquote> 0039 * <pre> 0040 * UErrorCode err = U_ZERO_ERROR; 0041 * char buffer[ULOC_FULLNAME_CAPACITY]; 0042 * ULocaleBuilder* builder = ulocbld_open(); 0043 * ulocbld_setLanguage(builder, "sr", -1); 0044 * ulocbld_setScript(builder, "Latn", -1); 0045 * ulocbld_setRegion(builder, "RS", -1); 0046 * int32_t length = ulocbld_buildLocaleID( 0047 * builder, buffer, ULOC_FULLNAME_CAPACITY, &error); 0048 * ulocbld_close(builder); 0049 * </pre> 0050 * </blockquote> 0051 * 0052 * <p>ULocaleBuilders can be reused; <code>ulocbld_clear()</code> resets all 0053 * fields to their default values. 0054 * 0055 * <p>ULocaleBuilder tracks errors in an internal UErrorCode. For all setters, 0056 * except ulocbld_setLanguageTag and ulocbld_setLocale, ULocaleBuilder will return immediately 0057 * if the internal UErrorCode is in error state. 0058 * To reset internal state and error code, call clear method. 0059 * The ulocbld_setLanguageTag and setLocale method will first clear the internal 0060 * UErrorCode, then track the error of the validation of the input parameter 0061 * into the internal UErrorCode. 0062 * 0063 * @stable ICU 74 0064 */ 0065 0066 /** 0067 * Constructs an empty ULocaleBuilder. The default value of all 0068 * fields, extensions, and private use information is the 0069 * empty string. The created builder should be destroyed by calling 0070 * ulocbld_close(); 0071 * 0072 * @stable ICU 74 0073 */ 0074 U_CAPI ULocaleBuilder* U_EXPORT2 0075 ulocbld_open(void); 0076 0077 /** 0078 * Close the builder and destroy it's internal states. 0079 * @param builder the builder 0080 * @stable ICU 74 0081 */ 0082 U_CAPI void U_EXPORT2 0083 ulocbld_close(ULocaleBuilder* builder); 0084 0085 /** 0086 * Resets the <code>ULocaleBuilder</code> to match the provided 0087 * <code>locale</code>. Existing state is discarded. 0088 * 0089 * <p>All fields of the locale must be well-formed. 0090 * <p>This method clears the internal UErrorCode. 0091 * 0092 * @param builder the builder 0093 * @param locale the locale, a const char * pointer (need not be terminated when 0094 * the length is non-negative) 0095 * @param length the length of the locale; if negative, then the locale need to be 0096 * null terminated, 0097 * 0098 * @stable ICU 74 0099 */ 0100 U_CAPI void U_EXPORT2 0101 ulocbld_setLocale(ULocaleBuilder* builder, const char* locale, int32_t length); 0102 0103 /** 0104 * Resets the <code>ULocaleBuilder</code> to match the provided 0105 * <code>ULocale</code>. Existing state is discarded. 0106 * 0107 * <p>The locale must be not bogus. 0108 * <p>This method clears the internal UErrorCode. 0109 * 0110 * @param builder the builder. 0111 * @param locale the locale, a ULocale* pointer. The builder adopts the locale 0112 * after the call and the client must not delete it. 0113 * 0114 * @stable ICU 74 0115 */ 0116 U_CAPI void U_EXPORT2 0117 ulocbld_adoptULocale(ULocaleBuilder* builder, ULocale* locale); 0118 0119 /** 0120 * Resets the ULocaleBuilder to match the provided IETF BCP 47 language tag. 0121 * Discards the existing state. 0122 * The empty string causes the builder to be reset, like {@link #ulocbld_clear}. 0123 * Legacy language tags (marked as “Type: grandfathered” in BCP 47) 0124 * are converted to their canonical form before being processed. 0125 * Otherwise, the <code>language tag</code> must be well-formed, 0126 * or else the ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods 0127 * will later report an U_ILLEGAL_ARGUMENT_ERROR. 0128 * 0129 * <p>This method clears the internal UErrorCode. 0130 * 0131 * @param builder the builder 0132 * @param tag the language tag, defined as IETF BCP 47 language tag, a 0133 * const char * pointer (need not be terminated when 0134 * the length is non-negative) 0135 * @param length the length of the tag; if negative, then the tag need to be 0136 * null terminated, 0137 * @stable ICU 74 0138 */ 0139 U_CAPI void U_EXPORT2 0140 ulocbld_setLanguageTag(ULocaleBuilder* builder, const char* tag, int32_t length); 0141 0142 /** 0143 * Sets the language. If <code>language</code> is the empty string, the 0144 * language in this <code>ULocaleBuilder</code> is removed. Otherwise, the 0145 * <code>language</code> must be well-formed, or else the ulocbld_buildLocaleID() 0146 * and ulocbld_buildLanguageTag() methods will 0147 * later report an U_ILLEGAL_ARGUMENT_ERROR. 0148 * 0149 * <p>The syntax of language value is defined as 0150 * [unicode_language_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_language_subtag). 0151 * 0152 * @param builder the builder 0153 * @param language the language, a const char * pointer (need not be terminated when 0154 * the length is non-negative) 0155 * @param length the length of the language; if negative, then the language need to be 0156 * null terminated, 0157 * @stable ICU 74 0158 */ 0159 U_CAPI void U_EXPORT2 0160 ulocbld_setLanguage(ULocaleBuilder* builder, const char* language, int32_t length); 0161 0162 /** 0163 * Sets the script. If <code>script</code> is the empty string, the script in 0164 * this <code>ULocaleBuilder</code> is removed. 0165 * Otherwise, the <code>script</code> must be well-formed, or else the 0166 * ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods will later 0167 * report an U_ILLEGAL_ARGUMENT_ERROR. 0168 * 0169 * <p>The script value is a four-letter script code as 0170 * [unicode_script_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_script_subtag) 0171 * defined by ISO 15924 0172 * 0173 * @param builder the builder 0174 * @param script the script, a const char * pointer (need not be terminated when 0175 * the length is non-negative) 0176 * @param length the length of the script; if negative, then the script need to be 0177 * null terminated, 0178 * @stable ICU 74 0179 */ 0180 U_CAPI void U_EXPORT2 0181 ulocbld_setScript(ULocaleBuilder* builder, const char* script, int32_t length); 0182 0183 /** 0184 * Sets the region. If region is the empty string, the region in this 0185 * <code>ULocaleBuilder</code> is removed. Otherwise, the <code>region</code> 0186 * must be well-formed, or else the ulocbld_buildLocaleID() and 0187 * ulocbld_buildLanguageTag() methods will later report an 0188 * U_ILLEGAL_ARGUMENT_ERROR. 0189 * 0190 * <p>The region value is defined by 0191 * [unicode_region_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_region_subtag) 0192 * as a two-letter ISO 3166 code or a three-digit UN M.49 area code. 0193 * 0194 * <p>The region value in the <code>Locale</code> created by the 0195 * <code>ULocaleBuilder</code> is always normalized to upper case. 0196 * 0197 * @param builder the builder 0198 * @param region the region, a const char * pointer (need not be terminated when 0199 * the length is non-negative) 0200 * @param length the length of the region; if negative, then the region need to be 0201 * null terminated, 0202 * @stable ICU 74 0203 */ 0204 U_CAPI void U_EXPORT2 0205 ulocbld_setRegion(ULocaleBuilder* builder, const char* region, int32_t length); 0206 0207 /** 0208 * Sets the variant. If variant is the empty string, the variant in this 0209 * <code>ULocaleBuilder</code> is removed. Otherwise, the <code>variant</code> 0210 * must be well-formed, or else the ulocbld_buildLocaleID() and 0211 * ulocbld_buildLanguageTag() methods will later report an 0212 * U_ILLEGAL_ARGUMENT_ERROR. 0213 * 0214 * <p><b>Note:</b> This method checks if <code>variant</code> 0215 * satisfies the 0216 * [unicode_variant_subtag](http://www.unicode.org/reports/tr35/tr35.html#unicode_variant_subtag) 0217 * syntax requirements, and normalizes the value to lowercase letters. However, 0218 * the <code>Locale</code> class does not impose any syntactic 0219 * restriction on variant. To set an ill-formed variant, use a Locale constructor. 0220 * If there are multiple unicode_variant_subtag, the caller must concatenate 0221 * them with '-' as separator (ex: "foobar-fibar"). 0222 * 0223 * @param builder the builder 0224 * @param variant the variant, a const char * pointer (need not be terminated when 0225 * the length is non-negative) 0226 * @param length the length of the variant; if negative, then the variant need to be 0227 * null terminated, 0228 * @stable ICU 74 0229 */ 0230 U_CAPI void U_EXPORT2 0231 ulocbld_setVariant(ULocaleBuilder* builder, const char* variant, int32_t length); 0232 0233 /** 0234 * Sets the extension for the given key. If the value is the empty string, 0235 * the extension is removed. Otherwise, the <code>key</code> and 0236 * <code>value</code> must be well-formed, or else the ulocbld_buildLocaleID() 0237 * and ulocbld_buildLanguageTag() methods will 0238 * later report an U_ILLEGAL_ARGUMENT_ERROR. 0239 * 0240 * <p><b>Note:</b> The key ('u') is used for the Unicode locale extension. 0241 * Setting a value for this key replaces any existing Unicode locale key/type 0242 * pairs with those defined in the extension. 0243 * 0244 * <p><b>Note:</b> The key ('x') is used for the private use code. To be 0245 * well-formed, the value for this key needs only to have subtags of one to 0246 * eight alphanumeric characters, not two to eight as in the general case. 0247 * 0248 * @param builder the builder 0249 * @param key the extension key 0250 * @param value the value, a const char * pointer (need not be terminated when 0251 * the length is non-negative) 0252 * @param length the length of the value; if negative, then the value need to be 0253 * null terminated, 0254 * @stable ICU 74 0255 */ 0256 U_CAPI void U_EXPORT2 0257 ulocbld_setExtension(ULocaleBuilder* builder, char key, const char* value, int32_t length); 0258 0259 /** 0260 * Sets the Unicode locale keyword type for the given key. If the type 0261 * StringPiece is constructed with a nullptr, the keyword is removed. 0262 * If the type is the empty string, the keyword is set without type subtags. 0263 * Otherwise, the key and type must be well-formed, or else the 0264 * ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() methods will later 0265 * report an U_ILLEGAL_ARGUMENT_ERROR. 0266 * 0267 * <p>Keys and types are converted to lower case. 0268 * 0269 * <p><b>Note</b>:Setting the 'u' extension via {@link #ulocbld_setExtension} 0270 * replaces all Unicode locale keywords with those defined in the 0271 * extension. 0272 * 0273 * @param builder the builder 0274 * @param key the Unicode locale key, a const char * pointer (need not be 0275 * terminated when the length is non-negative) 0276 * @param keyLength the length of the key; if negative, then the key need to be 0277 * null terminated, 0278 * @param type the Unicode locale type, a const char * pointer (need not be 0279 * terminated when the length is non-negative) 0280 * @param typeLength the length of the type; if negative, then the type need to 0281 * be null terminated, 0282 * @return This builder. 0283 * @stable ICU 74 0284 */ 0285 U_CAPI void U_EXPORT2 0286 ulocbld_setUnicodeLocaleKeyword(ULocaleBuilder* builder, 0287 const char* key, int32_t keyLength, const char* type, int32_t typeLength); 0288 0289 /** 0290 * Adds a unicode locale attribute, if not already present, otherwise 0291 * has no effect. The attribute must not be empty string and must be 0292 * well-formed or U_ILLEGAL_ARGUMENT_ERROR will be set to status 0293 * during the ulocbld_buildLocaleID() and ulocbld_buildLanguageTag() calls. 0294 * 0295 * @param builder the builder 0296 * @param attribute the attribute, a const char * pointer (need not be 0297 * terminated when the length is non-negative) 0298 * @param length the length of the attribute; if negative, then the attribute 0299 * need to be null terminated, 0300 * @stable ICU 74 0301 */ 0302 U_CAPI void U_EXPORT2 0303 ulocbld_addUnicodeLocaleAttribute( 0304 ULocaleBuilder* builder, const char* attribute, int32_t length); 0305 0306 /** 0307 * Removes a unicode locale attribute, if present, otherwise has no 0308 * effect. The attribute must not be empty string and must be well-formed 0309 * or U_ILLEGAL_ARGUMENT_ERROR will be set to status during the ulocbld_buildLocaleID() 0310 * and ulocbld_buildLanguageTag() calls. 0311 * 0312 * <p>Attribute comparison for removal is case-insensitive. 0313 * 0314 * @param builder the builder 0315 * @param attribute the attribute, a const char * pointer (need not be 0316 * terminated when the length is non-negative) 0317 * @param length the length of the attribute; if negative, then the attribute 0318 * need to be null terminated, 0319 * @stable ICU 74 0320 */ 0321 U_CAPI void U_EXPORT2 0322 ulocbld_removeUnicodeLocaleAttribute( 0323 ULocaleBuilder* builder, const char* attribute, int32_t length); 0324 0325 /** 0326 * Resets the builder to its initial, empty state. 0327 * <p>This method clears the internal UErrorCode. 0328 * 0329 * @param builder the builder 0330 * @stable ICU 74 0331 */ 0332 U_CAPI void U_EXPORT2 0333 ulocbld_clear(ULocaleBuilder* builder); 0334 0335 /** 0336 * Resets the extensions to their initial, empty state. 0337 * Language, script, region and variant are unchanged. 0338 * 0339 * @param builder the builder 0340 * @stable ICU 74 0341 */ 0342 U_CAPI void U_EXPORT2 0343 ulocbld_clearExtensions(ULocaleBuilder* builder); 0344 0345 /** 0346 * Build the LocaleID string from the fields set on this builder. 0347 * If any set methods or during the ulocbld_buildLocaleID() call require memory 0348 * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status. 0349 * If any of the fields set by the setters are not well-formed, the status 0350 * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will 0351 * not change after the ulocbld_buildLocaleID() call and the caller is 0352 * free to keep using the same builder to build more locales. 0353 * 0354 * @param builder the builder 0355 * @param locale the locale id 0356 * @param localeCapacity the size of the locale buffer to store the locale id 0357 * @param err the error code 0358 * @return the length of the locale id in buffer 0359 * @stable ICU 74 0360 */ 0361 U_CAPI int32_t U_EXPORT2 0362 ulocbld_buildLocaleID(ULocaleBuilder* builder, char* locale, 0363 int32_t localeCapacity, UErrorCode* err); 0364 0365 /** 0366 * Build the ULocale object from the fields set on this builder. 0367 * If any set methods or during the ulocbld_buildULocale() call require memory 0368 * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status. 0369 * If any of the fields set by the setters are not well-formed, the status 0370 * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will 0371 * not change after the ulocbld_buildULocale() call and the caller is 0372 * free to keep using the same builder to build more locales. 0373 * 0374 * @param builder the builder. 0375 * @param err the error code. 0376 * @return the locale, a ULocale* pointer. The created ULocale must be 0377 * destroyed by calling {@link ulocale_close}. 0378 * @stable ICU 74 0379 */ 0380 U_CAPI ULocale* U_EXPORT2 0381 ulocbld_buildULocale(ULocaleBuilder* builder, UErrorCode* err); 0382 0383 /** 0384 * Build the IETF BCP 47 language tag string from the fields set on this builder. 0385 * If any set methods or during the ulocbld_buildLanguageTag() call require memory 0386 * allocation but fail U_MEMORY_ALLOCATION_ERROR will be set to status. 0387 * If any of the fields set by the setters are not well-formed, the status 0388 * will be set to U_ILLEGAL_ARGUMENT_ERROR. The state of the builder will 0389 * not change after the ulocbld_buildLanguageTag() call and the caller is free 0390 * to keep using the same builder to build more locales. 0391 * 0392 * @param builder the builder 0393 * @param language the language tag 0394 * @param languageCapacity the size of the language buffer to store the language 0395 * tag 0396 * @param err the error code 0397 * @return the length of the language tag in buffer 0398 * @stable ICU 74 0399 */ 0400 U_CAPI int32_t U_EXPORT2 0401 ulocbld_buildLanguageTag(ULocaleBuilder* builder, char* language, 0402 int32_t languageCapacity, UErrorCode* err); 0403 0404 /** 0405 * Sets the UErrorCode if an error occurred while recording sets. 0406 * Preserves older error codes in the outErrorCode. 0407 * 0408 * @param builder the builder 0409 * @param outErrorCode Set to an error code that occurred while setting subtags. 0410 * Unchanged if there is no such error or if outErrorCode 0411 * already contained an error. 0412 * @return true if U_FAILURE(*outErrorCode) 0413 * @stable ICU 74 0414 */ 0415 U_CAPI UBool U_EXPORT2 0416 ulocbld_copyErrorTo(const ULocaleBuilder* builder, UErrorCode *outErrorCode); 0417 0418 #if U_SHOW_CPLUSPLUS_API 0419 0420 U_NAMESPACE_BEGIN 0421 0422 /** 0423 * \class LocalULocaleBuilderPointer 0424 * "Smart pointer" class, closes a ULocaleBuilder via ulocbld_close(). 0425 * For most methods see the LocalPointerBase base class. 0426 * 0427 * @see LocalPointerBase 0428 * @see LocalPointer 0429 * @stable ICU 74 0430 */ 0431 U_DEFINE_LOCAL_OPEN_POINTER(LocalULocaleBuilderPointer, ULocaleBuilder, ulocbld_close); 0432 0433 U_NAMESPACE_END 0434 0435 #endif /* U_SHOW_CPLUSPLUS_API */ 0436 0437 #endif // __ULOCBUILDER_H__
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|