Collate.cxx 2.71 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

#include <unicode/ucol.h>
#include <unicode/ustring.h>
#else
#include <algorithm>
32 33 34 35 36

#ifndef _WIN32
#include <string>
#endif

37 38
#endif

39
#ifdef _WIN32
40 41 42 43 44
#include "Win32.hxx"
#include "util/AllocatedString.hxx"
#include <windows.h>
#endif

45
#include <cassert>
46
#include <memory>
47
#include <stdexcept>
48

49 50 51 52 53 54
#include <string.h>

#ifdef HAVE_ICU
static UCollator *collator;
#endif

55 56
#ifdef HAVE_ICU

57 58
void
IcuCollateInit()
59 60 61
{
	assert(collator == nullptr);

62
	UErrorCode code = U_ZERO_ERROR;
63
	collator = ucol_open("", &code);
64 65 66
	if (collator == nullptr)
		throw FormatRuntimeError("ucol_open() failed: %s",
					 u_errorName(code));
67 68 69
}

void
70
IcuCollateFinish() noexcept
71 72 73 74 75 76 77 78 79 80
{
	assert(collator != nullptr);

	ucol_close(collator);
}

#endif

gcc_pure
int
81
IcuCollate(std::string_view a, std::string_view b) noexcept
82 83 84 85
{
#ifdef HAVE_ICU
	assert(collator != nullptr);

86
	UErrorCode code = U_ZERO_ERROR;
87 88
	return (int)ucol_strcollUTF8(collator, a.data(), a.size(),
				     b.data(), b.size(), &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

	auto result = CompareStringEx(LOCALE_NAME_INVARIANT,
111
				      NORM_IGNORECASE,
112 113 114 115 116 117 118 119 120 121
				      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 124 125
	/* need to duplicate for the fallback because std::string_view
	   is not null-terminated */
	return strcoll(std::string(a).c_str(), std::string(b).c_str());
126 127
#endif
}