-----------------------------------------------------------------------------
| 151 | |
| 152 | //----------------------------------------------------------------------------- |
| 153 | U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len) |
| 154 | { |
| 155 | AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator."); |
| 156 | PROFILE_SCOPE(convertUTF8toUTF16); |
| 157 | |
| 158 | #ifdef TORQUE_ENABLE_UTF16_CACHE |
| 159 | // If we have cached this conversion already, don't do it again |
| 160 | U32 hashKey = Torque::hash((const U8 *)unistring, dStrlen(unistring), 0); |
| 161 | UTF16CacheTable::Iterator cacheItr = sgUTF16Cache.find(hashKey); |
| 162 | if(cacheItr != sgUTF16Cache.end()) |
| 163 | { |
| 164 | const UTF16Cache &cache = (*cacheItr).value; |
| 165 | cache.copyToBuffer(outbuffer, len); |
| 166 | return getMin(cache.mLength,len - 1); |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | U32 walked, nCodepoints; |
| 171 | UTF32 middleman; |
| 172 | |
| 173 | nCodepoints=0; |
| 174 | while(*unistring != '\0' && nCodepoints < len) |
| 175 | { |
| 176 | walked = 1; |
| 177 | middleman = oneUTF8toUTF32(unistring,&walked); |
| 178 | outbuffer[nCodepoints] = oneUTF32toUTF16(middleman); |
| 179 | unistring+=walked; |
| 180 | nCodepoints++; |
| 181 | } |
| 182 | |
| 183 | nCodepoints = getMin(nCodepoints,len - 1); |
| 184 | outbuffer[nCodepoints] = '\0'; |
| 185 | |
| 186 | #ifdef TORQUE_ENABLE_UTF16_CACHE |
| 187 | // Cache the results. |
| 188 | // FIXME As written, this will result in some unnecessary memory copying due to copy constructor calls. |
| 189 | UTF16Cache cache(outbuffer, nCodepoints); |
| 190 | sgUTF16Cache.insertUnique(hashKey, cache); |
| 191 | #endif |
| 192 | |
| 193 | return nCodepoints; |
| 194 | } |
| 195 | |
| 196 | //----------------------------------------------------------------------------- |
| 197 | U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len) |
no test coverage detected