libmicroemu 0.2.0
ARM Microcontroller Emulator Library
Loading...
Searching...
No Matches
const_string_builder.h
1#pragma once
2
3#include "libmicroemu/types.h"
4#include <cstddef>
5#include <cstdint>
6#include <cstring>
7#include <string_view>
8
9namespace libmicroemu::internal {
10
14class ConstStringBuilder {
15public:
16 ConstStringBuilder(char *buf, std::size_t buf_len) : buf_(buf), buf_len_(buf_len) {}
17
23 ConstStringBuilder &AddString(const std::string_view &str) {
24 for (std::size_t i = 0U; i < str.length(); ++i) {
25 if (act_pos_ >= (buf_len_ - 1U)) { // -1 due to null termination
26 return *this;
27 }
28 buf_[act_pos_] = str[i];
29 act_pos_++;
30 }
31 return *this;
32 }
33
34 ConstStringBuilder &AddChar(char ch) {
35 if (act_pos_ >= (buf_len_ - 1U)) { // -1 due to null termination
36 return *this;
37 }
38 buf_[act_pos_] = ch;
39 act_pos_++;
40 return *this;
41 }
42 ConstStringBuilder &AddInt(i32 no) {
43 if (act_pos_ >= (buf_len_ - 1U)) { // -1 due to null termination
44 return *this;
45 }
46 std::size_t left_space = buf_len_ - static_cast<size_t>(act_pos_) -
47 1U; // -1 to have enough space for null termination
48
49 act_pos_ += IntToString(&buf_[act_pos_], left_space - 1U, no);
50 return *this;
51 }
52
53 ConstStringBuilder &AddUInt(u32 no) {
54 if (act_pos_ >= (buf_len_ - 1U)) { // -1 due to null termination
55 return *this;
56 }
57 std::size_t left_space = buf_len_ - static_cast<size_t>(act_pos_) -
58 1U; // -1 to have enough space for null termination
59
60 act_pos_ += UIntToString(&buf_[act_pos_], left_space - 1U, no);
61 return *this;
62 }
63
64 ConstStringBuilder &Terminate() {
65 buf_[act_pos_] = '\0';
66 return *this;
67 }
68
69private:
73 u32 IntToString(char *buf, std::size_t len, i32 number) {
74 std::size_t index = 0U;
75 i32 is_negative = 0;
76
77 // Überprüfen, ob die Zahl negativ ist
78 if (number < 0) {
79 is_negative = 1;
80 number = -number;
81 }
82
83 do {
84 if (index >= len - 1U) { // -1 for minus sign
85 break;
86 }
87 buf[index++] = (char)((number % 10) + '0');
88 number /= 10;
89 } while (number > 0);
90
91 // Add negative sign if needed
92 if (is_negative) {
93 if (index < len) {
94 buf[index++] = '-';
95 }
96 }
97 ReverseString(buf, index);
98 return index;
99 }
100
108 u32 UIntToString(char *buf, std::size_t len, u32 number) {
109 std::size_t index = 0U;
110
111 do {
112 if (index >= len) {
113 break;
114 }
115 buf[index++] = (char)((number % 10) + '0');
116 number /= 10;
117 } while (number > 0);
118 ReverseString(buf, index);
119 return index;
120 }
121
122 void ReverseString(char *str, size_t len) {
123 std::size_t start = 0;
124 std::size_t end = len - 1;
125 while (start < end) {
126 char temp = str[start];
127 str[start] = str[end];
128 str[end] = temp;
129 start++;
130 end--;
131 }
132 }
133
134 u32 act_pos_{0U};
135 char *buf_;
136 std::size_t buf_len_;
137};
138
139} // namespace libmicroemu::internal
A class to build a const string in a buffer.
Definition const_string_builder.h:14
ConstStringBuilder & AddString(const std::string_view &str)
Add a string to the buffer.
Definition const_string_builder.h:23
The libmicroemu::internal namespace contains all classes and functions of libmicroemu which are priva...
Definition bkpt_flags.h:6