CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
strliteral.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef _WIN32
7// _S definition conflicts with Windows SDK headers; pull them in first
8#include <mmintrin.h>
9#include <wchar.h>
10#endif
11
84
85// ---- Simple prefix macros (_S family) ----
86
91#define _S (string) "\xE0\xC1"
92
97#define _SU (string) "\xA0\xC1"
98
103#define _SO (string) "\x80\xC1"
104
105// ---- Named constant declaration helpers ----
106// STR_LEN8 layout: [hdr:u8][0xC1:u8][len:u8][content + NUL] — data at offset 3
107// STR_LEN16 layout: [hdr:u8][0xC1:u8][len:u16][content + NUL] — data at offset 4
108
109#define _STR_CONST8(name, enc, s) \
110 _Static_assert(sizeof(s) - 1 <= 255, "String too long for STR_CONST, use STR_CONSTL"); \
111 static const struct { \
112 uint8 _h, _m, _l; \
113 char _d[sizeof(s)]; \
114 } _cx_sc_##name = { (enc), 0xC1u, (uint8)(sizeof(s) - 1u), s }; \
115 static const strref name = (strref) & _cx_sc_##name
116
117#define _STR_CONST16(name, enc, s) \
118 _Static_assert(sizeof(s) - 1 <= 65535, "String too long for STR_CONSTL"); \
119 static const struct { \
120 uint8 _h, _m; \
121 uint16 _l; \
122 char _d[sizeof(s)]; \
123 } _cx_sc_##name = { (enc), 0xC1u, (uint16)(sizeof(s) - 1u), s }; \
124 static const strref name = (strref) & _cx_sc_##name
125
126// named constant helpers for references
127#define _STR_CONSTR8(name, enc, s) \
128 _Static_assert(sizeof(s) - 1 <= 255, "String too long for STR_CONSTR, use STR_CONSTRL"); \
129 static const struct { \
130 uint8 _h, _m, _l; \
131 char _d[sizeof(s)]; \
132 } _cx_sc_##name = { (enc), 0xC1u, (uint8)(sizeof(s) - 1u), s };
133
134#define _STR_CONSTR16(name, enc, s) \
135 _Static_assert(sizeof(s) - 1 <= 65535, "String too long for STR_CONSTRL"); \
136 static const struct { \
137 uint8 _h, _m; \
138 uint16 _l; \
139 char _d[sizeof(s)]; \
140 } _cx_sc_##name = { (enc), 0xC1u, (uint16)(sizeof(s) - 1u), s };
141
143#define STR_CONST(name, s) _STR_CONST8(name, 0xE1u, s)
145#define STR_CONSTU(name, s) _STR_CONST8(name, 0xA1u, s)
148#define STR_CONSTO(name, s) _STR_CONST8(name, 0x81u, s)
150#define STR_CONSTL(name, s) _STR_CONST16(name, 0xE2u, s)
152#define STR_CONSTUL(name, s) _STR_CONST16(name, 0xA2u, s)
155#define STR_CONSTOL(name, s) _STR_CONST16(name, 0x82u, s)
156
159#define STR_CONSTR(name, s) _STR_CONSTR8(name, 0xE1u, s)
162#define STR_CONSTRU(name, s) _STR_CONSTR8(name, 0xA1u, s)
165#define STR_CONSTRO(name, s) _STR_CONSTR8(name, 0x81u, s)
168#define STR_CONSTRL(name, s) _STR_CONSTR16(name, 0xE2u, s)
171#define STR_CONSTRUL(name, s) _STR_CONSTR16(name, 0xA2u, s)
174#define STR_CONSTROL(name, s) _STR_CONSTR16(name, 0x82u, s)
175
187#define _SR(name) ((strref)&_cx_sc_##name)
188
189// ---- Inline literal macros ----
190
191#if defined(_COMPILER_MSVC)
192// MSVC: no statement expressions; silently degrade to STR_LEN0, same as _S / _SU / _SO.
193#define _SL(s) ((strref)("\xE0\xC1" s))
194#define _SLU(s) ((strref)("\xA0\xC1" s))
195#define _SLO(s) ((strref)("\x80\xC1" s))
196#define _SLL(s) ((strref)("\xE0\xC1" s))
197#define _SLUL(s) ((strref)("\xA0\xC1" s))
198#define _SLOL(s) ((strref)("\x80\xC1" s))
199
200#else
201// GCC/Clang: statement expressions with file-scoped statics — no heap, no runtime strlen.
202
207#define _SL(s) \
208 __extension__({ \
209 _Static_assert(sizeof(s) - 1 < 200, "String too long for _SL, use _SLL"); \
210 static const struct { \
211 uint8 _h, _m, _l; \
212 char _d[sizeof(s)]; \
213 } _sl = { 0xE1u, 0xC1u, (uint8)(sizeof(s) - 1u), s }; \
214 (strref) & _sl; \
215 })
216
221#define _SLU(s) \
222 __extension__({ \
223 _Static_assert(sizeof(s) - 1 < 200, "String too long for _SLU, use _SLUL"); \
224 static const struct { \
225 uint8 _h, _m, _l; \
226 char _d[sizeof(s)]; \
227 } _sl = { 0xA1u, 0xC1u, (uint8)(sizeof(s) - 1u), s }; \
228 (strref) & _sl; \
229 })
230
235#define _SLO(s) \
236 __extension__({ \
237 _Static_assert(sizeof(s) - 1 < 200, "String too long for _SLO, use _SLOL"); \
238 static const struct { \
239 uint8 _h, _m, _l; \
240 char _d[sizeof(s)]; \
241 } _sl = { 0x81u, 0xC1u, (uint8)(sizeof(s) - 1u), s }; \
242 (strref) & _sl; \
243 })
244
249#define _SLL(s) \
250 __extension__({ \
251 static const struct { \
252 uint8 _h, _m; \
253 uint16 _l; \
254 char _d[sizeof(s)]; \
255 } _sl = { 0xE2u, 0xC1u, (uint16)(sizeof(s) - 1u), s }; \
256 (strref) & _sl; \
257 })
258
262#define _SLUL(s) \
263 __extension__({ \
264 static const struct { \
265 uint8 _h, _m; \
266 uint16 _l; \
267 char _d[sizeof(s)]; \
268 } _sl = { 0xA2u, 0xC1u, (uint16)(sizeof(s) - 1u), s }; \
269 (strref) & _sl; \
270 })
271
275#define _SLOL(s) \
276 __extension__({ \
277 static const struct { \
278 uint8 _h, _m; \
279 uint16 _l; \
280 char _d[sizeof(s)]; \
281 } _sl = { 0x82u, 0xC1u, (uint16)(sizeof(s) - 1u), s }; \
282 (strref) & _sl; \
283 })
284
285#endif // _COMPILER_MSVC
286