summaryrefslogtreecommitdiffstats
path: root/src/base/string_util.cc
blob: 39e80f6c01f02a7323f5d9496799cf1dcd83b0dd (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
 * Copyright (c) 2019, Timothy Stack
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of Timothy Stack nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <algorithm>
#include <iterator>
#include <regex>
#include <sstream>

#include "string_util.hh"

#include "config.h"
#include "is_utf8.hh"
#include "lnav_log.hh"

void
scrub_to_utf8(char* buffer, size_t length)
{
    while (true) {
        auto frag = string_fragment::from_bytes(buffer, length);
        auto scan_res = is_utf8(frag);

        if (scan_res.is_valid()) {
            break;
        }
        for (size_t lpc = 0; lpc < scan_res.usr_faulty_bytes; lpc++) {
            buffer[scan_res.usr_valid_frag.sf_end + lpc] = '?';
        }
    }
}

void
quote_content(auto_buffer& buf, const string_fragment& sf, char quote_char)
{
    for (char ch : sf) {
        if (ch == quote_char) {
            buf.push_back('\\').push_back(ch);
            continue;
        }
        switch (ch) {
            case '\\':
                buf.push_back('\\').push_back('\\');
                break;
            case '\n':
                buf.push_back('\\').push_back('n');
                break;
            case '\t':
                buf.push_back('\\').push_back('t');
                break;
            case '\r':
                buf.push_back('\\').push_back('r');
                break;
            case '\a':
                buf.push_back('\\').push_back('a');
                break;
            case '\b':
                buf.push_back('\\').push_back('b');
                break;
            default:
                buf.push_back(ch);
                break;
        }
    }
}

size_t
unquote_content(char* dst, const char* str, size_t len, char quote_char)
{
    size_t index = 0;

    for (size_t lpc = 0; lpc < len; lpc++, index++) {
        dst[index] = str[lpc];
        if (str[lpc] == quote_char) {
            lpc += 1;
        } else if (str[lpc] == '\\' && (lpc + 1) < len) {
            switch (str[lpc + 1]) {
                case 'n':
                    dst[index] = '\n';
                    break;
                case 'r':
                    dst[index] = '\r';
                    break;
                case 't':
                    dst[index] = '\t';
                    break;
                default:
                    dst[index] = str[lpc + 1];
                    break;
            }
            lpc += 1;
        }
    }
    dst[index] = '\0';

    return index;
}

size_t
unquote(char* dst, const char* str, size_t len)
{
    if (str[0] == 'r' || str[0] == 'u') {
        str += 1;
        len -= 1;
    }
    char quote_char = str[0];

    require(str[0] == '\'' || str[0] == '"');

    return unquote_content(dst, &str[1], len - 2, quote_char);
}

size_t
unquote_w3c(char* dst, const char* str, size_t len)
{
    size_t index = 0;

    require(str[0] == '\'' || str[0] == '"');

    for (size_t lpc = 1; lpc < (len - 1); lpc++, index++) {
        dst[index] = str[lpc];
        if (str[lpc] == '"') {
            lpc += 1;
        }
    }
    dst[index] = '\0';

    return index;
}

void
truncate_to(std::string& str, size_t max_char_len)
{
    static const std::string ELLIPSIS = "\u22ef";

    if (str.length() < max_char_len) {
        return;
    }

    auto str_char_len_res = utf8_string_length(str);

    if (str_char_len_res.isErr()) {
        // XXX
        return;
    }

    auto str_char_len = str_char_len_res.unwrap();
    if (str_char_len <= max_char_len) {
        return;
    }

    if (max_char_len < 3) {
        str = ELLIPSIS;
        return;
    }

    auto chars_to_remove = (str_char_len - max_char_len) + 1;
    auto midpoint = str_char_len / 2;
    auto chars_to_keep_at_front = midpoint - (chars_to_remove / 2);
    auto bytes_to_keep_at_front
        = utf8_char_to_byte_index(str, chars_to_keep_at_front);
    auto remove_up_to_bytes = utf8_char_to_byte_index(
        str, chars_to_keep_at_front + chars_to_remove);
    auto bytes_to_remove = remove_up_to_bytes - bytes_to_keep_at_front;
    str.erase(bytes_to_keep_at_front, bytes_to_remove);
    str.insert(bytes_to_keep_at_front, ELLIPSIS);
}

bool
is_url(const std::string& fn)
{
    static const auto url_re = std::regex("^(file|https?|ftps?|scp|sftp):.*");

    return std::regex_match(fn, url_re);
}

size_t
last_word_str(char* str, size_t len, size_t max_len)
{
    if (len < max_len) {
        return len;
    }

    size_t last_start = 0;

    for (size_t index = 0; index < len; index++) {
        switch (str[index]) {
            case '.':
            case '-':
            case '/':
            case ':':
                last_start = index + 1;
                break;
        }
    }

    if (last_start == 0) {
        return len;
    }

    memmove(&str[0], &str[last_start], len - last_start);
    return len - last_start;
}

size_t
abbreviate_str(char* str, size_t len, size_t max_len)
{
    size_t last_start = 1;

    if (len < max_len) {
        return len;
    }

    for (size_t index = 0; index < len; index++) {
        switch (str[index]) {
            case '.':
            case '-':
            case '/':
            case ':':
                memmove(&str[last_start], &str[index], len - index);
                len -= (index - last_start);
                index = last_start + 1;
                last_start = index + 1;

                if (len < max_len) {
                    return len;
                }
                break;
        }
    }

    return len;
}

void
split_ws(const std::string& str, std::vector<std::string>& toks_out)
{
    std::stringstream ss(str);
    std::string buf;

    while (ss >> buf) {
        toks_out.push_back(buf);
    }
}

std::string
repeat(const std::string& input, size_t num)
{
    std::ostringstream os;
    std::fill_n(std::ostream_iterator<std::string>(os), num, input);
    return os.str();
}

std::string
center_str(const std::string& subject, size_t width)
{
    std::string retval = subject;

    truncate_to(retval, width);

    auto retval_length = utf8_string_length(retval).unwrapOr(retval.length());
    auto total_fill = width - retval_length;
    auto before = total_fill / 2;
    auto after = total_fill - before;

    retval.insert(0, before, ' ');
    retval.append(after, ' ');

    return retval;
}

bool
is_blank(const std::string& str)
{
    return std::all_of(
        str.begin(), str.end(), [](const auto ch) { return isspace(ch); });
}

std::string
scrub_ws(const char* in, ssize_t len)
{
    static const std::string TAB_SYMBOL = "\u21e5";
    static const std::string LF_SYMBOL = "\u240a";
    static const std::string CR_SYMBOL = "\u240d";

    std::string retval;

    for (size_t lpc = 0; (len == -1 && in[lpc]) || (len >= 0 && lpc < len);
         lpc++)
    {
        auto ch = in[lpc];

        switch (ch) {
            case '\t':
                retval.append(TAB_SYMBOL);
                break;
            case '\n':
                retval.append(LF_SYMBOL);
                break;
            case '\r':
                retval.append(CR_SYMBOL);
                break;
            default:
                retval.append(1, ch);
                break;
        }
    }

    return retval;
}

namespace fmt {
auto
formatter<lnav::tainted_string>::format(const lnav::tainted_string& ts,
                                        format_context& ctx)
    -> decltype(ctx.out()) const
{
    auto esc_res = fmt::v10::detail::find_escape(&(*ts.ts_str.begin()),
                                                 &(*ts.ts_str.end()));
    if (esc_res.end == nullptr) {
        return formatter<string_view>::format(ts.ts_str, ctx);
    }

    return format_to(ctx.out(), FMT_STRING("{:?}"), ts.ts_str);
}
}  // namespace fmt

namespace lnav {
namespace pcre2pp {

static bool
is_meta(char ch)
{
    switch (ch) {
        case '\\':
        case '^':
        case '$':
        case '.':
        case '[':
        case ']':
        case '(':
        case ')':
        case '*':
        case '+':
        case '?':
        case '{':
        case '}':
            return true;
        default:
            return false;
    }
}

static nonstd::optional<const char*>
char_escape_seq(char ch)
{
    switch (ch) {
        case '\t':
            return "\\t";
        case '\n':
            return "\\n";
    }

    return nonstd::nullopt;
}

std::string
quote(string_fragment str)
{
    std::string retval;

    while (true) {
        auto cp_pair_opt = str.consume_codepoint();
        if (!cp_pair_opt) {
            break;
        }

        auto cp_pair = cp_pair_opt.value();
        if ((cp_pair.first & ~0xff) == 0) {
            if (is_meta(cp_pair.first)) {
                retval.push_back('\\');
            } else {
                auto esc_seq = char_escape_seq(cp_pair.first);
                if (esc_seq) {
                    retval.append(esc_seq.value());
                    str = cp_pair_opt->second;
                    continue;
                }
            }
        }
        ww898::utf::utf8::write(cp_pair.first,
                                [&retval](char ch) { retval.push_back(ch); });
        str = cp_pair_opt->second;
    }

    return retval;
}

}  // namespace pcre2pp
}  // namespace lnav