1//===-- string_utils.h ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef SCUDO_STRING_UTILS_H_
10#define SCUDO_STRING_UTILS_H_
11
12#include "internal_defs.h"
13#include "vector.h"
14
15#include <stdarg.h>
16
17namespace scudo {
18
19class ScopedString {
20public:
21 explicit ScopedString() { String.push_back(Element: '\0'); }
22 uptr length() { return String.size() - 1; }
23 const char *data() { return String.data(); }
24 void clear() {
25 String.clear();
26 String.push_back(Element: '\0');
27 }
28 void vappend(const char *Format, va_list &Args);
29 void append(const char *Format, ...) FORMAT(2, 3);
30 void append(const s32);
31 void append(const s64);
32 void append(const u32);
33 void append(const u64);
34 void append(const bool);
35 void ensureNullTerminated();
36 void output() const { outputRaw(Buffer: String.data()); }
37 void reserve(size_t Size) { String.reserve(NewSize: Size + 1); }
38 uptr capacity() { return String.capacity() - 1; }
39
40private:
41 void appendNumber(u64 AbsoluteValue, u8 Base, u8 MinNumberLength,
42 bool PadWithZero, bool Negative, bool Upper);
43 void appendUnsigned(u64 Num, u8 Base, u8 MinNumberLength, bool PadWithZero,
44 bool Upper);
45 void appendSignedDecimal(s64 Num, u8 MinNumberLength, bool PadWithZero);
46 void appendString(int Width, int MaxChars, const char *S);
47 void appendPointer(u64 ptr_value);
48
49 Vector<char, 256> String;
50};
51
52void Printf(const char *Format, ...) FORMAT(1, 2);
53
54} // namespace scudo
55
56#endif // SCUDO_STRING_UTILS_H_
57