summaryrefslogtreecommitdiff
path: root/src/string.hh
blob: fa627b90131ed6e00591e55fe10c8fd2eff4921b (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#ifndef string_hh_INCLUDED
#define string_hh_INCLUDED

#include "memoryview.hh"
#include "units.hh"
#include "utf8.hh"

#include <string>
#include <boost/regex.hpp>

namespace Kakoune
{

using Regex = boost::regex;

class StringView;

class String : public std::string
{
public:
    String() {}
    String(const char* content) : std::string(content) {}
    String(std::string content) : std::string(std::move(content)) {}
    explicit String(char content, CharCount count = 1) : std::string((size_t)(int)count, content) {}
    explicit String(Codepoint cp, CharCount count = 1)
    {
        while (count-- > 0)
            utf8::dump(back_inserter(*this), cp);
    }
    template<typename Iterator>
    String(Iterator begin, Iterator end) : std::string(begin, end) {}

    std::string& stdstr() { return *this; }
    const std::string& stdstr() const { return *this; }

    char      operator[](ByteCount pos) const { return std::string::operator[]((int)pos); }
    char&     operator[](ByteCount pos) { return std::string::operator[]((int)pos); }
    ByteCount length() const { return ByteCount{(int)std::string::length()}; }
    CharCount char_length() const { return utf8::distance(begin(), end()); }
    ByteCount byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); }
    CharCount char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); }

    String  operator+(const String& other) const { return String{stdstr() + other.stdstr()}; }
    String& operator+=(const String& other) { std::string::operator+=(other); return *this; }
    String  operator+(const char* other) const { return String{stdstr() + other}; }
    String& operator+=(const char* other) { std::string::operator+=(other); return *this; }
    String  operator+(char other) const { return String{stdstr() + other}; }
    String& operator+=(char other) { std::string::operator+=(other); return *this; }
    String  operator+(Codepoint cp) const { String res = *this; utf8::dump(back_inserter(res), cp); return res; }
    String& operator+=(Codepoint cp) { utf8::dump(back_inserter(*this), cp); return *this; }

    StringView substr(ByteCount pos, ByteCount length = INT_MAX) const;
    StringView substr(CharCount pos, CharCount length = INT_MAX) const;
};

class StringView
{
public:
    constexpr StringView() : m_data{nullptr}, m_length{0} {}
    constexpr StringView(const char* data, ByteCount length)
        : m_data{data}, m_length{length} {}
    StringView(const char* data) : m_data{data}, m_length{(int)strlen(data)} {}
    constexpr StringView(const char* begin, const char* end) : m_data{begin}, m_length{(int)(end - begin)} {}
    StringView(const String& str) : m_data{str.data()}, m_length{str.length()} {}

    bool operator==(StringView other) const;
    bool operator!=(StringView other) const;

    const char* data() const { return m_data; }

    using iterator = const char*;
    using reverse_iterator = std::reverse_iterator<const char*>;

    iterator begin() const { return m_data; }
    iterator end() const { return m_data + (int)m_length; }

    reverse_iterator rbegin() const { return reverse_iterator{m_data + (int)m_length}; }
    reverse_iterator rend() const { return reverse_iterator{m_data}; }

    char front() const { return *m_data; }
    char back() const { return m_data[(int)m_length - 1]; }

    char operator[](ByteCount pos) const { return m_data[(int)pos]; }

    ByteCount length() const { return m_length; }
    CharCount char_length() const { return utf8::distance(begin(), end()); }
    bool empty() { return m_length == 0_byte; }

    ByteCount byte_count_to(CharCount count) const;
    CharCount char_count_to(ByteCount count) const;

    StringView substr(ByteCount from, ByteCount length = INT_MAX) const;
    StringView substr(CharCount from, CharCount length = INT_MAX) const;

    String str() const { return String{begin(), end()}; }

    operator String() const { return str(); } // to remove

    struct ZeroTerminatedString
    {
        ZeroTerminatedString(const char* begin, const char* end)
        {
            if (*end == '\0')
                unowned = begin;
            else
                owned = std::string(begin, end);
        }
        operator const char*() const { return unowned ? unowned : owned.c_str(); }

    private:
        std::string owned;
        const char* unowned = nullptr;

    };
    ZeroTerminatedString zstr() const { return ZeroTerminatedString{begin(), end()}; }

private:
    const char* m_data;
    ByteCount m_length;
};

inline bool StringView::operator==(StringView other) const
{
    return m_length == other.m_length and memcmp(m_data, other.m_data, (int)m_length) == 0;
}

inline bool StringView::operator!=(StringView other) const
{
    return !this->operator==(other);
}

inline bool operator==(const char* lhs, StringView rhs)
{
    return StringView{lhs} == rhs;
}

inline bool operator!=(const char* lhs, StringView rhs)
{
    return StringView{lhs} != rhs;
}

inline bool operator==(const std::string& lhs, StringView rhs)
{
    return StringView{lhs} == rhs;
}

inline bool operator!=(const std::string& lhs, StringView rhs)
{
    return StringView{lhs} != rhs;
}

inline ByteCount StringView::byte_count_to(CharCount count) const
{
    return utf8::advance(begin(), end(), (int)count) - begin();
}
inline CharCount StringView::char_count_to(ByteCount count) const
{
    return utf8::distance(begin(), begin() + (int)count);
}

inline StringView StringView::substr(ByteCount from, ByteCount length) const
{
    if (length < 0)
        length = INT_MAX;
    return StringView{ m_data + (int)from, std::min(m_length - from, length) };
}

inline StringView StringView::substr(CharCount from, CharCount length) const
{
    if (length < 0)
        length = INT_MAX;
    auto beg = utf8::advance(begin(), end(), (int)from);
    return StringView{ beg, utf8::advance(beg, end(), length) };
}

inline StringView String::substr(ByteCount pos, ByteCount length) const
{
    return StringView{*this}.substr(pos, length);
}

inline StringView String::substr(CharCount pos, CharCount length) const
{
    return StringView{*this}.substr(pos, length);
}

inline const char* begin(StringView str)
{
    return str.begin();
}

inline const char* end(StringView str)
{
    return str.end();
}

inline String operator+(const char* lhs, const String& rhs)
{
    return String(lhs) + rhs;
}

inline String operator+(const std::string& lhs, const String& rhs)
{
    return String(lhs) + rhs;
}

inline String operator+(const String& lhs, const std::string& rhs)
{
    return lhs + String(rhs);
}

inline String& operator+=(String& lhs, StringView rhs)
{
    lhs.append(rhs.data(), (size_t)(int)rhs.length());
    return lhs;
}

inline String operator+(const char* lhs, StringView rhs)
{
    String res = lhs;
    res += rhs;
    return res;
}

inline String operator+(const String& lhs, StringView rhs)
{
    String res = lhs;
    res += rhs;
    return res;
}

inline String operator+(StringView lhs, const String& rhs)
{
    String res{lhs.begin(), lhs.end()};
    res.append(rhs);
    return res;
}

inline String operator+(StringView lhs, const char* rhs)
{
    String res{lhs.begin(), lhs.end()};
    res.append(rhs);
    return res;
}

inline String operator+(char lhs, const String& rhs)
{
    return String(lhs) + rhs;
}

inline String operator+(Codepoint lhs, const String& rhs)
{
    return String(lhs) + rhs;
}

inline String operator+(StringView lhs, StringView rhs)
{
    String res{lhs.begin(), lhs.end()};
    res += rhs;
    return res;
}

std::vector<String> split(const String& str, char separator, char escape = 0);
String escape(const String& str, char character, char escape);

inline String operator"" _str(const char* str, size_t)
{
    return String(str);
}

inline String codepoint_to_str(Codepoint cp)
{
    std::string str;
    utf8::dump(back_inserter(str), cp);
    return String(str);
}

String option_to_string(const Regex& re);
void option_from_string(const String& str, Regex& re);

int str_to_int(const String& str);

String to_string(int val);

template<typename RealType, typename ValueType>
String to_string(const StronglyTypedNumber<RealType, ValueType>& val)
{
    return to_string((ValueType)val);
}

bool prefix_match(StringView str, StringView prefix);
bool subsequence_match(StringView str, StringView subseq);

String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0);

}

namespace std
{
    template<>
    struct hash<Kakoune::String> : hash<std::string>
    {
        size_t operator()(const Kakoune::String& str) const
        {
            return hash<std::string>::operator()(str);
        }
    };
}

#endif // string_hh_INCLUDED