2021-06-07 20:09:59 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Google LLC.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkStringView_DEFINED
|
|
|
|
#define SkStringView_DEFINED
|
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <string_view>
|
2021-06-07 20:09:59 +00:00
|
|
|
|
|
|
|
namespace skstd {
|
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
using string_view = std::string_view;
|
2021-06-07 20:09:59 +00:00
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
// C++20 additions
|
|
|
|
inline constexpr bool starts_with(string_view str, string_view prefix) {
|
|
|
|
if (prefix.length() > str.length()) {
|
|
|
|
return false;
|
2021-09-07 17:49:07 +00:00
|
|
|
}
|
2022-01-27 02:37:22 +00:00
|
|
|
return prefix.length() == 0 || !memcmp(str.data(), prefix.data(), prefix.length());
|
|
|
|
}
|
2021-09-07 17:49:07 +00:00
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
inline constexpr bool starts_with(string_view str, string_view::value_type c) {
|
|
|
|
return !str.empty() && str.front() == c;
|
|
|
|
}
|
2021-09-07 17:49:07 +00:00
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
inline constexpr bool ends_with(string_view str, string_view suffix) {
|
|
|
|
if (suffix.length() > str.length()) {
|
|
|
|
return false;
|
2021-09-01 18:57:44 +00:00
|
|
|
}
|
2022-01-27 02:37:22 +00:00
|
|
|
return suffix.length() == 0 || !memcmp(str.data() + str.length() - suffix.length(),
|
|
|
|
suffix.data(), suffix.length());
|
|
|
|
}
|
2021-09-01 18:57:44 +00:00
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
inline constexpr bool ends_with(string_view str, string_view::value_type c) {
|
|
|
|
return !str.empty() && str.back() == c;
|
|
|
|
}
|
2021-06-07 20:09:59 +00:00
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
// C++23 additions
|
|
|
|
inline constexpr bool contains(string_view str, string_view needle) {
|
|
|
|
return str.find(needle) != string_view::npos;
|
|
|
|
}
|
2021-06-07 20:09:59 +00:00
|
|
|
|
2022-01-27 02:37:22 +00:00
|
|
|
} // namespace skstd
|
2021-06-10 15:21:59 +00:00
|
|
|
|
2021-06-07 20:09:59 +00:00
|
|
|
#endif
|