DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
StringBuilder.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Text/StringBuilder.h
"
3
#include <algorithm>
4
#include <string>
5
6
#ifdef UNICODE
7
#define TO_TSTRING std::to_wstring
8
#else
9
#define TO_TSTRING std::to_string
10
#endif
11
12
namespace
DotNetDupe
{
13
namespace
System {
14
namespace
Text {
15
16
struct
StringBuilder::Impl {
17
std::string buffer;
18
};
19
20
StringBuilder::StringBuilder
() : m_pImpl(new Impl()) {
22
}
23
24
StringBuilder::StringBuilder
(
int
nCapacity) : m_pImpl(new Impl()) {
26
if
(nCapacity > 0) {
27
m_pImpl->buffer.reserve(nCapacity);
28
}
29
}
30
31
StringBuilder::StringBuilder
(
const
String
& sValue) : m_pImpl(new Impl()) {
33
m_pImpl->buffer = (
const
char
*)sValue;
34
}
35
36
StringBuilder::~StringBuilder
() {
38
if
(m_pImpl) {
39
delete
m_pImpl;
40
m_pImpl =
nullptr
;
41
}
42
}
43
44
int
StringBuilder::GetLength
()
const
{
46
return
m_pImpl ?
static_cast<
int
>
(m_pImpl->buffer.length()) : 0;
47
}
48
49
void
StringBuilder::SetLength
(
int
nLength) {
51
if
(nLength < 0 || !m_pImpl)
return
;
52
m_pImpl->buffer.resize(nLength);
53
}
54
55
int
StringBuilder::GetCapacity
()
const
{
57
return
m_pImpl ?
static_cast<
int
>
(m_pImpl->buffer.capacity()) : 0;
58
}
59
60
void
StringBuilder::SetCapacity
(
int
nCapacity) {
62
if
(!m_pImpl || nCapacity <
static_cast<
int
>
(m_pImpl->buffer.length()))
return
;
63
m_pImpl->buffer.reserve(nCapacity);
64
}
65
66
StringBuilder
&
StringBuilder::Append
(
const
String
& sValue) {
68
if
(m_pImpl) m_pImpl->buffer.append((
const
char
*)sValue);
69
return
*
this
;
70
}
71
72
StringBuilder
&
StringBuilder::Append
(
const
char
* pValue) {
74
if
(m_pImpl && pValue) m_pImpl->buffer.append(pValue);
75
return
*
this
;
76
}
77
78
StringBuilder
&
StringBuilder::Append
(
char
chValue) {
80
if
(m_pImpl) m_pImpl->buffer.append(1, chValue);
81
return
*
this
;
82
}
83
84
StringBuilder
&
StringBuilder::Append
(
int
iValue) {
86
if
(m_pImpl) m_pImpl->buffer.append(std::to_string(iValue));
87
return
*
this
;
88
}
89
90
StringBuilder
&
StringBuilder::Append
(
long
long
llValue) {
92
if
(m_pImpl) m_pImpl->buffer.append(std::to_string(llValue));
93
return
*
this
;
94
}
95
96
StringBuilder
&
StringBuilder::Append
(
double
value) {
98
if
(m_pImpl) m_pImpl->buffer.append(std::to_string(value));
99
return
*
this
;
100
}
101
102
StringBuilder
&
StringBuilder::Append
(
bool
bValue) {
104
if
(m_pImpl) m_pImpl->buffer.append(bValue ?
"True"
:
"False"
);
105
return
*
this
;
106
}
107
108
StringBuilder
&
StringBuilder::AppendLine
() {
110
if
(m_pImpl) m_pImpl->buffer.append(
"\r\n"
);
111
return
*
this
;
112
}
113
114
StringBuilder
&
StringBuilder::AppendLine
(
const
String
& sValue) {
116
Append
(sValue);
117
return
AppendLine
();
118
}
119
120
StringBuilder
&
StringBuilder::Clear
() {
122
if
(m_pImpl) m_pImpl->buffer.clear();
123
return
*
this
;
124
}
125
126
String
StringBuilder::ToString
()
const
{
128
return
m_pImpl ?
String
(m_pImpl->buffer.c_str()) :
String
(
""
);
129
}
130
}
131
}
132
}
StringBuilder.h
Represents a mutable string of characters for efficient dynamic text composition.
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::String
String()
Initializes a new instance of the String class to an empty string.
Definition
String.cpp:64
DotNetDupe::System::Text::StringBuilder::Append
StringBuilder & Append(const String &value)
Appends a copy of the specified string to this instance.
Definition
StringBuilder.cpp:66
DotNetDupe::System::Text::StringBuilder::SetLength
void SetLength(int value)
Sets the length of the current StringBuilder object.
Definition
StringBuilder.cpp:49
DotNetDupe::System::Text::StringBuilder::SetCapacity
void SetCapacity(int value)
Sets the capacity of the current StringBuilder object.
Definition
StringBuilder.cpp:60
DotNetDupe::System::Text::StringBuilder::~StringBuilder
~StringBuilder() override
Destructor. Releases internal buffer.
Definition
StringBuilder.cpp:36
DotNetDupe::System::Text::StringBuilder::GetCapacity
int GetCapacity() const
Gets the maximum number of characters that can be contained in the memory allocated by the current in...
Definition
StringBuilder.cpp:55
DotNetDupe::System::Text::StringBuilder::AppendLine
StringBuilder & AppendLine()
Appends the default line terminator to the end of the current StringBuilder object.
Definition
StringBuilder.cpp:108
DotNetDupe::System::Text::StringBuilder::ToString
String ToString() const
Converts the value of this instance to a String.
Definition
StringBuilder.cpp:126
DotNetDupe::System::Text::StringBuilder::Clear
StringBuilder & Clear()
Removes all characters from the current StringBuilder instance.
Definition
StringBuilder.cpp:120
DotNetDupe::System::Text::StringBuilder::GetLength
int GetLength() const
Gets the length of the current StringBuilder object.
Definition
StringBuilder.cpp:44
DotNetDupe::System::Text::StringBuilder::StringBuilder
StringBuilder()
Initializes a new instance of the StringBuilder class.
Definition
StringBuilder.cpp:20
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
StringBuilder.cpp
Generated by
1.18.0