lib/icu/Util: use class AllocatedArray

This commit is contained in:
Max Kellermann 2016-04-12 21:32:02 +02:00
parent 60f32d0bce
commit 33a4dbe1e5
4 changed files with 19 additions and 27 deletions

View File

@ -24,7 +24,7 @@
#ifdef HAVE_ICU
#include "Util.hxx"
#include "Error.hxx"
#include "util/WritableBuffer.hxx"
#include "util/AllocatedArray.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx"
@ -99,15 +99,10 @@ IcuCollate(const char *a, const char *b)
const auto au = UCharFromUTF8(a);
const auto bu = UCharFromUTF8(b);
int result = !au.IsNull() && !bu.IsNull()
? (int)ucol_strcoll(collator, au.data, au.size,
bu.data, bu.size)
return !au.IsNull() && !bu.IsNull()
? (int)ucol_strcoll(collator, au.begin(), au.size(),
bu.begin(), bu.size())
: strcasecmp(a, b);
delete[] au.data;
delete[] bu.data;
return result;
#endif
#elif defined(WIN32)
@ -149,15 +144,14 @@ IcuCaseFold(const char *src)
if (u.IsNull())
return AllocatedString<>::Duplicate(src);
size_t folded_capacity = u.size * 2u;
size_t folded_capacity = u.size() * 2u;
UChar *folded = new UChar[folded_capacity];
UErrorCode error_code = U_ZERO_ERROR;
size_t folded_length = u_strFoldCase(folded, folded_capacity,
u.data, u.size,
u.begin(), u.size(),
U_FOLD_CASE_DEFAULT,
&error_code);
delete[] u.data;
if (folded_length == 0 || error_code != U_ZERO_ERROR) {
delete[] folded;
return AllocatedString<>::Duplicate(src);

View File

@ -23,7 +23,7 @@
#include "util/Error.hxx"
#include "util/Macros.hxx"
#include "util/AllocatedString.hxx"
#include "util/WritableBuffer.hxx"
#include "util/AllocatedArray.hxx"
#include "util/ConstBuffer.hxx"
#include <string.h>
@ -142,13 +142,12 @@ IcuConverter::FromUTF8(const char *s) const
// TODO: dynamic buffer?
char buffer[4096], *target = buffer;
const UChar *source = u.data;
const UChar *source = u.begin();
UErrorCode code = U_ZERO_ERROR;
ucnv_fromUnicode(converter, &target, buffer + ARRAY_SIZE(buffer),
&source, u.end(),
nullptr, true, &code);
delete[] u.data;
if (code != U_ZERO_ERROR)
return nullptr;

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "Util.hxx"
#include "util/AllocatedString.hxx"
#include "util/AllocatedArray.hxx"
#include "util/WritableBuffer.hxx"
#include "util/ConstBuffer.hxx"
@ -28,26 +29,25 @@
#include <assert.h>
#include <string.h>
WritableBuffer<UChar>
AllocatedArray<UChar>
UCharFromUTF8(const char *src)
{
assert(src != nullptr);
const size_t src_length = strlen(src);
const size_t dest_capacity = src_length;
UChar *dest = new UChar[dest_capacity];
AllocatedArray<UChar> dest(dest_capacity);
UErrorCode error_code = U_ZERO_ERROR;
int32_t dest_length;
u_strFromUTF8(dest, dest_capacity, &dest_length,
u_strFromUTF8(dest.begin(), dest_capacity, &dest_length,
src, src_length,
&error_code);
if (U_FAILURE(error_code)) {
delete[] dest;
return nullptr;
}
if (U_FAILURE(error_code))
return {};
return { dest, size_t(dest_length) };
dest.SetSize(dest_length);
return dest;
}
AllocatedString<>

View File

@ -24,15 +24,14 @@
#include <unicode/utypes.h>
template<typename T> struct WritableBuffer;
template<typename T> struct ConstBuffer;
template<typename T> class AllocatedArray;
template<typename T> class AllocatedString;
/**
* Wrapper for u_strFromUTF8(). The returned pointer must be freed
* with delete[].
* Wrapper for u_strFromUTF8().
*/
WritableBuffer<UChar>
AllocatedArray<UChar>
UCharFromUTF8(const char *src);
/**