1 /*
2  * Copyright 2013-2021 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef STRING_COMPARE_HXX
31 #define STRING_COMPARE_HXX
32 
33 #include "StringView.hxx"
34 #include "StringAPI.hxx"
35 
36 #ifdef _UNICODE
37 #include "WStringCompare.hxx"
38 #endif
39 
40 #include <string_view>
41 
42 [[gnu::pure]] [[gnu::nonnull]]
43 static inline bool
StringIsEmpty(const char * string)44 StringIsEmpty(const char *string) noexcept
45 {
46 	return *string == 0;
47 }
48 
49 [[gnu::pure]]
50 static inline bool
StringIsEqual(std::string_view a,std::string_view b)51 StringIsEqual(std::string_view a, std::string_view b) noexcept
52 {
53 	return a.size() == b.size() &&
54 		StringIsEqual(a.data(), b.data(), b.size());
55 }
56 
57 [[gnu::pure]]
58 static inline bool
StringIsEqualIgnoreCase(std::string_view a,std::string_view b)59 StringIsEqualIgnoreCase(std::string_view a, std::string_view b) noexcept
60 {
61 	return a.size() == b.size() &&
62 		StringIsEqualIgnoreCase(a.data(), b.data(), b.size());
63 }
64 
65 [[gnu::pure]] [[gnu::nonnull]]
66 static inline bool
StringStartsWith(const char * haystack,StringView needle)67 StringStartsWith(const char *haystack, StringView needle) noexcept
68 {
69 	return StringIsEqual(haystack, needle.data, needle.size);
70 }
71 
72 [[gnu::pure]] [[gnu::nonnull]]
73 bool
74 StringEndsWith(const char *haystack, const char *needle) noexcept;
75 
76 [[gnu::pure]] [[gnu::nonnull]]
77 bool
78 StringEndsWithIgnoreCase(const char *haystack, const char *needle) noexcept;
79 
80 /**
81  * Returns the portion of the string after a prefix.  If the string
82  * does not begin with the specified prefix, this function returns
83  * nullptr.
84  */
85 [[gnu::pure]] [[gnu::nonnull]]
86 static inline const char *
StringAfterPrefix(const char * haystack,StringView needle)87 StringAfterPrefix(const char *haystack, StringView needle) noexcept
88 {
89 	return StringStartsWith(haystack, needle)
90 		? haystack + needle.size
91 		: nullptr;
92 }
93 
94 [[gnu::pure]] [[gnu::nonnull]]
95 static inline bool
StringStartsWithIgnoreCase(const char * haystack,StringView needle)96 StringStartsWithIgnoreCase(const char *haystack, StringView needle) noexcept
97 {
98 	return StringIsEqualIgnoreCase(haystack, needle.data, needle.size);
99 }
100 
101 [[gnu::pure]]
102 static inline bool
StringStartsWithIgnoreCase(StringView haystack,StringView needle)103 StringStartsWithIgnoreCase(StringView haystack, StringView needle) noexcept
104 {
105 	return haystack.size >= needle.size &&
106 		StringIsEqualIgnoreCase(haystack.data, needle.data, needle.size);
107 }
108 
109 /**
110  * Returns the portion of the string after a prefix.  If the string
111  * does not begin with the specified prefix, this function returns
112  * nullptr.
113  * This function is case-independent.
114  */
115 [[gnu::pure]] [[gnu::nonnull]]
116 static inline const char *
StringAfterPrefixIgnoreCase(const char * haystack,StringView needle)117 StringAfterPrefixIgnoreCase(const char *haystack, StringView needle) noexcept
118 {
119 	return StringStartsWithIgnoreCase(haystack, needle)
120 		? haystack + needle.size
121 		: nullptr;
122 }
123 
124 [[gnu::pure]]
125 static inline StringView
StringAfterPrefixIgnoreCase(StringView haystack,StringView needle)126 StringAfterPrefixIgnoreCase(StringView haystack,
127 			    StringView needle) noexcept
128 {
129 	return StringStartsWithIgnoreCase(haystack, needle)
130 		? haystack.substr(needle.size)
131 		: nullptr;
132 }
133 
134 /**
135  * Check if the given string ends with the specified suffix.  If yes,
136  * returns the position of the suffix, and nullptr otherwise.
137  */
138 [[gnu::pure]] [[gnu::nonnull]]
139 const char *
140 FindStringSuffix(const char *p, const char *suffix) noexcept;
141 
142 #endif
143