summaryrefslogtreecommitdiff
path: root/zen/string_traits.h
blob: 8187126dc2095afc0b183ddaf12c2c2435417b0d (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#ifndef STRING_TRAITS_H_813274321443234
#define STRING_TRAITS_H_813274321443234

#include <cstring> //strlen
#include "type_traits.h"


//uniform access to string-like types, both classes and character arrays
namespace zen
{
/*
IsStringLikeV<>:
    IsStringLikeV<const wchar_t*> //equals "true"
    IsStringLikeV<const int*>     //equals "false"

GetCharTypeT<>:
    GetCharTypeT<std::wstring> //equals wchar_t
    GetCharTypeT<wchar_t[5]>   //equals wchar_t

strLength():
    strLength(str);   //equals str.length()
    strLength(array); //equals cStringLength(array)

strBegin():         -> not null-terminated! -> may be nullptr if length is 0!
    std::wstring str(L"dummy");
    char array[] = "dummy";
    strBegin(str);   //returns str.c_str()
    strBegin(array); //returns array
*/


//reference a sub-string for consumption by zen string_tools
template <class Char>
class StringRef
{
public:
    template <class Iterator>
    StringRef(Iterator first, Iterator last) : len_(last - first), str_(first != last ? &*first : nullptr) {}
    //StringRef(const Char* str, size_t len) : str_(str), len_(len) {} -> needless constraint! Char* not available for empty range!

    Char*  data  () const { return str_; } //1. no null-termination! 2. may be nullptr!
    size_t length() const { return len_; }

private:
    const size_t len_;
    Char* str_;
};












//---------------------- implementation ----------------------
namespace impl
{
template<class S, class Char> //test if result of S::c_str() can convert to const Char*
class HasConversion
{
    using Yes = char[1];
    using No  = char[2];

    static Yes& hasConversion(const Char*);
    static  No& hasConversion(...);

public:
    enum { value = sizeof(hasConversion(std::declval<S>().c_str())) == sizeof(Yes) };
};


template <class S, bool isStringClass>  struct GetCharTypeImpl { using Type = void; };

template <class S>
struct GetCharTypeImpl<S, true>
{
    using Type = std::conditional_t<HasConversion<S, wchar_t>::value, wchar_t,
          /**/   std::conditional_t<HasConversion<S, char   >::value, char, void>>;

    //using Type = typename S::value_type;
    /*DON'T use S::value_type:
        1. support Glib::ustring: value_type is "unsigned int" but c_str() returns "const char*"
        2. wxString, wxWidgets v2.9, has some questionable string design: wxString::c_str() returns a proxy (wxCStrData) which
           is implicitly convertible to *both* "const char*" and "const wchar_t*" while wxString::value_type is a wrapper around an unsigned int
    */
};

template <> struct GetCharTypeImpl<char,    false> { using Type = char; };
template <> struct GetCharTypeImpl<wchar_t, false> { using Type = wchar_t; };

template <> struct GetCharTypeImpl<StringRef<char         >, false> { using Type = char; };
template <> struct GetCharTypeImpl<StringRef<wchar_t      >, false> { using Type = wchar_t; };
template <> struct GetCharTypeImpl<StringRef<const char   >, false> { using Type = char; };
template <> struct GetCharTypeImpl<StringRef<const wchar_t>, false> { using Type = wchar_t; };


ZEN_INIT_DETECT_MEMBER_TYPE(value_type);
ZEN_INIT_DETECT_MEMBER(c_str);  //we don't know the exact declaration of the member attribute and it may be in a base class!
ZEN_INIT_DETECT_MEMBER(length); //

template <class S>
class StringTraits
{
    using CleanType       = std::remove_cv_t<std::remove_reference_t<S>>; //std::remove_cvref requires C++20
    using NonArrayType    = std::remove_extent_t <CleanType>;
    using NonPtrType      = std::remove_pointer_t<NonArrayType>;
    using UndecoratedType = std::remove_cv_t     <NonPtrType>; //handle "const char* const"

public:
    enum
    {
        isStringClass = HasMemberType_value_type<CleanType>::value &&
                        HasMember_c_str         <CleanType>::value &&
                        HasMember_length        <CleanType>::value
    };

    using CharType = typename GetCharTypeImpl<UndecoratedType, isStringClass>::Type;

    enum
    {
        isStringLike = std::is_same_v<CharType, char> ||
        std::is_same_v<CharType, wchar_t>
    };
};
}

template <class T>
struct IsStringLike : std::bool_constant<impl::StringTraits<T>::isStringLike> {};

template <class T>
struct GetCharType { using Type = typename impl::StringTraits<T>::CharType; };


//template alias helpers:
template<class T>
constexpr bool IsStringLikeV = IsStringLike<T>::value;

template<class T>
using GetCharTypeT = typename GetCharType<T>::Type;


namespace impl
{
//strlen/wcslen are vectorized since VS14 CTP3
inline size_t cStringLength(const char*    str) { return std::strlen(str); }
inline size_t cStringLength(const wchar_t* str) { return std::wcslen(str); }

//no significant perf difference for "comparison" test case between cStringLength/wcslen:
#if 0
template <class C> inline
size_t cStringLength(const C* str)
{
    static_assert(std::is_same_v<C, char> || std::is_same_v<C, wchar_t>);
    size_t len = 0;
    while (*str++ != 0)
        ++len;
    return len;
}
#endif

template <class S, typename = std::enable_if_t<StringTraits<S>::isStringClass>> inline
const GetCharTypeT<S>* strBegin(const S& str) //SFINAE: T must be a "string"
{
    return str.c_str();
}

inline const char*    strBegin(const char*    str) { return str; }
inline const wchar_t* strBegin(const wchar_t* str) { return str; }
inline const char*    strBegin(const char&    ch)  { return &ch; }
inline const wchar_t* strBegin(const wchar_t& ch)  { return &ch; }

inline const char*    strBegin(const StringRef<char         >& ref) { return ref.data(); }
inline const wchar_t* strBegin(const StringRef<wchar_t      >& ref) { return ref.data(); }
inline const char*    strBegin(const StringRef<const char   >& ref) { return ref.data(); }
inline const wchar_t* strBegin(const StringRef<const wchar_t>& ref) { return ref.data(); }


template <class S, typename = std::enable_if_t<StringTraits<S>::isStringClass>> inline
size_t strLength(const S& str) //SFINAE: T must be a "string"
{
    return str.length();
}

inline size_t strLength(const char*    str) { return cStringLength(str); }
inline size_t strLength(const wchar_t* str) { return cStringLength(str); }
inline size_t strLength(char)               { return 1; }
inline size_t strLength(wchar_t)            { return 1; }

inline size_t strLength(const StringRef<char         >& ref) { return ref.length(); }
inline size_t strLength(const StringRef<wchar_t      >& ref) { return ref.length(); }
inline size_t strLength(const StringRef<const char   >& ref) { return ref.length(); }
inline size_t strLength(const StringRef<const wchar_t>& ref) { return ref.length(); }
}


template <class S> inline
auto strBegin(S&& str) -> const GetCharTypeT<S>*
{
    static_assert(IsStringLikeV<S>);
    return impl::strBegin(std::forward<S>(str));
}


template <class S> inline
size_t strLength(S&& str)
{
    static_assert(IsStringLikeV<S>);
    return impl::strLength(std::forward<S>(str));
}
}

#endif //STRING_TRAITS_H_813274321443234
bgstack15