Collate.cxx 2.64 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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

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

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

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

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

#ifdef HAVE_ICU
static UCollator *collator;
#endif

50 51
#ifdef HAVE_ICU

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

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

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

	ucol_close(collator);
}

#endif

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

#ifdef HAVE_ICU
	assert(collator != nullptr);

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

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

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

	try {
		wb = MultiByteToWideChar(CP_UTF8, b);
106
	} catch (...) {
107
		return 1;
108
	}
109 110 111 112 113 114 115 116 117 118 119 120 121

	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;
122
#else
123
	return strcoll(a, b);
124 125
#endif
}