Collate.cxx 4.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "Collate.hxx"
22
#include "util/AllocatedString.hxx"
23 24

#ifdef HAVE_ICU
25
#include "Util.hxx"
26
#include "util/AllocatedArray.hxx"
27
#include "util/ConstBuffer.hxx"
28
#include "util/RuntimeError.hxx"
29 30 31 32 33 34 35 36

#include <unicode/ucol.h>
#include <unicode/ustring.h>
#else
#include <algorithm>
#include <ctype.h>
#endif

37 38 39 40 41 42
#ifdef WIN32
#include "Win32.hxx"
#include "util/AllocatedString.hxx"
#include <windows.h>
#endif

43
#include <memory>
44
#include <stdexcept>
45

46 47 48 49 50 51 52
#include <assert.h>
#include <string.h>

#ifdef HAVE_ICU
static UCollator *collator;
#endif

53 54
#ifdef HAVE_ICU

55 56
void
IcuCollateInit()
57 58 59
{
	assert(collator == nullptr);

60
	UErrorCode code = U_ZERO_ERROR;
61
	collator = ucol_open("", &code);
62 63 64
	if (collator == nullptr)
		throw FormatRuntimeError("ucol_open() failed: %s",
					 u_errorName(code));
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

void
IcuCollateFinish()
{
	assert(collator != nullptr);

	ucol_close(collator);
}

#endif

gcc_pure
int
IcuCollate(const char *a, const char *b)
{
81 82
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
83 84
	assert(a != nullptr);
	assert(b != nullptr);
85
#endif
86 87 88 89 90

#ifdef HAVE_ICU
	assert(collator != nullptr);

#if U_ICU_VERSION_MAJOR_NUM >= 50
91 92
	UErrorCode code = U_ZERO_ERROR;
	return (int)ucol_strcollUTF8(collator, a, -1, b, -1, &code);
93 94 95
#else
	/* fall back to ucol_strcoll() */

96 97 98
	try {
		const auto au = UCharFromUTF8(a);
		const auto bu = UCharFromUTF8(b);
99

100 101 102 103 104 105
		return ucol_strcoll(collator, au.begin(), au.size(),
				    bu.begin(), bu.size());
	} catch (const std::runtime_error &) {
		/* fall back to plain strcasecmp() */
		return strcasecmp(a, b);
	}
106 107
#endif

108
#elif defined(WIN32)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	AllocatedString<wchar_t> wa = nullptr, wb = nullptr;

	try {
		wa = MultiByteToWideChar(CP_UTF8, a);
	} catch (const std::runtime_error &) {
		try {
			wb = MultiByteToWideChar(CP_UTF8, b);
			return -1;
		} catch (const std::runtime_error &) {
			return 0;
		}
	}

	try {
		wb = MultiByteToWideChar(CP_UTF8, b);
	} catch (const std::runtime_error &) {
125
		return 1;
126
	}
127 128 129 130 131 132 133 134 135 136 137 138 139

	auto result = CompareStringEx(LOCALE_NAME_INVARIANT,
				      LINGUISTIC_IGNORECASE,
				      wa.c_str(), -1,
				      wb.c_str(), -1,
				      nullptr, nullptr, 0);
	if (result != 0)
		/* "To maintain the C runtime convention of comparing
		   strings, the value 2 can be subtracted from a
		   nonzero return value." */
		result -= 2;

	return result;
140
#else
141
	return strcoll(a, b);
142 143 144
#endif
}

145
AllocatedString<>
146
IcuCaseFold(const char *src)
147
try {
148 149
#ifdef HAVE_ICU
	assert(collator != nullptr);
150 151
#if !CLANG_CHECK_VERSION(3,6)
	/* disabled on clang due to -Wtautological-pointer-compare */
152
	assert(src != nullptr);
153
#endif
154

155 156
	const auto u = UCharFromUTF8(src);
	if (u.IsNull())
157
		return AllocatedString<>::Duplicate(src);
158

159
	AllocatedArray<UChar> folded(u.size() * 2u);
160 161

	UErrorCode error_code = U_ZERO_ERROR;
162
	size_t folded_length = u_strFoldCase(folded.begin(), folded.size(),
163
					     u.begin(), u.size(),
164 165
					     U_FOLD_CASE_DEFAULT,
					     &error_code);
166
	if (folded_length == 0 || error_code != U_ZERO_ERROR)
167
		return AllocatedString<>::Duplicate(src);
168

169 170
	folded.SetSize(folded_length);
	return UCharToUTF8({folded.begin(), folded.size()});
171 172 173 174 175 176 177 178 179 180 181

#elif defined(WIN32)
	const auto u = MultiByteToWideChar(CP_UTF8, src);

	const int size = LCMapStringEx(LOCALE_NAME_INVARIANT,
				       LCMAP_SORTKEY|LINGUISTIC_IGNORECASE,
				       u.c_str(), -1, nullptr, 0,
				       nullptr, nullptr, 0);
	if (size <= 0)
		return AllocatedString<>::Duplicate(src);

182
	std::unique_ptr<wchar_t[]> buffer(new wchar_t[size]);
183 184
	if (LCMapStringEx(LOCALE_NAME_INVARIANT,
			  LCMAP_SORTKEY|LINGUISTIC_IGNORECASE,
185 186
			  u.c_str(), -1, buffer.get(), size,
			  nullptr, nullptr, 0) <= 0)
187 188
		return AllocatedString<>::Duplicate(src);

189
	return WideCharToMultiByte(CP_UTF8, buffer.get());
190

191
#else
192
	size_t size = strlen(src) + 1;
193 194
	std::unique_ptr<char[]> buffer(new char[size]);
	size_t nbytes = strxfrm(buffer.get(), src, size);
195 196
	if (nbytes >= size) {
		/* buffer too small - reallocate and try again */
197
		buffer.reset();
198
		size = nbytes + 1;
199 200
		buffer.reset(new char[size]);
		nbytes = strxfrm(buffer.get(), src, size);
201 202 203 204 205
	}

	assert(nbytes < size);
	assert(buffer[nbytes] == 0);

206
	return AllocatedString<>::Donate(buffer.release());
207
#endif
208 209
} catch (const std::runtime_error &) {
	return AllocatedString<>::Duplicate(src);
210
}