Skip to content

Custom type for active_length to reduce wasteage #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <iostream>
#include <FastString.h>
#include <assert.h>
#include <cstdint>

int main()
{
Expand Down Expand Up @@ -97,4 +98,4 @@ int main()
/* swap */
l.swap(k); // l is "xyz" and k is "34"
swap(l, k); // l is "34" and k is "xyz"
}
}
8 changes: 6 additions & 2 deletions include/FastString.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
#include <algorithm>
#include <string>
#include <ostream>
#include <cstdint>

namespace fss
{
template <class CharT, std::size_t max_length, class Traits = std::char_traits<CharT>>
template <class CharT, std::size_t max_length, class SizeType = std::size_t, class Traits = std::char_traits<CharT>>
class basic_str
{

static_assert(SizeType(-1) >= max_length);

public:
constexpr basic_str() noexcept = default;

Expand Down Expand Up @@ -124,7 +128,7 @@ namespace fss
buffer_[active_length_] = '\0';
}

std::size_t active_length_{0};
SizeType active_length_{0};
CharT buffer_[max_length + 1]{};
};

Expand Down