Terminal++ 3.1.0.4
A C++ library for interacting with ANSI terminal windows
Loading...
Searching...
No Matches
core.hpp
1#pragma once
2
3#include "terminalpp/detail/export.hpp" // IWYU pragma: export
4
5#include <span>
6#include <string>
7#include <cstdint>
8
9namespace terminalpp {
10
11// A byte in Terminal++ is represented consistently as an unsigned
12// 8-bit integer.
13using byte = std::uint8_t;
14
15// A stream of bytes in Terminal++ is exposed as a non-owning span. It is
16// expected that the data will live for no longer than any function in which
17// it is found. For that reason, these spans should never be stored directly.
18// If storage is necessary, it must be converted into a longer-term data
19// structure.
20using bytes = std::span<byte const>;
21
22// Where necessary, bytes are stored in this type, which has the small string
23// optimization, meaning that most cases will not cause an allocation.
24using byte_storage = std::basic_string<byte>;
25
26// A co-ordinate in a terminal is represented consistently as a signed
27// 32-bit integer.
28using coordinate_type = std::int32_t;
29
30inline namespace literals {
31
32// A helper function to convert from a character literal to a byte.
33inline byte operator""_tb(char const text)
34{
35 return static_cast<byte>(text);
36}
37
38// A helper function to convert from string literals to stored bytes.
39inline byte_storage operator""_tb(char const *text, size_t length)
40{
41 byte_storage result;
42 result.reserve(length);
43
44 for (auto const ch : std::span{text, length})
45 {
46 result.push_back(static_cast<byte>(ch));
47 }
48
49 return result;
50}
51
52} // namespace literals
53
54// A helper function to convert from strings to stored bytes.
55inline byte_storage to_bytes(std::string const &str)
56{
57 byte_storage result;
58 result.reserve(str.size());
59
60 for (auto const ch : str)
61 {
62 result.push_back(static_cast<byte>(ch));
63 }
64
65 return result;
66}
67
68} // namespace terminalpp