skia2/include/core/SkStringView.h
John Stiles 64c971350e Use native std::string_view.
We also used some string_view functionality from C++20/23. These have
been replaced with free functions with the same name.

Change-Id: I3bf40f99aeb500495f344fd8c6872619267d42be
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/500897
Reviewed-by: Herb Derby <herb@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
2022-01-27 20:26:33 +00:00

50 lines
1.3 KiB
C++

/*
* 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
#include <string.h>
#include <string_view>
namespace skstd {
using string_view = std::string_view;
// C++20 additions
inline constexpr bool starts_with(string_view str, string_view prefix) {
if (prefix.length() > str.length()) {
return false;
}
return prefix.length() == 0 || !memcmp(str.data(), prefix.data(), prefix.length());
}
inline constexpr bool starts_with(string_view str, string_view::value_type c) {
return !str.empty() && str.front() == c;
}
inline constexpr bool ends_with(string_view str, string_view suffix) {
if (suffix.length() > str.length()) {
return false;
}
return suffix.length() == 0 || !memcmp(str.data() + str.length() - suffix.length(),
suffix.data(), suffix.length());
}
inline constexpr bool ends_with(string_view str, string_view::value_type c) {
return !str.empty() && str.back() == c;
}
// C++23 additions
inline constexpr bool contains(string_view str, string_view needle) {
return str.find(needle) != string_view::npos;
}
} // namespace skstd
#endif