aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/vcpkg_Strings.h
blob: d152eccf5f748f7c29c0ed28c95b1f34d7463118 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma once

#include <vector>
#include "CStringView.h"

namespace vcpkg::Strings::details
{
    template<class T>
    auto to_printf_arg(const T& t) -> decltype(t.to_string())
    {
        return t.to_string();
    }

    inline const char* to_printf_arg(const std::string& s)
    {
        return s.c_str();
    }

    inline const char* to_printf_arg(const char* s)
    {
        return s;
    }

    inline int to_printf_arg(const int s)
    {
        return s;
    }

    inline long long to_printf_arg(const long long s)
    {
        return s;
    }

    inline double to_printf_arg(const double s)
    {
        return s;
    }

    inline size_t to_printf_arg(const size_t s)
    {
        return s;
    }

    std::string format_internal(const char* fmtstr, ...);

    inline const wchar_t* to_wprintf_arg(const std::wstring& s)
    {
        return s.c_str();
    }

    inline const wchar_t* to_wprintf_arg(const wchar_t* s)
    {
        return s;
    }

    std::wstring wformat_internal(const wchar_t* fmtstr, ...);
}

namespace vcpkg::Strings
{
    template <class...Args>
    std::string format(const char* fmtstr, const Args&...args)
    {
        using vcpkg::Strings::details::to_printf_arg;
        return details::format_internal(fmtstr, to_printf_arg(to_printf_arg(args))...);
    }

    template <class...Args>
    std::wstring wformat(const wchar_t* fmtstr, const Args&...args)
    {
        using vcpkg::Strings::details::to_wprintf_arg;
        return details::wformat_internal(fmtstr, to_wprintf_arg(to_wprintf_arg(args))...);
    }

    std::wstring utf8_to_utf16(const CStringView s);

    std::string utf16_to_utf8(const CWStringView w);

    std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);

    std::string ascii_to_lowercase(const std::string& input);

    template <class T, class Transformer, class CharType>
    std::basic_string<CharType> join(
        const CharType* delimiter,
        const std::vector<T>& v,
        Transformer transformer)
    {
        if (v.empty())
        {
            return std::basic_string<CharType>();
        }

        std::basic_string<CharType> output;
        size_t size = v.size();

        output.append(transformer(v.at(0)));

        for (size_t i = 1; i < size; ++i)
        {
            output.append(delimiter);
            output.append(transformer(v.at(i)));
        }

        return output;
    }
    template <class T, class CharType>
    std::basic_string<CharType> join(
        const CharType* delimiter,
        const std::vector<T>& v)
    {
        return join(delimiter, v, [](const T& x) -> const T&{ return x; });
    }

    void trim(std::string* s);

    std::string trimmed(const std::string& s);

    void trim_all_and_remove_whitespace_strings(std::vector<std::string>* strings);

    std::vector<std::string> split(const std::string& s, const std::string& delimiter);

    template<class T>
    std::string serialize(const T& t)
    {
        std::string ret;
        serialize(t, ret);
        return ret;
    }
}