Compare.hxx 2.04 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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.
 */

#ifndef MPD_ICU_COMPARE_HXX
#define MPD_ICU_COMPARE_HXX

#include "util/AllocatedString.hxx"

25 26
#include <string_view>

27 28 29 30
#ifdef _WIN32
#include <wchar.h>
#endif

31 32 33 34 35 36
/**
 * This class can compare one string ("needle") with lots of other
 * strings ("haystacks") efficiently, ignoring case.  With some
 * configurations, it can prepare a case-folded version of the needle.
 */
class IcuCompare {
37 38 39
#ifdef _WIN32
	/* Windows API functions work with wchar_t strings, so let's
	   cache the MultiByteToWideChar() result for performance */
40
	using AllocatedString = BasicAllocatedString<wchar_t>;
41
#endif
42

43 44
	AllocatedString needle;

45 46 47
public:
	IcuCompare():needle(nullptr) {}

48
	explicit IcuCompare(std::string_view needle) noexcept;
49

50 51
	IcuCompare(const IcuCompare &src) noexcept
		:needle(src
52
			? AllocatedString(src.needle)
53 54 55 56
			: nullptr) {}

	IcuCompare &operator=(const IcuCompare &src) noexcept {
		needle = src
57
			? AllocatedString(src.needle)
58 59 60 61
			: nullptr;
		return *this;
	}

62 63 64
	IcuCompare(IcuCompare &&) = default;
	IcuCompare &operator=(IcuCompare &&) = default;

65
	[[gnu::pure]]
66
	operator bool() const noexcept {
67
		return needle != nullptr;
68 69
	}

70
	[[gnu::pure]]
71 72
	bool operator==(const char *haystack) const noexcept;

73
	[[gnu::pure]]
74 75 76 77
	bool IsIn(const char *haystack) const noexcept;
};

#endif