aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/util/stringutil.h
blob: 27c5e16c97b59c8d714e2bc3f09b9cd8588e9d3b (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * \class document::StringUtil
 * \ingroup util
 *
 * \brief Utility class for string related functionality.
 */

#pragma once

#include <vespa/vespalib/stllike/string.h>

namespace document {

class StringUtil {
public:
    /**
     * Escapes a string, turning backslash or unprintable characters into
     * \\\\ \\n \\t \\f \\r or \\x##.
     *
     * The delimiter can be set to also escape an otherwise printable character that you don't
     * want the string to contain. (Useful to escape content to use in a context where you want
     * to use a given delimiter)
     */
    static vespalib::string escape(const vespalib::string & source, char delimiter = '\0') {
        vespalib::string escaped;
        return escape(source, escaped, delimiter);
    }
    /**
    */
    static const vespalib::string & escape(const vespalib::string & source, vespalib::string & dst,
                                      char delimiter = '\0');

    /**
     * Unescape a string, replacing \\\\ \\n \\t \\f \\r or \\x## with their
     * ascii value counterparts.
     */
    static vespalib::string unescape(vespalib::stringref source);

    /**
     * Print whatever source points to in a readable format.
     *
     * @param output Stream to print to
     * @param source Pointer to what should be printed
     * @param size The number of bytes to print.
     * @param columnwidth Max number of bytes to print per line.
     * @param inlinePrintables If true, print printable characters in the list
     *                         instead of ASCII values. If false, print ASCII
     *                         values for all bytes, and add printables output
     *                         only to the right column.
     * @param indent Whitespace to put after each newline in output
     */
    static void printAsHex(std::ostream& output,
                           const void* source, unsigned int size,
                           unsigned int columnwidth = 16,
                           bool inlinePrintables = false,
                           const std::string& indent = "");
};

} // document