Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
TextConvert.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Characters_TextConvert_h_
5#define _Stroika_Foundation_Characters_TextConvert_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <optional>
10#include <span>
11
13
14/**
15 * \file
16 * Wrappers and extensions to the CodeCvt/std::codecvt<> logic. Often when reading/writing files (or a series of bytes)
17 * you have no, or limited knowledge of the code page. This contains logic to help with that case.
18 *
19 * @todo - this will replace the (probably to be deprecated) CodePage module - at least much/most of it.
20 */
21
23
24 using namespace std;
25
26 /**
27 * \brief Whether text written as bytes begins with a byte order mark (BOM) - U+FEFF, encoded, saying which Unicode
28 * encoding (and byte order) follows
29 *
30 * Whether one helps depends on the FORMAT written, not on where the bytes go (file, memory, network) - which a writer
31 * rarely knows anyway:
32 * o Harmful - JSON (RFC 8259: implementations "MUST NOT add a byte order mark" to networked JSON), XML in UTF-8 (it
33 * declares its own encoding, UTF-8 by default), anything sent with a charset (HTTP's Content-Type), and any format
34 * parsed by a reader that does not expect one - a small device's parser, a regular expression, a shell
35 * script's first line. Unicode itself: for UTF-8, "neither required nor recommended".
36 * o Helpful - plain text that declares no encoding (.txt, CSV, INI), read by a program that otherwise assumes a
37 * legacy code page: Excel opening a UTF-8 CSV is the common case.
38 * o Needed - UTF-16 and UTF-32: XML requires one there (XML 1.0, section 4.3.3), and other text has no other way to
39 * give its byte order.
40 *
41 * So Stroika:
42 * o writes none by default - each writer, TextToBinary::Writer included, takes eDontInclude unless told otherwise
43 * o reads one where it is present (ReadByteOrderMark, BinaryToText::Reader), and without one, and with no encoding
44 * named, takes the text as UTF-8 (@see Streams::BinaryToText::ByteOrderMarkOr)
45 */
46 enum class ByteOrderMark {
47 eInclude,
48 eDontInclude
49 };
50
51 /**
52 * returns the byte order mark for the given unicode encoding. Size is always <= kMaxBOMSize
53 */
54 constexpr span<const byte> GetByteOrderMark (UnicodeExternalEncodings e) noexcept;
55
56 /**
57 * Max size of span returned by GetByteOrderMark () - UTF-32's
58 */
59 constexpr size_t kMaxBOMSize = 4;
60
61 /**
62 * returns guessed encoding, and number of bytes consumed. If 'd' doesn't contain
63 * BOM (possible cuz not large enuf) - returns nullopt
64 *
65 * Pass in any size span, but recommended to use size kMaxBOMSize (less and you may miss some, more and wont use extra data).
66 *
67 * \par Example Usage:
68 * \code
69 * span<const byte> from = argument;
70 * if (optional<tuple<UnicodeExternalEncodings, size_t>> o = ReadByteOrderMark (from)) {
71 * return make_tuple (Characters::CodeCvt<Character> (get<0> (*o)), get<1> (*o));
72 * }
73 * else {
74 * return make_tuple (Characters::CodeCvt<Character> (UnicodeExternalEncodings::eDEFAULT), 0);
75 * }
76 * \endcode
77 */
78 constexpr optional<tuple<UnicodeExternalEncodings, size_t>> ReadByteOrderMark (span<const byte> d) noexcept;
79
80 /**
81 * \pre into.size () >= SizeOfByteOrderMark (e)
82 *
83 * returns remaining span to write into (basically just into.subspan(SizeOfByteOrderMark (e))
84 * so caller can continue writing
85 */
86 span<byte> WriteByteOrderMark (UnicodeExternalEncodings e, span<byte> into);
87
88}
89
90/*
91 ********************************************************************************
92 ***************************** Implementation Details ***************************
93 ********************************************************************************
94 */
95#include "TextConvert.inl"
96
97#endif /*_Stroika_Foundation_Characters_TextConvert_h_*/
ByteOrderMark
Whether text written as bytes begins with a byte order mark (BOM) - U+FEFF, encoded,...
Definition TextConvert.h:46
span< byte > WriteByteOrderMark(UnicodeExternalEncodings e, span< byte > into)
constexpr optional< tuple< UnicodeExternalEncodings, size_t > > ReadByteOrderMark(span< const byte > d) noexcept
constexpr span< const byte > GetByteOrderMark(UnicodeExternalEncodings e) noexcept
UnicodeExternalEncodings
list of external UNICODE character encodings, for file IO (eDEFAULT = eUTF8)
Definition UTFConvert.h:31
STL namespace.