aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/CStringView.h
blob: c1810b4f170a763a3e6e4af66d7dc3a3960b1f92 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <string>

namespace vcpkg
{
    template<class CharType>
    struct BasicCStringView
    {
        constexpr BasicCStringView() : cstr(nullptr) {}
        constexpr BasicCStringView(const CharType* cstr) : cstr(cstr) {}
        constexpr BasicCStringView(const BasicCStringView&) = default;
        BasicCStringView(const std::basic_string<CharType>& str) : cstr(str.c_str()) {}

        constexpr const CharType* c_str() const { return cstr; }

    private:
        const CharType* cstr;
    };

    namespace details
    {
        inline bool vcpkg_strcmp(const char* l, const char* r) { return strcmp(l, r) == 0; }
        inline bool vcpkg_strcmp(const wchar_t* l, const wchar_t* r) { return wcscmp(l, r) == 0; }
    }

    template<class CharType>
    bool operator==(const BasicCStringView<CharType>& l, const BasicCStringView<CharType>& r)
    {
        return details::vcpkg_strcmp(l.c_str(), r.c_str());
    }

    template<class CharType>
    bool operator==(const CharType* l, const BasicCStringView<CharType>& r)
    {
        return details::vcpkg_strcmp(l, r.c_str());
    }

    template<class CharType>
    bool operator==(const BasicCStringView<CharType>& r, const CharType* l)
    {
        return details::vcpkg_strcmp(l, r.c_str());
    }

    template<class CharType>
    bool operator==(const std::basic_string<CharType>& l, const BasicCStringView<CharType>& r)
    {
        return l == r.c_str();
    }

    template<class CharType>
    bool operator==(const BasicCStringView<CharType>& r, const std::basic_string<CharType>& l)
    {
        return l == r.c_str();
    }

    // notequals
    template<class CharType>
    bool operator!=(const BasicCStringView<CharType>& l, const BasicCStringView<CharType>& r)
    {
        return !details::vcpkg_strcmp(l.c_str(), r.c_str());
    }

    template<class CharType>
    bool operator!=(const CharType* l, const BasicCStringView<CharType>& r)
    {
        return !details::vcpkg_strcmp(l, r.c_str());
    }

    template<class CharType>
    bool operator!=(const BasicCStringView<CharType>& r, const CharType* l)
    {
        return !details::vcpkg_strcmp(l, r.c_str());
    }

    template<class CharType>
    bool operator!=(const BasicCStringView<CharType>& r, const std::basic_string<CharType>& l)
    {
        return l != r.c_str();
    }

    template<class CharType>
    bool operator!=(const std::basic_string<CharType>& l, const BasicCStringView<CharType>& r)
    {
        return l != r.c_str();
    }

    using CStringView = BasicCStringView<char>;
    using CWStringView = BasicCStringView<wchar_t>;

    inline const char* to_printf_arg(const CStringView string_view) { return string_view.c_str(); }

    inline const wchar_t* to_wprintf_arg(const CWStringView string_view) { return string_view.c_str(); }

    static_assert(sizeof(CStringView) == sizeof(void*), "CStringView must be a simple wrapper around char*");
    static_assert(sizeof(CWStringView) == sizeof(void*), "CWStringView must be a simple wrapper around wchar_t*");
}