#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library 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 IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_TEXT_STRINGMATCHER_H #define NUCLEX_SUPPORT_TEXT_STRINGMATCHER_H #include "Nuclex/Support/Config.h" #include // for std::string #include //for std::hash, std::equal_to, std::less #include // for std::uint32_t, std::uint64_t namespace Nuclex { namespace Support { namespace Text { // ------------------------------------------------------------------------------------------- // /// Compares strings using different matching algorithms class NUCLEX_SUPPORT_TYPE StringMatcher { /// Compares two UTF-8 strings for equality, optionally ignoring case /// String that will be compared on the left side /// String that will be compared on the right side /// Whether the comparison will be case sensitive /// True if the two strings are equal, false otherwise /// /// This method is ideal for one-off comparisons. If you have to compare one string /// against multiple strings or want to create a case-insensitive string map, /// consider using the method. /// public: NUCLEX_SUPPORT_API static bool AreEqual( const std::string &left, const std::string &right, bool caseSensitive = false ); /// Checks whether one UTF-8 string contains another UTF-8 string /// /// String that will be scanned for instances of another string /// /// String which might appear inside the other string /// Whether the comparison will be case sensitive /// /// True if the 'needle' string appears at least once in the 'haystack' string /// public: NUCLEX_SUPPORT_API static bool Contains( const std::string &haystack, const std::string &needle, bool caseSensitive = false ); /// Checks whether one UTF-8 string starts with another UTF-8 string /// /// String whose beginning will be compared with the searched-for string /// /// String with which the checked string must begin /// Whether the comparison will be case sensitive /// /// True if the 'haystack' string starts with the 'needle' string /// public: NUCLEX_SUPPORT_API static bool StartsWith( const std::string &text, const std::string &beginning, bool caseSensitive = false ); /// Checks whether a UTF-8 string matches a wildcard /// Text that will be matched against the wildcard /// Wildcard against which the text will be matched /// Whether the comparison will be case sensitive /// True if the specified text matches the wildcard /// /// Wildcards refer to the simple placeholder symbols employed by many shells, /// where a '?' acts as a stand-in for one UTF-8 character and a '*' acts as /// a stand-in for zero or more UTF-8 characters. For example "*l?o*" /// would match "Hello" and "lion" but not "glow". /// public: NUCLEX_SUPPORT_API static bool FitsWildcard( const std::string &text, const std::string &wildcard, bool caseSensitive = false ); }; // ------------------------------------------------------------------------------------------- // /// Case-insensitive UTF-8 version of std::hash<std::string> /// /// You can use this to construct a case-insensitive std::unordered_map. /// struct NUCLEX_SUPPORT_TYPE CaseInsensitiveUtf8Hash { /// Calculates a case-insensitive hash of an UTF-8 string /// UTF-8 string of which a hash value will be calculated /// The case-insensitive hash value of the provided string public: NUCLEX_SUPPORT_API std::size_t operator()( const std::string &text ) const noexcept; }; // ------------------------------------------------------------------------------------------- // /// Case-insensitive UTF-8 version of std::equal_to<std::string> /// /// You can use this to construct a case-insensitive std::unordered_map. /// struct NUCLEX_SUPPORT_TYPE CaseInsensitiveUtf8EqualTo { /// Checks if two UTF-8 strings are equal, ignoring case /// First UTF-8 string to compare /// Other UTF-8 string to compare /// True if both UTF-8 strings have equal contents public: NUCLEX_SUPPORT_API bool operator()( const std::string &left, const std::string &right ) const noexcept; }; // ------------------------------------------------------------------------------------------- // /// Case-insensitive UTF-8 version of std::less<std::string> /// /// You can use this to construct a case-insensitive std::map. /// struct NUCLEX_SUPPORT_TYPE CaseInsensitiveUtf8Less { /// Checks if the first UTF-8 string is 'less' than the second /// First UTF-8 string to compare /// Other UTF-8 string to compare /// True if the first UTF-8 string is 'less', ignoring case public: NUCLEX_SUPPORT_API bool operator()( const std::string &left, const std::string &right ) const noexcept; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Text #endif // NUCLEX_SUPPORT_TEXT_STRINGMATCHER_H