Collate.cxx 2.65 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "Collate.hxx"
21
#include "util/AllocatedString.hxx"
22
#include "config.h"
23 24

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

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

35
#ifdef _WIN32
36 37 38 39 40
#include "Win32.hxx"
#include "util/AllocatedString.hxx"
#include <windows.h>
#endif

41
#include <memory>
42
#include <stdexcept>
43

44 45 46 47 48 49 50
#include <assert.h>
#include <string.h>

#ifdef HAVE_ICU
static UCollator *collator;
#endif

51 52
#ifdef HAVE_ICU

53 54
void
IcuCollateInit()
55 56 57
{
	assert(collator == nullptr);

58
	UErrorCode code = U_ZERO_ERROR;
59
	collator = ucol_open("", &code);
60 61 62
	if (collator == nullptr)
		throw FormatRuntimeError("ucol_open() failed: %s",
					 u_errorName(code));
63 64 65
}

void
66
IcuCollateFinish() noexcept
67 68 69 70 71 72 73 74 75 76
{
	assert(collator != nullptr);

	ucol_close(collator);
}

#endif

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

#ifdef HAVE_ICU
	assert(collator != nullptr);

88 89
	UErrorCode code = U_ZERO_ERROR;
	return (int)ucol_strcollUTF8(collator, a, -1, b, -1, &code);
90

91
#elif defined(_WIN32)
92 93 94 95
	AllocatedString<wchar_t> wa = nullptr, wb = nullptr;

	try {
		wa = MultiByteToWideChar(CP_UTF8, a);
96
	} catch (...) {
97 98 99
		try {
			wb = MultiByteToWideChar(CP_UTF8, b);
			return -1;
100
		} catch (...) {
101 102 103 104 105 106
			return 0;
		}
	}

	try {
		wb = MultiByteToWideChar(CP_UTF8, b);
107
	} catch (...) {
108
		return 1;
109
	}
110 111

	auto result = CompareStringEx(LOCALE_NAME_INVARIANT,
112
				      NORM_IGNORECASE,
113 114 115 116 117 118 119 120 121 122
				      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;
123
#else
124
	return strcoll(a, b);
125 126
#endif
}