Stroika Library 3.0d23x
 
Loading...
Searching...
No Matches
SDKString.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Configuration_SDKString_h_
5#define _Stroika_Foundation_Configuration_SDKString_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <span>
10#include <string>
11
12#include "Stroika/Foundation/Characters/SDKChar.h"
13
14/**
15 * TODO
16 * @todo CONSIDER directly documenting/supporting EUC - https://en.wikipedia.org/wiki/Extended_Unix_Code as alternate for non-unicode systems
17 */
18
20
21 /**
22 * This is the kind of String passed to most platform APIs.
23 *
24 * The easiest way to convert between a String and SDKString, is via the String class APIs:
25 * AsSDKString, AsNarrowSDKString, FromSDKString, FromNarrowSDKString.
26 *
27 * For std::string (etc) interop, that works, but also @SDK2Narrow and @Narrow2SDK
28 *
29 * @see SDKChar
30 *
31 * Notes:
32 * NOTE - in the context of this file, the word "Narrow" refers to single byte encodings of UNICODE
33 * characters (such as SJIS, UTF-8, or ISO-Latin-1, for example).
34 *
35 * NOTE - in the context of this file, the word "Wide" refers to wchar_t based encoding of UNICODE
36 * characters.
37 */
38 using SDKString = basic_string<SDKChar>;
39
40 /**
41 * SDKSTR is a macro to wrap constant string literals to get const SDKChar*
42 */
43#if qTargetPlatformSDKUseswchar_t
44#define SDKSTR(x) L##x
45#else
46#define SDKSTR(x) x
47#endif
48
49 /**
50 * This flag ignores missing code points (when transforming from UNICODE to some character set that might not contain them),
51 * and does the best possible to map characters. Needed for things like translating a UNICODE error message to a locale{}
52 * character set which might not contain some of those UNICODE characters.
53 */
55 eIgnoreErrors
56 };
57 using AllowMissingCharacterErrorsFlag::eIgnoreErrors;
58
59 /**
60 * \brief often apply this adjustment to return the non-nul string prefix from a span of characters.
61 *
62 * If using a buffer, which is known to be nul-terminated, automatic conversions of the buffer to a span return the SIZE
63 * of the buffer (not the size of the 'string' part). Use this to produce a span of just the interesting characters.
64 *
65 * Typically just returns one less than the original span size, or span{data,strlen(data)}.
66 *
67 * \req span contains a NUL-terminator
68 */
69 template <typename CHAR_T, size_t EXTENT>
70 requires (same_as<remove_cv_t<CHAR_T>, char> or same_as<remove_cv_t<CHAR_T>, SDKChar>)
71 span<CHAR_T, EXTENT> AdjustNulTerminatedStringSpan (span<CHAR_T, EXTENT> s);
72
73 /**
74 * Convert string/span of 'char' - interpreting the char in the locale/active code page of the current
75 * operating systems (@see SDKChar).
76 *
77 * On most platforms, this does nothing, but on Windows, it maps strings to wstring using code-page CP_ACP
78 *
79 * Characters with (detectibly) missing code-points will generate an exception, unless AllowMissingCharacterErrorsFlag is
80 * specified (but exceptions can happen in any case due to possible bad_alloc).
81 */
82 SDKString Narrow2SDK (span<const char> s);
83 SDKString Narrow2SDK (const string& s);
86
87 /**
88 * Interpret the narrow string in the SDKChar manner (locale/charset) and convert to UNICODE wstring.
89 *
90 * This is identical to SDK2Wide () if SDKChar==char (e.g. on Unix).
91 *
92 * Characters with (detectibly) missing code-points will generate an exception, unless AllowMissingCharacterErrorsFlag is
93 * specified (but exceptions can happen in any case due to possible bad_alloc).
94 */
95 wstring NarrowSDK2Wide (span<const char> s);
96 wstring NarrowSDK2Wide (const string& s);
97 wstring NarrowSDK2Wide (span<const char> s, AllowMissingCharacterErrorsFlag);
98 wstring NarrowSDK2Wide (const string& s, AllowMissingCharacterErrorsFlag);
99
100 /**
101 * Interpret the string/span of SDKChar (@see SDKChar) - and convert it to narrow 'char' using the current code-page/locale.
102 *
103 * On most platforms, this does nothing, but on Windows, it maps wstrings to string using code-page CP_ACP
104 *
105 * Characters with (detectibly) missing code-points will generate an exception, unless AllowMissingCharacterErrorsFlag is
106 * specified (but exceptions can happen in any case due to possible bad_alloc).
107 */
108 string SDK2Narrow (span<const SDKChar> s);
109 string SDK2Narrow (const SDKString& s);
110 string SDK2Narrow (span<const SDKChar> s, AllowMissingCharacterErrorsFlag);
112
113 /**
114 * Interpret the string/span of SDKChar (@see SDKChar) - and convert it to UNICODE 'wchar_t' string using the current code-page/locale.
115 *
116 * On Windows, this does nothing as SDKString==wstring, but on other platforms it follows the rules of SDKChar to map it to wstring.
117 *
118 * Characters with (detectibly) missing code-points will generate an exception, unless AllowMissingCharacterErrorsFlag is
119 * specified (but exceptions can happen in any case due to possible bad_alloc).
120 */
121 wstring SDK2Wide (span<const SDKChar> s);
122 wstring SDK2Wide (const SDKString& s);
123 wstring SDK2Wide (span<const SDKChar> s, AllowMissingCharacterErrorsFlag);
125
126 /**
127 * Interpret the string/span of UNICODE wchar_t - and convert it an SDKString string, using the current locale/SDKChar/SDKString rules.
128 *
129 * On Windows, this does nothing as SDKString==wstring, but on other platforms it follows the rules of SDKChar to map it from wstring.
130 *
131 * Characters with (detectibly) missing code-points will generate an exception, unless AllowMissingCharacterErrorsFlag is
132 * specified (but exceptions can happen in any case due to possible bad_alloc).
133 */
134 SDKString Wide2SDK (span<const wchar_t> s);
135 SDKString Wide2SDK (const wstring& s);
136 SDKString Wide2SDK (span<const wchar_t> s, AllowMissingCharacterErrorsFlag);
138
139}
140
141/*
142 ********************************************************************************
143 ***************************** Implementation Details ***************************
144 ********************************************************************************
145 */
146#include "SDKString.inl"
147
148#endif /*_Stroika_Foundation_Configuration_SDKString_h_*/
wstring NarrowSDK2Wide(span< const char > s)
SDKString Wide2SDK(span< const wchar_t > s)
Definition SDKString.cpp:58
wstring SDK2Wide(span< const SDKChar > s)
conditional_t< qTargetPlatformSDKUseswchar_t, wchar_t, char > SDKChar
Definition SDKChar.h:71
basic_string< SDKChar > SDKString
Definition SDKString.h:38
SDKString Narrow2SDK(span< const char > s)
Definition SDKString.inl:40
span< CHAR_T, EXTENT > AdjustNulTerminatedStringSpan(span< CHAR_T, EXTENT > s)
often apply this adjustment to return the non-nul string prefix from a span of characters.
Definition SDKString.inl:21
string SDK2Narrow(span< const SDKChar > s)