MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
byte_endian.hpp File Reference
#include "mtcore/byte_order.hpp"
#include "mtcore/colls/slice.hpp"
#include "mtcore/io/writer.hpp"
#include <array>
Include dependency graph for byte_endian.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mtcore::EndianSlice< T, Endian >
 A Slice with Endian-aware data which is just a pointer + length + endian direction Accessing elements through the array operator will do bounds checks and will convert to native endian by default (this can be overridden with access methods) Accessing out of bounds will terminate the program instead of throw Can also get sub slices. More...
 
struct  mtcore::EndianSlice< T, Endian >::ConstEndianWrapper
 Endian Wrapper for grabbing the endian value By default, will handle converting to and from the native architecture Can also manually specify the endian architecture to use. More...
 
struct  mtcore::EndianSlice< T, Endian >::EndianWrapper
 Endian Wrapper for grabbing the endian value By default, will handle converting to and from the native architecture Can also manually specify the endian architecture to use. More...
 
struct  mtcore::RuneIterator< T >
 Iterates over runes of utf8 sequences (char, char_t) More...
 
struct  mtcore::RuneIterator< char32_t >
 Iterates over runes of utf32 sequences Also handles flipping endianness to native endian. More...
 
struct  mtcore::RuneIterator< char16_t >
 Iterates over runes of utf16 sequences Also handles flipping endianness to native endian. More...
 

Namespaces

namespace  mtcore
 Core library for C++ with Zig-related functionality.
 

Typedefs

using mtcore::CharSeq = impl::ShortSeq<char, 4>
 
using mtcore::U8Seq = impl::ShortSeq<char8_t, 4>
 
using mtcore::U16Seq = impl::ShortSeq<char16_t, 2>
 
using mtcore::SliceU16Native = EndianSlice<char16_t, std::endian::native>
 Slice type representing native UTF-16 string *.
 
using mtcore::SliceU16Le = EndianSlice<char16_t, std::endian::little>
 Slice type representing little endian UTF-16 string *.
 
using mtcore::SliceU16Be = EndianSlice<char16_t, std::endian::big>
 Slice type representing big endian UTF-16 string *.
 
using mtcore::SliceConstU16Native = EndianSlice<const char16_t, std::endian::native>
 Slice type representing const native UTF-16 string *.
 
using mtcore::SliceConstU16Le = EndianSlice<const char16_t, std::endian::little>
 Slice type representing const little endian UTF-16 string *.
 
using mtcore::SliceConstU16Be = EndianSlice<const char16_t, std::endian::big>
 Slice type representing const big endian UTF-16 string *.
 
using mtcore::SliceU32Native = EndianSlice<char32_t, std::endian::native>
 Slice type representing native UTF-32 string *.
 
using mtcore::SliceU32Le = EndianSlice<char32_t, std::endian::little>
 Slice type representing little endian UTF-32 string *.
 
using mtcore::SliceU32Be = EndianSlice<char32_t, std::endian::big>
 Slice type representing big endian UTF-32 string.
 
using mtcore::SliceConstU32Native = EndianSlice<const char32_t, std::endian::native>
 Slice type representing const native UTF-32 string.
 
using mtcore::SliceConstU32Le = EndianSlice<const char32_t, std::endian::little>
 Slice type representing const little endian UTF-32 string.
 
using mtcore::SliceConstU32Be = EndianSlice<const char32_t, std::endian::big>
 Slice type representing const big endian UTF-32 string.
 
using mtcore::Rune = char32_t
 

Enumerations

enum class  mtcore::RuneConversionError { mtcore::RuneConversionError::NoInput , mtcore::RuneConversionError::InvalidChars }
 Errors that can occur when doing conversions to a rune. More...
 

Functions

constexpr auto mtcore::is_bom (const char32_t ch)
 Checks if a character is a BOM in native ordering.
 
constexpr auto mtcore::is_bom_rev_16_bit (const char16_t ch)
 Checks if a 16-bit character is a BOM in reverse ordering.
 
constexpr auto mtcore::is_bom_rev_32_bit (const char32_t ch)
 Checks if a 32-bit character is a BOM in reverse ordering.
 
Optional< std::endian > mtcore::u16_detect_endian (const char16_t ch)
 Tries to detect the endian encoding based on a possible BOM mark.
 
Optional< std::endian > mtcore::u32_detect_endian (const char32_t ch)
 Tries to detect the endian encoding based on a possible BOM mark.
 
constexpr bool mtcore::is_surrogate (char16_t ch)
 Checks if UTF-16 character is a surrogate.
 
constexpr bool mtcore::is_high_surrogate (char16_t ch)
 Checks if UTF-16 character is a high surrogate *.
 
constexpr bool mtcore::is_low_surrogate (char16_t ch)
 Checks if UTF-16 character is a low surrogate *.
 
SliceU16Native mtcore::endian_from_native_slice (Slice< char16_t > s)
 Gets a UTF-16 endian slice from a slice which is assumed ot be native endian.
 
SliceU32Native mtcore::endian_from_native_slice (Slice< char32_t > s)
 Gets a UTF-32 endian slice from a slice which is assumed ot be native endian.
 
SliceU16Le mtcore::endian_from_le_slice (Slice< char16_t > s)
 Gets a UTF-16 endian slice from a slice which is assumed ot be little endian.
 
SliceU32Le mtcore::endian_from_le_slice (Slice< char32_t > s)
 Gets a UTF-32 endian slice from a slice which is assumed ot be little endian.
 
SliceU16Be mtcore::endian_from_be_slice (Slice< char16_t > s)
 Gets a UTF-16 endian slice from a slice which is assumed ot be big endian.
 
SliceU32Be mtcore::endian_from_be_slice (Slice< char32_t > s)
 Gets a UTF-32 endian slice from a slice which is assumed ot be big endian.
 
std::variant< SliceU16Le, SliceU16Bemtcore::endian_from_slice (Slice< char16_t > s)
 Gets a UTF-16 endian slice from an input slice Will try to use the first character of the slice as a BOM to detect endianness If endian detection fails, will assume native endian Since endian slices are distinct types, this will return a variant (typed union) If there is a BOM, will drop it.
 
std::variant< SliceU32Le, SliceU32Bemtcore::endian_from_slice (Slice< char32_t > s)
 Gets a UTF-32 endian slice from an input slice Will try to use the first character of the slice as a BOM to detect endianness If endian detection fails, will assume native endian Since endian slices are distinct types, this will return a variant (typed union) If there is a BOM, will drop it.
 
Slice< char16_t > mtcore::slice_from_native_endian (SliceU16Native s)
 Gets a normal slice from a native endian slice.
 
Slice< const char16_t > mtcore::slice_from_native_endian (SliceConstU16Native s)
 Gets a normal slice from a native endian slice.
 
Slice< char32_t > mtcore::slice_from_native_endian (SliceU32Native s)
 Gets a normal slice from a native endian slice.
 
Slice< const char32_t > mtcore::slice_from_native_endian (SliceConstU32Native s)
 Gets a normal slice from a native endian slice.
 
Slice< char16_t > mtcore::slice_from_le_endian (SliceU16Native s)
 Gets a normal slice from a little endian slice.
 
Slice< const char16_t > mtcore::slice_from_le_endian (SliceConstU16Native s)
 Gets a normal slice from a little endian slice.
 
Slice< char32_t > mtcore::slice_from_le_endian (SliceU32Native s)
 Gets a normal slice from a little endian slice.
 
Slice< const char32_t > mtcore::slice_from_le_endian (SliceConstU32Native s)
 Gets a normal slice from a little endian slice.
 
Slice< char16_t > mtcore::slice_from_be_endian (SliceU16Native s)
 Gets a normal slice from a big endian slice.
 
Slice< const char16_t > mtcore::slice_from_be_endian (SliceConstU16Native s)
 Gets a normal slice from a big endian slice.
 
Slice< char32_t > mtcore::slice_from_be_endian (SliceU32Native s)
 Gets a normal slice from a big endian slice.
 
Slice< const char32_t > mtcore::slice_from_be_endian (SliceConstU32Native s)
 Gets a normal slice from a big endian slice.
 
CharSeq mtcore::rune_to_str (Rune rune)
 Converts a UTF-32 Rune to a sequence of char.
 
U8Seq mtcore::rune_to_utf8 (Rune rune)
 Converts a UTF-32 Rune to a utf-8 sequence.
 
U16Seq mtcore::rune_to_utf16 (Rune rune)
 Converts a UTF-32 Rune to a utf-16 sequence.
 
Result< Rune, RuneConversionErrormtcore::utf8_to_rune (const Slice< const char8_t > &rune)
 UTF-8 to a Rune.
 
Result< Rune, RuneConversionErrormtcore::str_to_rune (const Slice< const char > &rune)
 Characters to a Rune.
 
Result< Rune, RuneConversionErrormtcore::utf16_to_rune (const Slice< const char16_t > &rune)
 UTF-16 to a Rune.
 
Result< Rune, RuneConversionErrormtcore::utf16_to_rune (const SliceConstU16Native &rune)
 UTF-16 to a Rune.
 
RuneIterator< char > mtcore::str_rune_iterator (const Slice< const char > &s)
 Rune iterator for a slice of characters.
 
RuneIterator< char8_t > mtcore::utf8_rune_iterator (const Slice< const char8_t > &s)
 Rune iterator for a UTF8 sequence.
 
RuneIterator< char16_t > mtcore::utf16_rune_iterator (const Slice< const char16_t > &s)
 Rune iterator for a UTF16 sequence.
 
RuneIterator< char16_t > mtcore::utf16_rune_iterator (const SliceConstU16Le &s)
 Rune iterator for a little endian UTF16 sequence.
 
RuneIterator< char16_t > mtcore::utf16_rune_iterator (const SliceConstU16Be &s)
 Rune iterator for a big endian UTF16 sequence.
 
RuneIterator< char16_t > mtcore::utf16_rune_iterator (const SliceU16Le &s)
 Rune iterator for a little endian UTF16 sequence.
 
RuneIterator< char16_t > mtcore::utf16_rune_iterator (const SliceU16Be &s)
 Rune iterator for a big endian UTF16 sequence.
 
RuneIterator< char32_t > mtcore::utf32_rune_iterator (const Slice< const char32_t > &s)
 Rune iterator for a UTF32 sequence.
 
RuneIterator< char32_t > mtcore::utf32_rune_iterator (const SliceConstU32Le &s)
 Rune iterator for a little endian UTF32 sequence.
 
RuneIterator< char32_t > mtcore::utf32_rune_iterator (const SliceConstU32Be &s)
 Rune iterator for a big endian UTF32 sequence.
 
RuneIterator< char32_t > mtcore::utf32_rune_iterator (const SliceU32Le &s)
 Rune iterator for a little endian UTF32 sequence.
 
RuneIterator< char32_t > mtcore::utf32_rune_iterator (const SliceU32Be &s)
 Rune iterator for a big endian UTF32 sequence.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf16 (io::Writer< WI > &writer, const SliceConstU32Be &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to UTF-16 string Can optionally write a BOM to the prefix.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf16 (io::Writer< WI > &writer, const SliceConstU32Le &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to UTF-16 string Can optionally write a BOM to the prefix.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf16 (io::Writer< WI > &writer, const Slice< const char32_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to UTF-16 string Can optionally write a BOM to the prefix.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf8 (io::Writer< WI > &writer, const SliceConstU32Be &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to UTF-8 string.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf8 (io::Writer< WI > &writer, const SliceConstU32Le &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to UTF-8 string.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf8 (io::Writer< WI > &writer, const Slice< const char32_t > &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to UTF-8 string.
 
template<WriterImpl WI>
auto mtcore::utf32_to_str (io::Writer< WI > &writer, const SliceConstU32Be &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to character sequence (UTF-8 encoded chars)
 
template<WriterImpl WI>
auto mtcore::utf32_to_str (io::Writer< WI > &writer, const SliceConstU32Le &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to character sequence (UTF-8 encoded chars)
 
template<WriterImpl WI>
auto mtcore::utf32_to_str (io::Writer< WI > &writer, const Slice< const char32_t > &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-32 string to character sequence (UTF-8 encoded chars)
 
template<WriterImpl WI>
auto mtcore::utf8_to_utf32 (io::Writer< WI > &writer, const Slice< const char8_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-8 string to UTF-32.
 
template<WriterImpl WI>
auto mtcore::str_to_utf32 (io::Writer< WI > &writer, const Slice< const char > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-8 string to UTF-32.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf8 (io::Writer< WI > &writer, const SliceConstU16Be &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-8.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf32 (io::Writer< WI > &writer, const SliceConstU16Le &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-32.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf32 (io::Writer< WI > &writer, const SliceConstU16Be &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-32.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf32 (io::Writer< WI > &writer, const Slice< const char16_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-32.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf8 (io::Writer< WI > &writer, const SliceConstU16Le &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-8.
 
template<WriterImpl WI>
auto mtcore::utf16_to_str (io::Writer< WI > &writer, const SliceConstU16Be &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-8.
 
template<WriterImpl WI>
auto mtcore::utf16_to_str (io::Writer< WI > &writer, const SliceConstU16Le &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-8.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf8 (io::Writer< WI > &writer, const Slice< const char16_t > &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-8.
 
template<WriterImpl WI>
auto mtcore::utf16_to_str (io::Writer< WI > &writer, const Slice< const char16_t > &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-16 string to UTF-8.
 
template<WriterImpl WI>
auto mtcore::utf8_to_utf16 (io::Writer< WI > &writer, const Slice< const char8_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-8 string to UTF-16.
 
template<WriterImpl WI>
auto mtcore::str_to_utf16 (io::Writer< WI > &writer, const Slice< const char > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-8 string to UTF-16.
 
template<WriterImpl WI>
auto mtcore::utf8_to_str (io::Writer< WI > &writer, const Slice< const char8_t > &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a UTF-8 string to character sequence.
 
template<WriterImpl WI>
auto mtcore::str_to_utf8 (io::Writer< WI > &writer, const Slice< const char > &str) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Will convert a character sequence to UTF-8 string.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf16le (io::Writer< WI > &writer, const SliceConstU16Native &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts native UTF-16 to UTF-16 Little Endian.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf16le (io::Writer< WI > &writer, const Slice< const char16_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts native UTF-16 to UTF-16 Little Endian.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf16be (io::Writer< WI > &writer, const SliceConstU16Native &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts native UTF-16 to UTF-16 Big Endian.
 
template<WriterImpl WI>
auto mtcore::utf16_to_utf16be (io::Writer< WI > &writer, const Slice< const char16_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts native UTF-16 to UTF-16 Big Endian.
 
template<WriterImpl WI>
auto mtcore::utf16le_to_utf16be (io::Writer< WI > &writer, const SliceConstU16Le &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-16 Little Endian to UTF-16 Big Endian.
 
template<WriterImpl WI>
auto mtcore::utf16be_to_utf16le (io::Writer< WI > &writer, const SliceConstU16Be &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-16 Big Endian to UTF-16 Little Endian.
 
template<WriterImpl WI>
auto mtcore::utf16le_to_utf16 (io::Writer< WI > &writer, const SliceConstU16Le &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-16 Little Endian to Native UTF-16.
 
template<WriterImpl WI>
auto mtcore::utf16be_to_utf16 (io::Writer< WI > &writer, const SliceConstU16Be &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-16 Big Endian to Native UTF-16.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf32le (io::Writer< WI > &writer, const SliceConstU32Native &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Native Endian to UTF-32 Little Endian.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf32le (io::Writer< WI > &writer, const Slice< const char32_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Native Endian to UTF-32 Little Endian.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf32be (io::Writer< WI > &writer, const SliceConstU32Native &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Native Endian to UTF-32 Big Endian.
 
template<WriterImpl WI>
auto mtcore::utf32_to_utf32be (io::Writer< WI > &writer, const Slice< const char32_t > &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Native Endian to UTF-32 Big Endian.
 
template<WriterImpl WI>
auto mtcore::utf32le_to_utf32be (io::Writer< WI > &writer, const SliceConstU32Le &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Little Endian to UTF-32 Big Endian.
 
template<WriterImpl WI>
auto mtcore::utf32be_to_utf32le (io::Writer< WI > &writer, const SliceConstU32Be &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Big Endian to UTF-32 Little Endian.
 
template<WriterImpl WI>
auto mtcore::utf32le_to_utf32 (io::Writer< WI > &writer, const SliceConstU32Le &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Little Endian to UTF-32 Native Endian.
 
template<WriterImpl WI>
auto mtcore::utf32be_to_utf32 (io::Writer< WI > &writer, const SliceConstU32Be &str, bool writeBom=false) -> Result< size_t, typename io::Writer< WI >::ErrType >
 Converts UTF-32 Big Endian to UTF-32 Native Endian.
 

Variables

constexpr char32_t mtcore::bom = 0xFEFF
 Native representation of Byte Order Mark (BOM)
 
template<std::endian target, typename T = char32_t>
constexpr T mtcore::endian_bom = target == std::endian::native ? bom : flip_endian<T>(bom)
 
template<std::endian E = std::endian::native>
constexpr auto mtcore::oppositeEndian = E == std::endian::big ? std::endian::little : std::endian::big
 Returns the opposite endianness of a given endian (default is opposite of the native endian)