summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/src/base/SkStringView.h
blob: f8f83ae77e7f94b471cc147fbdb8d482fc39e157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * 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 <cstring>
#include <string_view>

namespace skstd {

// C++20 additions
inline constexpr bool starts_with(std::string_view str, std::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(std::string_view str, std::string_view::value_type c) {
    return !str.empty() && str.front() == c;
}

inline constexpr bool ends_with(std::string_view str, std::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(std::string_view str, std::string_view::value_type c) {
    return !str.empty() && str.back() == c;
}

// C++23 additions
inline constexpr bool contains(std::string_view str, std::string_view needle) {
    return str.find(needle) != std::string_view::npos;
}

inline constexpr bool contains(std::string_view str, std::string_view::value_type c) {
    return str.find(c) != std::string_view::npos;
}

}  // namespace skstd

#endif