15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)******************************************************************************
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*                                                                            *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)* Copyright (C) 2003-2013, International Business Machines                   *
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*                Corporation and others. All Rights Reserved.                *
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*                                                                            *
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)******************************************************************************
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*   file name:  ulocdata.c
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*   encoding:   US-ASCII
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*   tab size:   8 (not used)
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*   indentation:4
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*   created on: 2003Oct21
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*   created by: Ram Viswanadha,John Emmons
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)*/
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "cmemory.h"
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "unicode/ustring.h"
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "unicode/ures.h"
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "unicode/uloc.h"
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "unicode/ulocdata.h"
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "uresimp.h"
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "ureslocs.h"
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define MEASUREMENT_SYSTEM  "MeasurementSystem"
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define PAPER_SIZE          "PaperSize"
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/** A locale data object.
2993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *  For usage in C programs.
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) *  @draft ICU 3.4
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) */
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)struct ULocaleData {
3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    /**
3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)     * Controls the "No Substitute" behavior of this locale data object
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)     */
3653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    UBool noSubstitute;
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /**
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)     * Pointer to the resource bundle associated with this locale data object
407757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch     */
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    UResourceBundle *bundle;
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    /**
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)     * Pointer to the lang resource bundle associated with this locale data object
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)     */
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    UResourceBundle *langBundle;
4793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)};
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)U_CAPI ULocaleData* U_EXPORT2
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)ulocdata_open(const char *localeID, UErrorCode *status)
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   ULocaleData *uld;
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   if (U_FAILURE(*status)) {
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)       return NULL;
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   }
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   uld = (ULocaleData *)uprv_malloc(sizeof(ULocaleData));
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   if (uld == NULL) {
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      *status = U_MEMORY_ALLOCATION_ERROR;
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      return(NULL);
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   }
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   uld->langBundle = NULL;
657757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   uld->noSubstitute = FALSE;
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   uld->bundle = ures_open(NULL, localeID, status);
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   uld->langBundle = ures_open(U_ICUDATA_LANG, localeID, status);
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   if (U_FAILURE(*status)) {
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      uprv_free(uld);
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      return NULL;
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   }
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   return uld;
76926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)U_CAPI void U_EXPORT2
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)ulocdata_close(ULocaleData *uld)
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if ( uld != NULL ) {
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)       ures_close(uld->langBundle);
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)       ures_close(uld->bundle);
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)       uprv_free(uld);
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)U_CAPI void U_EXPORT2
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)ulocdata_setNoSubstitute(ULocaleData *uld, UBool setting)
905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   uld->noSubstitute = setting;
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)U_CAPI UBool U_EXPORT2
955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)ulocdata_getNoSubstitute(ULocaleData *uld)
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)   return uld->noSubstitute;
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)U_CAPI USet* U_EXPORT2
1015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)ulocdata_getExemplarSet(ULocaleData *uld, USet *fillIn,
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                        uint32_t options, ULocaleDataExemplarSetType extype, UErrorCode *status){
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    static const char* const exemplarSetTypes[] = { "ExemplarCharacters",
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                                                    "AuxExemplarCharacters",
1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                                                    "ExemplarCharactersIndex",
1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                                                    "ExemplarCharactersPunctuation"};
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    const UChar *exemplarChars = NULL;
1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    int32_t len = 0;
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    UErrorCode localStatus = U_ZERO_ERROR;
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (U_FAILURE(*status))
1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return NULL;
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    exemplarChars = ures_getStringByKey(uld->bundle, exemplarSetTypes[extype], &len, &localStatus);
1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        localStatus = U_MISSING_RESOURCE_ERROR;
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
1195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (localStatus != U_ZERO_ERROR) {
1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        *status = localStatus;
1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (U_FAILURE(*status))
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return NULL;
1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    if(fillIn != NULL)
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        uset_applyPattern(fillIn, exemplarChars, len,
1295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                          USET_IGNORE_SPACE | options, status);
1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    else
1315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        fillIn = uset_openPatternOptions(exemplarChars, len,
1325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)                                         USET_IGNORE_SPACE | options, status);
1335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
134    return fillIn;
135
136}
137
138U_CAPI int32_t U_EXPORT2
139ulocdata_getDelimiter(ULocaleData *uld, ULocaleDataDelimiterType type,
140                      UChar *result, int32_t resultLength, UErrorCode *status){
141
142    static const char* const delimiterKeys[] =  {
143        "quotationStart",
144        "quotationEnd",
145        "alternateQuotationStart",
146        "alternateQuotationEnd"
147    };
148
149    UResourceBundle *delimiterBundle;
150    int32_t len = 0;
151    const UChar *delimiter = NULL;
152    UErrorCode localStatus = U_ZERO_ERROR;
153
154    if (U_FAILURE(*status))
155        return 0;
156
157    delimiterBundle = ures_getByKey(uld->bundle, "delimiters", NULL, &localStatus);
158
159    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
160        localStatus = U_MISSING_RESOURCE_ERROR;
161    }
162
163    if (localStatus != U_ZERO_ERROR) {
164        *status = localStatus;
165    }
166
167    if (U_FAILURE(*status)){
168        ures_close(delimiterBundle);
169        return 0;
170    }
171
172    delimiter = ures_getStringByKey(delimiterBundle, delimiterKeys[type], &len, &localStatus);
173    ures_close(delimiterBundle);
174
175    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
176        localStatus = U_MISSING_RESOURCE_ERROR;
177    }
178
179    if (localStatus != U_ZERO_ERROR) {
180        *status = localStatus;
181    }
182
183    if (U_FAILURE(*status)){
184        return 0;
185    }
186
187    u_strncpy(result,delimiter, resultLength);
188    return len;
189}
190
191static UResourceBundle * measurementTypeBundleForLocale(const char *localeID, const char *measurementType, UErrorCode *status){
192    char fullLoc[ULOC_FULLNAME_CAPACITY];
193    char region[ULOC_COUNTRY_CAPACITY];
194    UResourceBundle *rb;
195    UResourceBundle *measTypeBundle = NULL;
196
197    /* The following code is basically copied from Calendar::setWeekData and
198     * Calendar::getCalendarTypeForLocale with adjustments for resource name
199     */
200    uloc_addLikelySubtags(localeID, fullLoc, ULOC_FULLNAME_CAPACITY, status);
201    uloc_getCountry(fullLoc, region, ULOC_COUNTRY_CAPACITY, status);
202
203    rb = ures_openDirect(NULL, "supplementalData", status);
204    ures_getByKey(rb, "measurementData", rb, status);
205    if (rb != NULL) {
206        UResourceBundle *measDataBundle = ures_getByKey(rb, region, NULL, status);
207        if (U_SUCCESS(*status)) {
208        	measTypeBundle = ures_getByKey(measDataBundle, measurementType, NULL, status);
209        }
210        if (*status == U_MISSING_RESOURCE_ERROR) {
211            *status = U_ZERO_ERROR;
212            if (measDataBundle != NULL) {
213                ures_close(measDataBundle);
214            }
215            measDataBundle = ures_getByKey(rb, "001", NULL, status);
216            measTypeBundle = ures_getByKey(measDataBundle, measurementType, NULL, status);
217        }
218        ures_close(measDataBundle);
219    }
220    ures_close(rb);
221    return measTypeBundle;
222}
223
224U_CAPI UMeasurementSystem U_EXPORT2
225ulocdata_getMeasurementSystem(const char *localeID, UErrorCode *status){
226
227    UResourceBundle* measurement=NULL;
228    UMeasurementSystem system = UMS_LIMIT;
229
230    if(status == NULL || U_FAILURE(*status)){
231        return system;
232    }
233
234    measurement = measurementTypeBundleForLocale(localeID, MEASUREMENT_SYSTEM, status);
235    system = (UMeasurementSystem) ures_getInt(measurement, status);
236
237    ures_close(measurement);
238
239    return system;
240
241}
242
243U_CAPI void U_EXPORT2
244ulocdata_getPaperSize(const char* localeID, int32_t *height, int32_t *width, UErrorCode *status){
245    UResourceBundle* paperSizeBundle = NULL;
246    const int32_t* paperSize=NULL;
247    int32_t len = 0;
248
249    if(status == NULL || U_FAILURE(*status)){
250        return;
251    }
252
253    paperSizeBundle = measurementTypeBundleForLocale(localeID, PAPER_SIZE, status);
254    paperSize = ures_getIntVector(paperSizeBundle, &len,  status);
255
256    if(U_SUCCESS(*status)){
257        if(len < 2){
258            *status = U_INTERNAL_PROGRAM_ERROR;
259        }else{
260            *height = paperSize[0];
261            *width  = paperSize[1];
262        }
263    }
264
265    ures_close(paperSizeBundle);
266
267}
268
269U_CAPI void U_EXPORT2
270ulocdata_getCLDRVersion(UVersionInfo versionArray, UErrorCode *status) {
271    UResourceBundle *rb = NULL;
272    rb = ures_openDirect(NULL, "supplementalData", status);
273    ures_getVersionByKey(rb, "cldrVersion", versionArray, status);
274    ures_close(rb);
275}
276
277U_CAPI int32_t U_EXPORT2
278ulocdata_getLocaleDisplayPattern(ULocaleData *uld,
279                                 UChar *result,
280                                 int32_t resultCapacity,
281                                 UErrorCode *status) {
282    UResourceBundle *patternBundle;
283    int32_t len = 0;
284    const UChar *pattern = NULL;
285    UErrorCode localStatus = U_ZERO_ERROR;
286
287    if (U_FAILURE(*status))
288        return 0;
289
290    patternBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", NULL, &localStatus);
291
292    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
293        localStatus = U_MISSING_RESOURCE_ERROR;
294    }
295
296    if (localStatus != U_ZERO_ERROR) {
297        *status = localStatus;
298    }
299
300    if (U_FAILURE(*status)){
301        ures_close(patternBundle);
302        return 0;
303    }
304
305    pattern = ures_getStringByKey(patternBundle, "pattern", &len, &localStatus);
306    ures_close(patternBundle);
307
308    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
309        localStatus = U_MISSING_RESOURCE_ERROR;
310    }
311
312    if (localStatus != U_ZERO_ERROR) {
313        *status = localStatus;
314    }
315
316    if (U_FAILURE(*status)){
317        return 0;
318    }
319
320    u_strncpy(result, pattern, resultCapacity);
321    return len;
322}
323
324
325U_CAPI int32_t U_EXPORT2
326ulocdata_getLocaleSeparator(ULocaleData *uld,
327                            UChar *result,
328                            int32_t resultCapacity,
329                            UErrorCode *status)  {
330    UResourceBundle *separatorBundle;
331    int32_t len = 0;
332    const UChar *separator = NULL;
333    UErrorCode localStatus = U_ZERO_ERROR;
334    UChar *p0, *p1;
335    static const UChar sub0[4] = { 0x007b, 0x0030, 0x007d , 0x0000 }; /* {0} */
336    static const UChar sub1[4] = { 0x007b, 0x0031, 0x007d , 0x0000 }; /* {1} */
337    static const int32_t subLen = 3;
338
339    if (U_FAILURE(*status))
340        return 0;
341
342    separatorBundle = ures_getByKey(uld->langBundle, "localeDisplayPattern", NULL, &localStatus);
343
344    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
345        localStatus = U_MISSING_RESOURCE_ERROR;
346    }
347
348    if (localStatus != U_ZERO_ERROR) {
349        *status = localStatus;
350    }
351
352    if (U_FAILURE(*status)){
353        ures_close(separatorBundle);
354        return 0;
355    }
356
357    separator = ures_getStringByKey(separatorBundle, "separator", &len, &localStatus);
358    ures_close(separatorBundle);
359
360    if ( (localStatus == U_USING_DEFAULT_WARNING) && uld->noSubstitute ) {
361        localStatus = U_MISSING_RESOURCE_ERROR;
362    }
363
364    if (localStatus != U_ZERO_ERROR) {
365        *status = localStatus;
366    }
367
368    if (U_FAILURE(*status)){
369        return 0;
370    }
371
372    /* For backwards compatibility, if we have a pattern, return the portion between {0} and {1} */
373    p0=u_strstr(separator, sub0);
374    p1=u_strstr(separator, sub1);
375    if (p0!=NULL && p1!=NULL && p0<=p1) {
376        separator = (const UChar *)p0 + subLen;
377        len = p1 - separator;
378        /* Desired separator is no longer zero-terminated; handle that if necessary */
379        if (len < resultCapacity) {
380            u_strncpy(result, separator, len);
381            result[len] = 0;
382            return len;
383        }
384    }
385
386    u_strncpy(result, separator, resultCapacity);
387    return len;
388}
389