Commit 178f7379 authored by Max Kellermann's avatar Max Kellermann

lib/icu/Collate: use std::unique_ptr

parent 74963bce
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
#include <windows.h> #include <windows.h>
#endif #endif
#include <memory>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
...@@ -169,17 +171,14 @@ IcuCaseFold(const char *src) ...@@ -169,17 +171,14 @@ IcuCaseFold(const char *src)
if (size <= 0) if (size <= 0)
return AllocatedString<>::Duplicate(src); return AllocatedString<>::Duplicate(src);
auto buffer = new wchar_t[size]; std::unique_ptr<wchar_t[]> buffer(new wchar_t[size]);
if (LCMapStringEx(LOCALE_NAME_INVARIANT, if (LCMapStringEx(LOCALE_NAME_INVARIANT,
LCMAP_SORTKEY|LINGUISTIC_IGNORECASE, LCMAP_SORTKEY|LINGUISTIC_IGNORECASE,
u.c_str(), -1, buffer, size, u.c_str(), -1, buffer.get(), size,
nullptr, nullptr, 0) <= 0) { nullptr, nullptr, 0) <= 0)
delete[] buffer;
return AllocatedString<>::Duplicate(src); return AllocatedString<>::Duplicate(src);
}
auto result = WideCharToMultiByte(CP_UTF8, buffer); auto result = WideCharToMultiByte(CP_UTF8, buffer.get());
delete[] buffer;
if (result.IsNull()) if (result.IsNull())
return AllocatedString<>::Duplicate(src); return AllocatedString<>::Duplicate(src);
...@@ -187,20 +186,20 @@ IcuCaseFold(const char *src) ...@@ -187,20 +186,20 @@ IcuCaseFold(const char *src)
#else #else
size_t size = strlen(src) + 1; size_t size = strlen(src) + 1;
auto buffer = new char[size]; std::unique_ptr<char[]> buffer(new char[size]);
size_t nbytes = strxfrm(buffer, src, size); size_t nbytes = strxfrm(buffer.get(), src, size);
if (nbytes >= size) { if (nbytes >= size) {
/* buffer too small - reallocate and try again */ /* buffer too small - reallocate and try again */
delete[] buffer; buffer.reset();
size = nbytes + 1; size = nbytes + 1;
buffer = new char[size]; buffer.reset(new char[size]);
nbytes = strxfrm(buffer, src, size); nbytes = strxfrm(buffer.get(), src, size);
} }
assert(nbytes < size); assert(nbytes < size);
assert(buffer[nbytes] == 0); assert(buffer[nbytes] == 0);
return AllocatedString<>::Donate(buffer); return AllocatedString<>::Donate(buffer.release());
#endif #endif
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment