Go to the documentation of this file.
1#ifndef STRING32_HH
2#define STRING32_HH
4#include <cassert>
5#include <cstdint>
6#include <limits>
7#include <type_traits>
9// Given a buffer of max size 4GB, 32 bits are enough to address each position
10// in that buffer.
11// - On 32-bit systems we can use a pointer to a location in the buffer.
12// - On 64-bit systems a pointer is too large (64 bit), but an index in the
13// buffer works fine. (An index works fine on 32-bit systems as well, but is
14// slightly less efficient).
15// The String32 helper abstracts the difference between the two above
16// approaches. In both cases it is a 32-bit type (hence the name). And on
17// both 32/64-bit systems it uses the more efficient implementation.
19using String32 = std::conditional_t<
20 (sizeof(char*) > sizeof(uint32_t)), // is a pointer bigger than an uint32_t
21 uint32_t, // yes -> use uint32_t
22 const char*>; // no -> directly use pointer
24// convert string in buffer to String32
25constexpr void toString32(const char* buffer, const char* str, uint32_t& result) {
26 assert(buffer <= str);
27 assert(str < (buffer + std::numeric_limits<uint32_t>::max()));
28 result = str - buffer;
30constexpr void toString32(const char* /*buffer*/, const char* str, const char*& result) {
31 result = str;
34// convert String32 back to string in buffer
35[[nodiscard]] constexpr const char* fromString32(const char* buffer, uint32_t str32) {
36 return buffer + str32;
38[[nodiscard]] constexpr const char* fromString32(const char* /*buffer*/, const char* str32) {
39 return str32;
constexpr const char * fromString32(const char *buffer, uint32_t str32)
Definition: String32.hh:35
constexpr void toString32(const char *buffer, const char *str, uint32_t &result)
Definition: String32.hh:25
std::conditional_t<(sizeof(char *) > sizeof(uint32_t)), uint32_t, const char * > String32
Definition: String32.hh:22
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:284