57#if defined(__GNUC__) || defined(__clang__)
58#define ALWAYS_INLINE inline __attribute__((always_inline))
69 template<
typename char_t>
80 static constexpr auto EOF_VALUE = traits_type::eof();
86 : m_nPos(0), m_pData(nullptr), m_nSize(0) {
95 : m_nPos(0), m_pData(pData), m_nSize(nSize) {
103 : m_nPos(0), m_pData(sText.data()), m_nSize(sText.length()) {
120 auto nChar = m_nPos < m_nSize ? m_pData[m_nPos] :
EOF_VALUE;
130 return m_nPos < m_nSize ? m_pData[m_nPos] :
EOF_VALUE;
145 if (m_nPos > 0)--m_nPos;
154 ALWAYS_INLINE std::size_t
read(
char_type *
const pBuffer,
const std::size_t nCount)
noexcept {
155 const std::size_t nRead = m_nPos + nCount > m_nSize
156 ? nCount + m_nPos - m_nSize
158 std::memcpy(pBuffer, this->m_pData + this->m_nPos, nRead *
sizeof(
char_type));
159 this->m_nPos += nRead;
167 [[nodiscard]] ALWAYS_INLINE
bool eof() const noexcept {
168 return this->m_nPos >= m_nSize;
175 [[nodiscard]] ALWAYS_INLINE std::size_t
tell() const noexcept {
183 ALWAYS_INLINE
void seek(
const std::size_t pos)
noexcept {
191 [[nodiscard]] ALWAYS_INLINE std::size_t
size() const noexcept {
192 return this->m_nSize;
199 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type>
view() const noexcept {
200 return {this->m_pData, this->m_nSize};
207 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type>
sub_view(
208 const std::size_t nBegin, std::size_t nLength)
const noexcept {
209 nLength = nBegin + nLength > m_nSize
210 ? nBegin + nLength - m_nSize
212 return {this->m_pData + nBegin, nLength};
219 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type>
str()
const {
220 return {this->m_pData, this->m_nSize};
227 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type>
remaining_view() const noexcept {
228 if (this->m_nPos >= this->m_nSize)
return "";
229 return {this->m_pData + this->m_nPos, this->m_nSize - this->m_nPos};
236 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type>
remaining_str()
const {
237 if (this->m_nPos >= this->m_nSize)
return "";
238 return {this->m_pData + this->m_nPos, this->m_nSize - this->m_nPos};
244 const std::size_t m_nSize;
262 template<
typename char_t>
286 if (this->m_pData ==
nullptr)
throw std::bad_alloc();
302 std::free(this->m_pData);
310 if (m_nPos + 1 >= m_nSize) realloc(m_nPos + 1);
311 m_pData[m_nPos++] = cChar;
319 ALWAYS_INLINE
void write(
const char_type *
const pData,
const std::size_t nLength) {
320 if (m_nPos + nLength >= m_nSize) realloc(m_nPos + nLength);
321 std::memcpy(this->m_pData + this->m_nPos, pData, nLength *
sizeof(
char_type));
322 this->m_nPos += nLength;
329 [[nodiscard]] ALWAYS_INLINE
bool empty() const noexcept {
330 return this->m_nPos == 0;
337 [[nodiscard]] ALWAYS_INLINE std::size_t
size() const noexcept {
344 ALWAYS_INLINE
void clear() noexcept {
352 ALWAYS_INLINE
void operator<<(
const std::basic_string<char_type> &sString) {
353 const std::size_t nLength = sString.length();
354 if (m_nPos + nLength >= m_nSize) realloc(m_nPos + nLength);
355 std::memcpy(this->m_pData + this->m_nPos, sString.data(), nLength *
sizeof(
char_type));
356 this->m_nPos += nLength;
363 ALWAYS_INLINE
void operator<<(
const std::basic_string_view<char_type> &sString) {
364 const std::size_t nLength = sString.length();
365 if (m_nPos + nLength >= m_nSize) realloc(m_nPos + nLength);
366 std::memcpy(this->m_pData + this->m_nPos, sString.data(), nLength *
sizeof(
char_type));
367 this->m_nPos += nLength;
374 [[nodiscard]] ALWAYS_INLINE std::basic_string_view<char_type>
view() const noexcept {
375 return {this->m_pData, m_nPos};
382 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type>
str()
const {
383 return {this->m_pData, m_nPos};
390 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type>
str_reset() {
391 std::basic_string<char_type> sResult{this->m_pData, m_nPos};
403 [[nodiscard]] ALWAYS_INLINE std::basic_string<char_type>
sub_str(
404 const std::size_t nBegin, std::size_t nLength)
const noexcept {
405 nLength = nBegin + nLength >= m_nSize
406 ? nBegin + nLength - m_nSize
408 return {this->m_pData + nBegin, nLength};
423 void realloc(
const std::size_t nRequestedSize) {
425 std::size_t nNewSize = m_nSize * 2;
426 while (nNewSize < nRequestedSize) nNewSize *= 2;
429 const auto pNewData =
static_cast<char *
>(std::realloc(this->m_pData, nNewSize *
sizeof(
char_type)));
430 if (pNewData ==
nullptr)
throw std::bad_alloc();
433 this->m_pData = pNewData;
434 this->m_nSize = nNewSize;
A class for input stream behavior with a character type.
Definition fakestream.h:70
ALWAYS_INLINE std::basic_string< char_type > str() const
Converts the data to a string.
Definition fakestream.h:219
ALWAYS_INLINE int_type prev() noexcept
Retrieves the previous character from the stream. Does not move stream position.
Definition fakestream.h:137
ALWAYS_INLINE basic_ifakestream(const char *const pData, const std::size_t nSize)
Constructs an input stream with data and size.
Definition fakestream.h:94
traits_type::int_type int_type
Type for representing character values.
Definition fakestream.h:77
ALWAYS_INLINE std::basic_string_view< char_type > sub_view(const std::size_t nBegin, std::size_t nLength) const noexcept
Gets a read-only view of the stream data.
Definition fakestream.h:207
static constexpr auto EOF_VALUE
End-of-file marker.
Definition fakestream.h:80
ALWAYS_INLINE basic_ifakestream()
Constructs an empty input stream.
Definition fakestream.h:85
ALWAYS_INLINE std::basic_string_view< char_type > view() const noexcept
Gets a read-only view of the stream data.
Definition fakestream.h:199
ALWAYS_INLINE void put_back() noexcept
Returns the last read character to the stream.
Definition fakestream.h:144
ALWAYS_INLINE int_type get() noexcept
Retrieves the next character character from the stream and moves to the next.
Definition fakestream.h:119
ALWAYS_INLINE std::basic_string_view< char_type > remaining_view() const noexcept
Gets a read-only view of the stream remaining data.
Definition fakestream.h:227
ALWAYS_INLINE std::size_t tell() const noexcept
Gets the current position in the stream.
Definition fakestream.h:175
ALWAYS_INLINE void seek(const std::size_t pos) noexcept
Seeks to a specified position in the stream.
Definition fakestream.h:183
ALWAYS_INLINE std::size_t size() const noexcept
Gets the stream data size.
Definition fakestream.h:191
std::char_traits< char_t > traits_type
Type traits for the character type.
Definition fakestream.h:73
ALWAYS_INLINE int_type peek() noexcept
Retrieves the next character from the stream. Does not increment stream position.
Definition fakestream.h:129
ALWAYS_INLINE std::size_t read(char_type *const pBuffer, const std::size_t nCount) noexcept
Reads a specified number of characters into a buffer.
Definition fakestream.h:154
traits_type::char_type char_type
Fundamental character type.
Definition fakestream.h:75
ALWAYS_INLINE std::basic_string< char_type > remaining_str() const
Converts the remaining data to a string.
Definition fakestream.h:236
ALWAYS_INLINE basic_ifakestream(const std::string_view &sText)
Constructs an input stream with string view.
Definition fakestream.h:102
ALWAYS_INLINE bool eof() const noexcept
Checks if the end of the stream has been reached.
Definition fakestream.h:167
A class for output stream behavior with a character type.
Definition fakestream.h:263
ALWAYS_INLINE void operator<<(const std::basic_string< char_type > &sString)
Writes a string to the stream using the << operator.
Definition fakestream.h:352
ALWAYS_INLINE basic_ofakestream()
Constructs an output stream with a default buffer.
Definition fakestream.h:280
ALWAYS_INLINE std::size_t size() const noexcept
Gets the stream size.
Definition fakestream.h:337
ALWAYS_INLINE void write(const char_type *const pData, const std::size_t nLength)
Writes a block of data to the stream.
Definition fakestream.h:319
traits_type::char_type char_type
Fundamental character type.
Definition fakestream.h:268
ALWAYS_INLINE std::basic_string_view< char_type > view() const noexcept
Gets a read-only view of the current state of the stream.
Definition fakestream.h:374
ALWAYS_INLINE std::basic_string< char_type > sub_str(const std::size_t nBegin, std::size_t nLength) const noexcept
Converts a portion of the current data to a string.
Definition fakestream.h:403
static constexpr auto EOF_VALUE
End-of-file marker.
Definition fakestream.h:275
ALWAYS_INLINE std::basic_string< char_type > str() const
Converts the current data to a string.
Definition fakestream.h:382
std::char_traits< char_t > traits_type
Type traits for the character type.
Definition fakestream.h:266
ALWAYS_INLINE void operator<<(const std::basic_string_view< char_type > &sString)
Writes a string view to the stream using the << operator.
Definition fakestream.h:363
ALWAYS_INLINE ~basic_ofakestream()
Destructor that frees allocated memory.
Definition fakestream.h:301
static constexpr auto DEFAULT_BUFFER_SIZE
Default buffer size.
Definition fakestream.h:273
ALWAYS_INLINE std::basic_string< char_type > str_reset()
Resets the stream and returns the current data as a string.
Definition fakestream.h:390
ALWAYS_INLINE void clear() noexcept
Clears the stream by resetting the position.
Definition fakestream.h:344
ALWAYS_INLINE void put(const char_type cChar)
Writes a single character to the stream.
Definition fakestream.h:309
ALWAYS_INLINE bool empty() const noexcept
Checks if the stream is empty.
Definition fakestream.h:329
traits_type::int_type int_type
Type for representing character values.
Definition fakestream.h:270
BashSpark main namespace.
Definition command.h:39