DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
StringBuilder.cpp
Go to the documentation of this file.
1#include "pch.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
12namespace 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
38 if (m_pImpl) {
39 delete m_pImpl;
40 m_pImpl = nullptr;
41 }
42 }
43
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
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
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
80 if (m_pImpl) m_pImpl->buffer.append(1, chValue);
81 return *this;
82 }
83
86 if (m_pImpl) m_pImpl->buffer.append(std::to_string(iValue));
87 return *this;
88 }
89
92 if (m_pImpl) m_pImpl->buffer.append(std::to_string(llValue));
93 return *this;
94 }
95
98 if (m_pImpl) m_pImpl->buffer.append(std::to_string(value));
99 return *this;
100 }
101
104 if (m_pImpl) m_pImpl->buffer.append(bValue ? "True" : "False");
105 return *this;
106 }
107
110 if (m_pImpl) m_pImpl->buffer.append("\r\n");
111 return *this;
112 }
113
116 Append(sValue);
117 return AppendLine();
118 }
119
122 if (m_pImpl) m_pImpl->buffer.clear();
123 return *this;
124 }
125
128 return m_pImpl ? String(m_pImpl->buffer.c_str()) : String("");
129 }
130 }
131 }
132}
Represents a mutable string of characters for efficient dynamic text composition.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
StringBuilder & Append(const String &value)
Appends a copy of the specified string to this instance.
void SetLength(int value)
Sets the length of the current StringBuilder object.
void SetCapacity(int value)
Sets the capacity of the current StringBuilder object.
~StringBuilder() override
Destructor. Releases internal buffer.
int GetCapacity() const
Gets the maximum number of characters that can be contained in the memory allocated by the current in...
StringBuilder & AppendLine()
Appends the default line terminator to the end of the current StringBuilder object.
String ToString() const
Converts the value of this instance to a String.
StringBuilder & Clear()
Removes all characters from the current StringBuilder instance.
int GetLength() const
Gets the length of the current StringBuilder object.
StringBuilder()
Initializes a new instance of the StringBuilder class.