| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef SkStringView_DEFINED |
| 9 |
#define SkStringView_DEFINED |
| 10 |
|
| 11 |
#include <cstring> |
| 12 |
#include <string_view> |
| 13 |
|
| 14 |
namespace skstd { |
| 15 |
|
| 16 |
// C++20 additions |
| 17 |
inline constexpr bool starts_with(std::string_view str, std::string_view prefix) { |
| 18 |
if (prefix.length() > str.length()) { |
| 19 |
return false; |
| 20 |
} |
| 21 |
return prefix.length() == 0 || !memcmp(str.data(), prefix.data(), prefix.length()); |
| 22 |
} |
| 23 |
|
| 24 |
inline constexpr bool starts_with(std::string_view str, std::string_view::value_type c) { |
| 25 |
return !str.empty() && str.front() == c; |
| 26 |
} |
| 27 |
|
| 28 |
inline constexpr bool ends_with(std::string_view str, std::string_view suffix) { |
| 29 |
if (suffix.length() > str.length()) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
return suffix.length() == 0 || !memcmp(str.data() + str.length() - suffix.length(), |
| 33 |
suffix.data(), suffix.length()); |
| 34 |
} |
| 35 |
|
| 36 |
inline constexpr bool ends_with(std::string_view str, std::string_view::value_type c) { |
| 37 |
return !str.empty() && str.back() == c; |
| 38 |
} |
| 39 |
|
| 40 |
// C++23 additions |
| 41 |
inline constexpr bool contains(std::string_view str, std::string_view needle) { |
| 42 |
return str.find(needle) != std::string_view::npos; |
| 43 |
} |
| 44 |
|
| 45 |
inline constexpr bool contains(std::string_view str, std::string_view::value_type c) { |
| 46 |
return str.find(c) != std::string_view::npos; |
| 47 |
} |
| 48 |
|
| 49 |
} // namespace skstd |
| 50 |
|
| 51 |
#endif |