Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkStringView.h
Go to the documentation of this file.
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
14namespace skstd {
15
16// C++20 additions
17inline 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
24inline constexpr bool starts_with(std::string_view str, std::string_view::value_type c) {
25 return !str.empty() && str.front() == c;
26}
27
28inline 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
36inline 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
41inline constexpr bool contains(std::string_view str, std::string_view needle) {
42 return str.find(needle) != std::string_view::npos;
43}
44
45inline 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
constexpr bool starts_with(std::string_view str, std::string_view prefix)
constexpr bool ends_with(std::string_view str, std::string_view suffix)
constexpr bool contains(std::string_view str, std::string_view needle)