-
Notifications
You must be signed in to change notification settings - Fork 12
ST::buffer
#include <string_theory/char_buffer>
Type | Description |
---|---|
char_T |
Character type. Usually one of char , wchar_t , char16_t , or char32_t
|
Member Type | Definition |
---|---|
size_type |
size_t |
difference_type |
ptrdiff_t |
value_type |
char_T |
pointer |
value_type * |
const_pointer |
const value_type * |
reference |
value_type & |
const_reference |
const value_type & |
iterator |
value_type * |
const_iterator |
const value_type * |
reverse_iterator |
std::reverse_iterator<iterator> |
const_reverse_iterator |
std::reverse_iterator<const_iterator> |
Type | Definition |
---|---|
ST::char_buffer |
ST::buffer<char> |
ST::wchar_buffer |
ST::buffer<wchar_t> |
ST::utf16_buffer |
ST::buffer<char16_t> |
ST::utf32_buffer |
ST::buffer<char32_t> |
Name | Summary |
---|---|
(constructor) | Constructor for character buffers |
(destructor) | Destructor for character buffers |
compare | Compare buffer content lexicographically |
compare_n | Compare N characters of buffer content lexicographically |
operator= | Assignment operator |
operator== operator!= operator< |
Comparison operators |
data | Access the character buffer data |
c_str | Get a C-style pointer to the character buffer data |
size | Return the number of characters stored in the buffer |
clear | Reset the buffer to the empty state (size() == 0 ) |
empty |
Determine if the buffer is empty (size() == 0 ) |
operator[] | Direct character access |
at | Direct character access |
front | Access the first character in the buffer |
back | Access the last character in the buffer |
begin cbegin |
Get an iterator to the beginning of the buffer |
end cend |
Get an iterator to the end of the buffer |
rbegin crbegin |
Get a reverse iterator to the beginning of the buffer |
rend crend |
Get a reverse iterator to the end of the buffer |
allocate | Allocate uninitialized space for the buffer to be filled in |
Create and return a writable pointer to the buffer | |
to_std_string | Convert the buffer to a std::basic_string<char_T>
|
view | Create a std::string_view of all or part of the buffer |
Implicit conversion to std::string_view
|
Name | Summary |
---|---|
strlen | Generic string length utility for templates |
Name | Summary |
---|---|
Null type for constructing empty objects efficiently | |
operator== operator!= |
Comparison operator overloads |
operator"" _stbuf | ST::buffer user-defined literal operator |
Name | Summary |
---|---|
ST_AUTO_SIZE | Special value to indicate that a size should be calculated automatically |
ST_CHAR_LITERAL | Efficient construction of ST::char_buffer literals |
ST_UTF16_LITERAL | Efficient construction of ST::utf16_buffer literals |
ST_UTF32_LITERAL | Efficient construction of ST::utf16_buffer literals |
ST_WCHAR_LITERAL | Efficient construction of ST::wchar_buffer literals |
The ST::buffer<char_T>
class provides basic storage for a contiguous
sequence of characters. This is used internally by ST::string,
as well as a storage area for the various UTF conversion results. It can also
be used by itself as a way to store characters in various encodings; for
example, ST::wchar_buffer objects may provide convenient
storage for Win32 APIs which use the wchar_t
variants.
Note that ST::buffer
objects are meant to be used primarily as storage, not
for string manipulation. You should convert the buffer to an ST::string
in order to use the normal string methods on the data. However, when constructing
strings from various sources (such as reading from a file), it may be more
convenient to create the buffer with the allocate() method, and then
fill in the data directly before passing it off to an ST::string.
Unlike ST::string, ST::buffer
objects are not necessarily
immutable or re-entrant. It is not recommended to share buffer objects across
multiple threads without locking.
Implementation Note: ST::buffer
is a short-string optimized data type.
This means that buffers smaller than the buffer size (currently 11-15 char_T
code units depending on the size of char_T
, plus one for the nul-terminator)
will exist purely on the stack with no associated heap data.
Signature | |
---|---|
buffer() noexcept | (1) |
(2) | |
buffer(const buffer<char_T> ©) | (3) |
buffer(buffer<char_T> &&move) noexcept | (4) |
buffer(const char_T *data, size_t size) | (5) |
buffer(size_t count, char_T fill) | (6) |
- Default constructor for character buffers. Creates an empty buffer.
- Shortcut constructor for empty character buffers. This is equivalent to the empty constructor [1].
- Copy constructor. Creates a new buffer with the same contents as
copy
. - Move constructor. Moves the contents of
move
into this object. - Creates a new buffer with a copy of the first
size
characters pointed to bydata
. This will make a copy of the data buffer, so the original is safe to delete or modify after the object is constructed. - Create a buffer of size
count
and pre-fill it with the specifiedfill
character.
Changed in 3.0: Added overload #6 to construct a buffer with a fill character.
Changed in 3.4: Deprecated null_t
overload.
See also operator=()
Signature |
---|
~buffer() noexcept |
Destroys the buffer, freeing any associated memory.
Signature | |
---|---|
char_T &at(size_t index) | (1) |
const char_T &at(size_t index) const | (2) |
Returns a reference to the character at position index
with bounds checking.
If the provided index
is >= the buffer's size()
, a
std::out_of_range
exception will be thrown.
Since string_theory 2.0.
Signature | |
---|---|
void allocate(size_t size) | (1) |
void allocate(size_t size, char_T fill) |
(2) |
- Allocate (uninitialized) storage for
size
char_T
characters, plus one space for the terminating nul character. - Allocate storage for
size
char_T
characters and initialize it with the specifiedfill
character.
This method will allocate a new storage buffer within the ST::buffer
object,
with enough space for the given size
plus the terminating nul character. If
the buffer already had any existing content, that content will be destroyed.
This is primarily useful in constructing buffers that can be populated from
external sources more efficiently than storing the data elsewhere and then
converting it to a ST::buffer
or ST::string.
Note that unlike create_writable_buffer
from string_theory 1.0, this method
will nul-terminate the internal buffer, even if the rest of the buffer is left
uninitialized as with the first variant.
Example
ST::string read_string(FILE *stream)
{
ST::char_buffer result;
uint32_t size;
size_t nread = fread(&size, sizeof(size), 1, stream);
assert(nread == sizeof(size));
result.allocate(size);
nread = fread(result.data(), sizeof(char), size, stream);
return ST::string::from_utf8(result);
}
Since string_theory 2.0.
Changed in 3.0: Change type of fill
parameter in overload #2 to
char_T.
Signature | |
---|---|
char_T &back() noexcept | (1) |
const char_T &back() const noexcept | (2) |
Returns a reference to the last character in the buffer. If the buffer is empty, this will be a reference to the terminating nul character.
Since string_theory 2.0.
Signature | |
---|---|
iterator begin() noexcept | (1) |
const_iterator begin() const noexcept | (2) |
const_iterator cbegin() const noexcept | (3) |
Return an iterator to the beginning of the buffer.
Since string_theory 2.0.
See also end()
Signature | |
---|---|
const char_T *c_str() const noexcept | (1) |
const char_T *c_str(const char_T *substitute) const noexcept | (2) |
Returns a pointer to the stored character data. This buffer should always be nul-terminated, so it's safe to use in functions which require C-style string buffers.
For variant #2, If this buffer is empty, the pointer provided in substitute
will be returned instead.
Since string_theory 2.3.
See also data()
Signature |
---|
void clear() noexcept |
Clear the buffer data, resetting this buffer to the empty state.
Since string_theory 3.4
See also empty()
Signature | |
---|---|
static int compare(const char_T *left, size_t lsize, const char_T *right, size_t rsize) noexcept | (1) |
static int compare(const char_T *left, size_t lsize, const char_T *right, size_t rsize, size_t maxlen) noexcept | (2) |
int compare(const buffer<char_T> &other) const noexcept | (3) |
int compare(const char_T *str) const noexcept | (4) |
- Compare two
char_T
strings lexicographically, in a manner similar tostrcmp
. The size of both input strings must be known and specified. This function returns a value less than, equal to or greater than0
depending on the ordering or equality of the inputs. This comparison is safe on strings with embedded nul characters. - Compare two
char_T
strings lexicographically. This is similar to overload #1, but also takes a maximum number of characters to compare, which may be less than the length of both input strings. This comparison is safe on strings with embedded nul characters. - Compare this buffer to
other
lexicographically in a manner similar tostrcmp
. This comparison is safe on buffers with embedded nul characters. - Compare this buffer to
str
lexicographically in a manner similar tostrcmp
. The array pointed to bystr
must be nul-terminated.
Since string_theory 3.0
See also operator==(), operator<(), compare_n()
Signature | |
---|---|
int compare_n(const buffer<char_T> &other, size_t count) const noexcept | (1) |
int compare_n(const char_T *str, size_t count) const noexcept | (2) |
- Compare up to the first
count
characters of this buffer toother
lexicographically in a manner similar tostrncmp
. This comparison is safe on buffers with embedded nul characters. - Compare up to the first
count
characters of this buffer tostr
lexicographically in a manner similar tostrncmp
. The array pointed to bystr
must be nul-terminated.
Since string_theory 3.0
See also compare()
Signature |
---|
NOTE: This method is deprecated in 2.0 and should not be used in new code. Instead, use allocate() and directly fill in the buffer with the provided accessors.
Deprecated in string_theory 2.0.
Removed in string_theory 3.0.
Signature | |
---|---|
char_T *data() noexcept | (1) |
const char_T *data() const noexcept | (2) |
Returns a pointer to the first stored character in the buffer.
Changed in 2.0: Added non-const method.
See also c_str()
Signature | |
---|---|
bool empty() const noexcept | (1) |
(2) |
Returns true if this buffer is empty (has no characters). Note that even
for an empty buffer, the first character pointed to by data() can
be accessed, and should be the nul character ('\0'
).
Changed in 2.0: Added empty()
and deprecated is_empty()
.
Changed in 3.0: Removed is_empty()
.
Signature | |
---|---|
iterator end() noexcept | (1) |
const_iterator end() const noexcept | (2) |
const_iterator cend() const noexcept | (3) |
Return an iterator to the end of the buffer.
Since string_theory 2.0.
See also begin()
Signature | |
---|---|
char_T &front() noexcept | (1) |
const char_T &front() const noexcept | (2) |
Returns a reference to the first character in the buffer. If the buffer is empty, this will be a reference to the terminating nul character.
Since string_theory 2.0.
Signature | |
---|---|
(1) | |
buffer<char_T> &operator=(const buffer<char_T> ©) | (2) |
buffer<char_T> &operator=(buffer<char_T> &&) noexcept | (3) |
- Assigns an empty buffer to this object. Equivalent to calling clear().
- Copy the contents of
copy
into the current buffer object. - Move the contents of
move
into the current buffer object.
Changed in 3.4: Deprecated null_t
overload.
See also (constructor)
Signature | |
---|---|
(1) | |
bool operator==(const buffer<char_T> &other) const noexcept | (2) |
(3) | |
bool operator!=(const buffer<char_T> &other) const noexcept | (4) |
bool operator<(const buffer<char_T> &other) const noexcept | (5) |
- Returns empty().
- Returns true if the contents of this buffer are identical to the contents of
other
. This comparison is safe on buffers with embedded nul characters. - Returns !empty().
- Returns true if the contents of this buffer are different from the contents of
other
. This comparison is safe on buffers with embedded nul characters. - Returns true if the contents of this buffer are lexicographically sorted
before
other
. This comparison is safe on buffers with embedded nul characters.
Changed in 3.0: Added operator<() overload.
Changed in 3.4: Deprecated null_t
overloads.
See also operator==(), operator!=(), compare()
Signature | |
---|---|
char_T &operator[](size_t index) noexcept | (1) |
const char_T &operator[](size_t index) const noexcept | (2) |
Returns a reference to the character at position index
with NO bounds checking.
Since string_theory 2.0.
Signature |
---|
This operator overload allows implicit conversion of a ST::buffer
to a
std::string_view into
the entire contents of the buffer.
Since string_theory 2.0.
Removed in string_theory 3.0.
Signature | |
---|---|
reverse_iterator rbegin() noexcept | (1) |
const_reverse_iterator rbegin() const noexcept | (2) |
const_reverse_iterator crbegin() const noexcept | (3) |
Return a reverse iterator to the reverse-start of the buffer.
Since string_theory 2.0.
See also rend()
Signature | |
---|---|
reverse_iterator rend() noexcept | (1) |
const_reverse_iterator rend() const noexcept | (2) |
const_reverse_iterator crend() const noexcept | (3) |
Return a reverse iterator to the reverse-end of the buffer.
Since string_theory 2.0.
See also rbegin()
Signature |
---|
size_t size() const noexcept |
Returns the number of char_T
character units stored in the buffer, not
including the terminating nul ('\0'
) character.
Signature |
---|
static size_t strlen(const char_T *buffer) |
Returns the number of characters up to but not including the first
nul-terminator pointed to by buffer
. This is equivalent to the C standard
function strlen()
, except that it works on arbitrary character types.
Signature |
---|
std::basic_string<char_T> to_std_string() const |
Convert this buffer to a std::basic_string
type compatible with the buffer's char_T
character type. Note that this
conversion uses the buffer as-is; that is, it does no encoding or conversion on
the buffer data.
Since string_theory 2.0.
Signature |
---|
std::basic_string_view<char_T> view(size_t start = 0, size_t length = ST_AUTO_SIZE) const |
Return a std::basic_string_view into the buffer's character data.
Since string_theory 2.0.
namespace ST
{
struct null_t { };
extern const null_t null;
}
This tag structure is deprecated, and should not be used in new code.
Instead of using this object, use the empty constructors (e.g.
ST::char_buffer{}
or ST::string{}
), or the appropriate methods (e.g.
ST::string::empty()
or ST::string::clear()
) for the object.
Deprecated in string_theory 3.4.
Signature | |
---|---|
(1) | |
(2) |
- Returns
right.empty()
. - Returns
!right.empty()
.
Changed in 3.4: Deprecated null_t
overloads.
See also operator==(), operator!=()
using namespace ST::literals;
Signature | |
---|---|
ST::char_buffer operator"" _stbuf(const char *str, size_t size) | (1) |
ST::wchar_buffer operator"" _stbuf(const wchar_t *str, size_t size) | (2) |
ST::utf16_buffer operator"" _stbuf(const char16_t *str, size_t size) | (3) |
ST::utf32_buffer operator"" _stbuf(const char32_t *str, size_t size) | (4) |
ST::char_buffer operator"" _stbuf(const char8_t *str, size_t size) | (5) |
User-defined literal operator to convert string literals to ST::buffer<T>
objects efficiently.
Since: string_theory 2.3.
Changed in 3.0: Moved these to the ST::literals
namespace to avoid
polluting the global namespace.
See also ST_CHAR_LITERAL(), ST_UTF16_LITERAL(), ST_UTF32_LITERAL(), ST_WCHAR_LITERAL()
#define ST_AUTO_SIZE (static_cast<size_t>(-1))
This value is used specially by a variety of string_theory functions to indicate that a size should be determined automatically instead of by the caller.
For example, when passing arguments to functions that take a C-style
string, the ST_AUTO_SIZE
parameter will instruct the function to
determine the length of the string with ::strlen
or equivalent. For
functions that take a range of characters, this may indicate to use the
remainder of the string after the specified start point rather than the
requiring the caller to determine the length.
#define ST_CHAR_LITERAL(str) ...
Construct a ST::char_literal
from static string data in an efficient manner.
Since string_theory 2.3.
See also operator"" _stbuf()
#define ST_UTF16_LITERAL(str) ...
Construct a ST::utf16_literal
from static string data in an efficient manner.
Since string_theory 2.3.
See also operator"" _stbuf()
#define ST_UTF32_LITERAL(str) ...
Construct a ST::utf32_literal
from static string data in an efficient manner.
Since string_theory 2.3.
See also operator"" _stbuf()
#define ST_WCHAR_LITERAL(str) ...
Construct a ST::wchar_literal
from static string data in an efficient manner.
Since string_theory 2.3.
See also operator"" _stbuf()