1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/common/string_util.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/constants.hpp"
12#include <stdarg.h> // for va_list
13
14namespace duckdb {
15/**
16 * String Utility Functions
17 * Note that these are not the most efficient implementations (i.e., they copy
18 * memory) and therefore they should only be used for debug messages and other
19 * such things.
20 */
21class StringUtil {
22public:
23 //! Returns true if the needle string exists in the haystack
24 static bool Contains(const string &haystack, const string &needle);
25
26 //! Returns true if the target string starts with the given prefix
27 static bool StartsWith(const string &str, const string &prefix);
28
29 //! Returns true if the target string <b>ends</b> with the given suffix.
30 static bool EndsWith(const string &str, const string &suffix);
31
32 //! Repeat a string multiple times
33 static string Repeat(const string &str, const idx_t n);
34
35 //! Split the input string based on newline char
36 static vector<string> Split(const string &str, char delimiter);
37
38 //! Join multiple strings into one string. Components are concatenated by the given separator
39 static string Join(const vector<string> &input, const string &separator);
40
41 //! Join multiple items of container with given size, transformed to string
42 //! using function, into one string using the given separator
43 template <typename C, typename S, typename Func>
44 static string Join(const C &input, S count, const string &separator, Func f) {
45 // The result
46 std::string result;
47
48 // If the input isn't empty, append the first element. We do this so we
49 // don't need to introduce an if into the loop.
50 if (count > 0) {
51 result += f(input[0]);
52 }
53
54 // Append the remaining input components, after the first
55 for (size_t i = 1; i < count; i++) {
56 result += separator + f(input[i]);
57 }
58
59 return result;
60 }
61
62 //! Append the prefix to the beginning of each line in str
63 static string Prefix(const string &str, const string &prefix);
64
65 //! Return a string that formats the give number of bytes
66 static string FormatSize(idx_t bytes);
67
68 //! Convert a string to uppercase
69 static string Upper(const string &str);
70
71 //! Convert a string to lowercase
72 static string Lower(const string &str);
73
74 //! Format a string using printf semantics
75 static string Format(const string fmt_str, ...);
76 static string VFormat(const string fmt_str, va_list ap);
77
78 //! Split the input string into a vector of strings based on the split string
79 static vector<string> Split(const string &input, const string &split);
80
81 //! Remove the whitespace char in the left end of the string
82 static void LTrim(string &str);
83 //! Remove the whitespace char in the right end of the string
84 static void RTrim(string &str);
85 //! Remove the whitespace char in the left and right end of the string
86 static void Trim(string &str);
87
88 static string Replace(string source, const string &from, const string &to);
89};
90} // namespace duckdb
91