lib/icu/Util: use class AllocatedArray
This commit is contained in:
@@ -24,7 +24,7 @@
|
|||||||
#ifdef HAVE_ICU
|
#ifdef HAVE_ICU
|
||||||
#include "Util.hxx"
|
#include "Util.hxx"
|
||||||
#include "Error.hxx"
|
#include "Error.hxx"
|
||||||
#include "util/WritableBuffer.hxx"
|
#include "util/AllocatedArray.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
#include "util/ConstBuffer.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
|
|
||||||
@@ -99,15 +99,10 @@ IcuCollate(const char *a, const char *b)
|
|||||||
const auto au = UCharFromUTF8(a);
|
const auto au = UCharFromUTF8(a);
|
||||||
const auto bu = UCharFromUTF8(b);
|
const auto bu = UCharFromUTF8(b);
|
||||||
|
|
||||||
int result = !au.IsNull() && !bu.IsNull()
|
return !au.IsNull() && !bu.IsNull()
|
||||||
? (int)ucol_strcoll(collator, au.data, au.size,
|
? (int)ucol_strcoll(collator, au.begin(), au.size(),
|
||||||
bu.data, bu.size)
|
bu.begin(), bu.size())
|
||||||
: strcasecmp(a, b);
|
: strcasecmp(a, b);
|
||||||
|
|
||||||
delete[] au.data;
|
|
||||||
delete[] bu.data;
|
|
||||||
|
|
||||||
return result;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#elif defined(WIN32)
|
#elif defined(WIN32)
|
||||||
@@ -149,15 +144,14 @@ IcuCaseFold(const char *src)
|
|||||||
if (u.IsNull())
|
if (u.IsNull())
|
||||||
return AllocatedString<>::Duplicate(src);
|
return AllocatedString<>::Duplicate(src);
|
||||||
|
|
||||||
size_t folded_capacity = u.size * 2u;
|
size_t folded_capacity = u.size() * 2u;
|
||||||
UChar *folded = new UChar[folded_capacity];
|
UChar *folded = new UChar[folded_capacity];
|
||||||
|
|
||||||
UErrorCode error_code = U_ZERO_ERROR;
|
UErrorCode error_code = U_ZERO_ERROR;
|
||||||
size_t folded_length = u_strFoldCase(folded, folded_capacity,
|
size_t folded_length = u_strFoldCase(folded, folded_capacity,
|
||||||
u.data, u.size,
|
u.begin(), u.size(),
|
||||||
U_FOLD_CASE_DEFAULT,
|
U_FOLD_CASE_DEFAULT,
|
||||||
&error_code);
|
&error_code);
|
||||||
delete[] u.data;
|
|
||||||
if (folded_length == 0 || error_code != U_ZERO_ERROR) {
|
if (folded_length == 0 || error_code != U_ZERO_ERROR) {
|
||||||
delete[] folded;
|
delete[] folded;
|
||||||
return AllocatedString<>::Duplicate(src);
|
return AllocatedString<>::Duplicate(src);
|
||||||
|
@@ -23,7 +23,7 @@
|
|||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
#include "util/Macros.hxx"
|
#include "util/Macros.hxx"
|
||||||
#include "util/AllocatedString.hxx"
|
#include "util/AllocatedString.hxx"
|
||||||
#include "util/WritableBuffer.hxx"
|
#include "util/AllocatedArray.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
#include "util/ConstBuffer.hxx"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -142,13 +142,12 @@ IcuConverter::FromUTF8(const char *s) const
|
|||||||
|
|
||||||
// TODO: dynamic buffer?
|
// TODO: dynamic buffer?
|
||||||
char buffer[4096], *target = buffer;
|
char buffer[4096], *target = buffer;
|
||||||
const UChar *source = u.data;
|
const UChar *source = u.begin();
|
||||||
UErrorCode code = U_ZERO_ERROR;
|
UErrorCode code = U_ZERO_ERROR;
|
||||||
|
|
||||||
ucnv_fromUnicode(converter, &target, buffer + ARRAY_SIZE(buffer),
|
ucnv_fromUnicode(converter, &target, buffer + ARRAY_SIZE(buffer),
|
||||||
&source, u.end(),
|
&source, u.end(),
|
||||||
nullptr, true, &code);
|
nullptr, true, &code);
|
||||||
delete[] u.data;
|
|
||||||
|
|
||||||
if (code != U_ZERO_ERROR)
|
if (code != U_ZERO_ERROR)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "Util.hxx"
|
#include "Util.hxx"
|
||||||
#include "util/AllocatedString.hxx"
|
#include "util/AllocatedString.hxx"
|
||||||
|
#include "util/AllocatedArray.hxx"
|
||||||
#include "util/WritableBuffer.hxx"
|
#include "util/WritableBuffer.hxx"
|
||||||
#include "util/ConstBuffer.hxx"
|
#include "util/ConstBuffer.hxx"
|
||||||
|
|
||||||
@@ -28,26 +29,25 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
WritableBuffer<UChar>
|
AllocatedArray<UChar>
|
||||||
UCharFromUTF8(const char *src)
|
UCharFromUTF8(const char *src)
|
||||||
{
|
{
|
||||||
assert(src != nullptr);
|
assert(src != nullptr);
|
||||||
|
|
||||||
const size_t src_length = strlen(src);
|
const size_t src_length = strlen(src);
|
||||||
const size_t dest_capacity = src_length;
|
const size_t dest_capacity = src_length;
|
||||||
UChar *dest = new UChar[dest_capacity];
|
AllocatedArray<UChar> dest(dest_capacity);
|
||||||
|
|
||||||
UErrorCode error_code = U_ZERO_ERROR;
|
UErrorCode error_code = U_ZERO_ERROR;
|
||||||
int32_t dest_length;
|
int32_t dest_length;
|
||||||
u_strFromUTF8(dest, dest_capacity, &dest_length,
|
u_strFromUTF8(dest.begin(), dest_capacity, &dest_length,
|
||||||
src, src_length,
|
src, src_length,
|
||||||
&error_code);
|
&error_code);
|
||||||
if (U_FAILURE(error_code)) {
|
if (U_FAILURE(error_code))
|
||||||
delete[] dest;
|
return {};
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { dest, size_t(dest_length) };
|
dest.SetSize(dest_length);
|
||||||
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
AllocatedString<>
|
AllocatedString<>
|
||||||
|
@@ -24,15 +24,14 @@
|
|||||||
|
|
||||||
#include <unicode/utypes.h>
|
#include <unicode/utypes.h>
|
||||||
|
|
||||||
template<typename T> struct WritableBuffer;
|
|
||||||
template<typename T> struct ConstBuffer;
|
template<typename T> struct ConstBuffer;
|
||||||
|
template<typename T> class AllocatedArray;
|
||||||
template<typename T> class AllocatedString;
|
template<typename T> class AllocatedString;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for u_strFromUTF8(). The returned pointer must be freed
|
* Wrapper for u_strFromUTF8().
|
||||||
* with delete[].
|
|
||||||
*/
|
*/
|
||||||
WritableBuffer<UChar>
|
AllocatedArray<UChar>
|
||||||
UCharFromUTF8(const char *src);
|
UCharFromUTF8(const char *src);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user