Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/gender.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
*******************************************************************************
5
* Copyright (C) 2008-2013, International Business Machines Corporation and
6
* others. All Rights Reserved.
7
*******************************************************************************
8
*
9
*
10
* File GENDER.CPP
11
*
12
* Modification History:*
13
*   Date        Name        Description
14
*
15
********************************************************************************
16
*/
17
18
#include "unicode/utypes.h"
19
20
#if !UCONFIG_NO_FORMATTING
21
22
#include "unicode/gender.h"
23
#include "unicode/ugender.h"
24
#include "unicode/ures.h"
25
26
#include "cmemory.h"
27
#include "cstring.h"
28
#include "mutex.h"
29
#include "uassert.h"
30
#include "ucln_in.h"
31
#include "umutex.h"
32
#include "uhash.h"
33
34
static UHashtable* gGenderInfoCache = NULL;
35
static UMutex gGenderMetaLock = U_MUTEX_INITIALIZER;
36
static const char* gNeutralStr = "neutral";
37
static const char* gMailTaintsStr = "maleTaints";
38
static const char* gMixedNeutralStr = "mixedNeutral";
39
static icu::GenderInfo* gObjs = NULL;
40
static icu::UInitOnce gGenderInitOnce = U_INITONCE_INITIALIZER;
41
42
enum GenderStyle {
43
  NEUTRAL,
44
  MIXED_NEUTRAL,
45
  MALE_TAINTS,
46
  GENDER_STYLE_LENGTH
47
};
48
49
U_CDECL_BEGIN
50
51
0
static UBool U_CALLCONV gender_cleanup(void) {
52
0
  if (gGenderInfoCache != NULL) {
53
0
    uhash_close(gGenderInfoCache);
54
0
    gGenderInfoCache = NULL;
55
0
    delete [] gObjs;
56
0
  }
57
0
  gGenderInitOnce.reset();
58
0
  return TRUE;
59
0
}
60
61
U_CDECL_END
62
63
U_NAMESPACE_BEGIN
64
65
0
void U_CALLCONV GenderInfo_initCache(UErrorCode &status) {
66
0
  ucln_i18n_registerCleanup(UCLN_I18N_GENDERINFO, gender_cleanup);
67
0
  U_ASSERT(gGenderInfoCache == NULL);
68
0
  if (U_FAILURE(status)) {
69
0
      return;
70
0
  }
71
0
  gObjs = new GenderInfo[GENDER_STYLE_LENGTH];
72
0
  if (gObjs == NULL) {
73
0
    status = U_MEMORY_ALLOCATION_ERROR;
74
0
    return;
75
0
  }
76
0
  for (int i = 0; i < GENDER_STYLE_LENGTH; i++) {
77
0
    gObjs[i]._style = i;
78
0
  }
79
0
  gGenderInfoCache = uhash_open(uhash_hashChars, uhash_compareChars, NULL, &status);
80
0
  if (U_FAILURE(status)) {
81
0
    delete [] gObjs;
82
0
    return;
83
0
  }
84
0
  uhash_setKeyDeleter(gGenderInfoCache, uprv_free);
85
0
}
86
87
88
0
GenderInfo::GenderInfo() {
89
0
}
90
91
0
GenderInfo::~GenderInfo() {
92
0
}
93
94
0
const GenderInfo* GenderInfo::getInstance(const Locale& locale, UErrorCode& status) {
95
0
  // Make sure our cache exists.
96
0
  umtx_initOnce(gGenderInitOnce, &GenderInfo_initCache, status);
97
0
  if (U_FAILURE(status)) {
98
0
    return NULL;
99
0
  }
100
0
101
0
  const GenderInfo* result = NULL;
102
0
  const char* key = locale.getName();
103
0
  {
104
0
    Mutex lock(&gGenderMetaLock);
105
0
    result = (const GenderInfo*) uhash_get(gGenderInfoCache, key);
106
0
  }
107
0
  if (result) {
108
0
    return result;
109
0
  }
110
0
111
0
  // On cache miss, try to create GenderInfo from CLDR data
112
0
  result = loadInstance(locale, status);
113
0
  if (U_FAILURE(status)) {
114
0
    return NULL;
115
0
  }
116
0
117
0
  // Try to put our GenderInfo object in cache. If there is a race condition,
118
0
  // favor the GenderInfo object that is already in the cache.
119
0
  {
120
0
    Mutex lock(&gGenderMetaLock);
121
0
    GenderInfo* temp = (GenderInfo*) uhash_get(gGenderInfoCache, key);
122
0
    if (temp) {
123
0
      result = temp;
124
0
    } else {
125
0
      uhash_put(gGenderInfoCache, uprv_strdup(key), (void*) result, &status);
126
0
      if (U_FAILURE(status)) {
127
0
        return NULL;
128
0
      }
129
0
    }
130
0
  }
131
0
  return result;
132
0
}
133
134
0
const GenderInfo* GenderInfo::loadInstance(const Locale& locale, UErrorCode& status) {
135
0
  LocalUResourceBundlePointer rb(
136
0
      ures_openDirect(NULL, "genderList", &status));
137
0
  if (U_FAILURE(status)) {
138
0
    return NULL;
139
0
  }
140
0
  LocalUResourceBundlePointer locRes(ures_getByKey(rb.getAlias(), "genderList", NULL, &status));
141
0
  if (U_FAILURE(status)) {
142
0
    return NULL;
143
0
  }
144
0
  int32_t resLen = 0;
145
0
  const char* curLocaleName = locale.getName();
146
0
  UErrorCode key_status = U_ZERO_ERROR;
147
0
  const UChar* s = ures_getStringByKey(locRes.getAlias(), curLocaleName, &resLen, &key_status);
148
0
  if (s == NULL) {
149
0
    key_status = U_ZERO_ERROR;
150
0
    char parentLocaleName[ULOC_FULLNAME_CAPACITY];
151
0
    uprv_strcpy(parentLocaleName, curLocaleName);
152
0
    while (s == NULL && uloc_getParent(parentLocaleName, parentLocaleName, ULOC_FULLNAME_CAPACITY, &key_status) > 0) {
153
0
      key_status = U_ZERO_ERROR;
154
0
      resLen = 0;
155
0
      s = ures_getStringByKey(locRes.getAlias(), parentLocaleName, &resLen, &key_status);
156
0
      key_status = U_ZERO_ERROR;
157
0
    }
158
0
  }
159
0
  if (s == NULL) {
160
0
    return &gObjs[NEUTRAL];
161
0
  }
162
0
  char type_str[256];
163
0
  u_UCharsToChars(s, type_str, resLen + 1);
164
0
  if (uprv_strcmp(type_str, gNeutralStr) == 0) {
165
0
    return &gObjs[NEUTRAL];
166
0
  }
167
0
  if (uprv_strcmp(type_str, gMixedNeutralStr) == 0) {
168
0
    return &gObjs[MIXED_NEUTRAL]; 
169
0
  }
170
0
  if (uprv_strcmp(type_str, gMailTaintsStr) == 0) {
171
0
    return &gObjs[MALE_TAINTS];
172
0
  }
173
0
  return &gObjs[NEUTRAL];
174
0
}
175
176
0
UGender GenderInfo::getListGender(const UGender* genders, int32_t length, UErrorCode& status) const {
177
0
  if (U_FAILURE(status)) {
178
0
    return UGENDER_OTHER;
179
0
  }
180
0
  if (length == 0) {
181
0
    return UGENDER_OTHER;
182
0
  }
183
0
  if (length == 1) {
184
0
    return genders[0];
185
0
  }
186
0
  UBool has_female = FALSE;
187
0
  UBool has_male = FALSE;
188
0
  switch (_style) {
189
0
    case NEUTRAL:
190
0
      return UGENDER_OTHER;
191
0
    case MIXED_NEUTRAL:
192
0
      for (int32_t i = 0; i < length; ++i) {
193
0
        switch (genders[i]) {
194
0
          case UGENDER_OTHER:
195
0
            return UGENDER_OTHER;
196
0
            break;
197
0
          case UGENDER_FEMALE:
198
0
            if (has_male) {
199
0
              return UGENDER_OTHER;
200
0
            }
201
0
            has_female = TRUE;
202
0
            break;
203
0
          case UGENDER_MALE:
204
0
            if (has_female) {
205
0
              return UGENDER_OTHER;
206
0
            }
207
0
            has_male = TRUE;
208
0
            break;
209
0
          default:
210
0
            break;
211
0
        }
212
0
      }
213
0
      return has_male ? UGENDER_MALE : UGENDER_FEMALE;
214
0
      break;
215
0
    case MALE_TAINTS:
216
0
      for (int32_t i = 0; i < length; ++i) {
217
0
        if (genders[i] != UGENDER_FEMALE) {
218
0
          return UGENDER_MALE;
219
0
        }
220
0
      }
221
0
      return UGENDER_FEMALE;
222
0
      break;
223
0
    default:
224
0
      return UGENDER_OTHER;
225
0
      break;
226
0
  }
227
0
}
228
229
0
const GenderInfo* GenderInfo::getNeutralInstance() {
230
0
  return &gObjs[NEUTRAL];
231
0
}
232
233
0
const GenderInfo* GenderInfo::getMixedNeutralInstance() {
234
0
  return &gObjs[MIXED_NEUTRAL];
235
0
}
236
237
0
const GenderInfo* GenderInfo::getMaleTaintsInstance() {
238
0
  return &gObjs[MALE_TAINTS];
239
0
}
240
241
U_NAMESPACE_END
242
243
U_CAPI const UGenderInfo* U_EXPORT2
244
0
ugender_getInstance(const char* locale, UErrorCode* status) {
245
0
  return (const UGenderInfo*) icu::GenderInfo::getInstance(locale, *status);
246
0
}
247
248
U_CAPI UGender U_EXPORT2
249
0
ugender_getListGender(const UGenderInfo* genderInfo, const UGender* genders, int32_t size, UErrorCode* status) {
250
0
  return ((const icu::GenderInfo *)genderInfo)->getListGender(genders, size, *status);
251
0
}
252
253
#endif /* #if !UCONFIG_NO_FORMATTING */