summaryrefslogtreecommitdiff
path: root/dev-cpp/cpprestsdk/files/cpprestsdk-2.10.19-llvm-char-traits-patch.patch
blob: d2a3ef9009443ba32c70eecf5f4462ef536730e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
Also submitted upstream https://github.com/microsoft/cpprestsdk/pull/1829
Cpprestsdk is using a lot of unsigned types like uint8_t with C++ streams, this
seems to be fine with msvc/win32 and at least for now with gcc. This patch will
provide a char_traits type to the streams types other than std::char_traits if
needed. This is detected via template specialisations. Google & AIs will probably
suggest adding a std::char_traits<unsingned char> implementation, but I think that
is very innappropriate in a library for use by applications.
--- a/Release/include/cpprest/streams.h	(revision 0b1ce318a757bbfb89bdb0fffb61ca4e38dc3b33)
+++ b/Release/include/cpprest/streams.h	(revision 32b322b564e5e540ff02393ffe3bd3bade8d299c)
@@ -16,6 +16,7 @@
 #define CASA_STREAMS_H
 
 #include "cpprest/astreambuf.h"
+#include "cpprest/details/char_traits.h"
 #include <iosfwd>
 #include <cstdio>
 
@@ -60,30 +61,95 @@
     concurrency::streams::streambuf<CharType> m_buffer;
 };
 
-template<typename CharType>
+template<typename CharType, class Traits = typename utility::CanUseStdCharTraits<CharType>::TraitsType>
 struct Value2StringFormatter
 {
+    struct SanitizeInput
+    {
+        const std::basic_string<char> &operator () (const std::basic_string<char> &input)
+        {
+            return input;
+        }
+        template <class InputTraits> std::basic_string<char> operator () (const std::basic_string<unsigned char, InputTraits> &input)
+        {
+            return {reinterpret_cast<const char *>(input.c_str()), input.size()};
+        }
+        const char *operator () (const char *input) {
+            return input;
+        }
+        const char *operator () (const unsigned char *input)
+        {
+            return reinterpret_cast<const char *>(input);
+        }
+        template <class T> T operator () (T input)
+        {
+            return input;
+        }
+    };
+    struct GenerateFormatOutput
+    {
+        std::basic_string<CharType,Traits> &&operator() (std::basic_string<CharType,Traits> &&result)
+        {
+            return std::move(result);
+        }
+        std::basic_string<CharType,Traits> operator() (const std::basic_string<char> &intermediate)
+        {
+            return {reinterpret_cast<const CharType *>(intermediate.c_str()), intermediate.size()};
+        }
+    };
     template<typename T>
-    static std::basic_string<CharType> format(const T& val)
+    static std::basic_string<CharType, Traits> format(const T& val)
     {
-        std::basic_ostringstream<CharType> ss;
+        typename std::conditional<
+            sizeof(CharType) == 1,
+            std::basic_ostringstream<char>,
+            std::basic_ostringstream<typename std::make_signed<CharType>::type>
+        >::type ss;
+        SanitizeInput sanitizer;
+        ss << sanitizer(val);
+        typename std::conditional<
+            sizeof(CharType) == 1,
+            std::basic_string<char>,
+            std::basic_string<typename std::make_signed<CharType>::type>
+        >::type str = ss.str();
+        GenerateFormatOutput generateFormatOutput;
+        return generateFormatOutput(std::move(str));
+    }
+};
+
+template<class Traits, typename T>
+struct Value2StringFormatterUint8Format
+{
+    std::basic_string<uint8_t, Traits> operator () (const T& val)
+    {
+        std::basic_ostringstream<char> ss;
         ss << val;
-        return ss.str();
+        return reinterpret_cast<const uint8_t*>(ss.str().c_str());
+    }
+};
+
+template <class Traits>
+struct Value2StringFormatterUint8Format<Traits, std::basic_string<uint8_t,Traits>>
+{
+    std::basic_string<uint8_t, Traits> operator () (
+        const std::basic_string<uint8_t, typename utility::CanUseStdCharTraits<uint8_t>::TraitsType>& val)
+    {
+        Value2StringFormatterUint8Format<Traits,std::basic_string<char>> format;
+        return format(reinterpret_cast<const std::basic_string<char>&>(val));
     }
 };
 
 template<>
 struct Value2StringFormatter<uint8_t>
 {
-    template<typename T>
-    static std::basic_string<uint8_t> format(const T& val)
+    template <typename T, class Traits = typename utility::CanUseStdCharTraits<uint8_t>::TraitsType>
+    static std::basic_string<uint8_t, Traits> format(const T& val)
     {
-        std::basic_ostringstream<char> ss;
-        ss << val;
-        return reinterpret_cast<const uint8_t*>(ss.str().c_str());
+        Value2StringFormatterUint8Format<Traits, T> format;
+        return format(val);
     }
 
-    static std::basic_string<uint8_t> format(const utf16string& val)
+    static std::basic_string<uint8_t, typename utility::CanUseStdCharTraits<uint8_t>::TraitsType> format(const utf16string& val)
     {
         return format(utility::conversions::utf16_to_utf8(val));
     }
@@ -262,7 +328,7 @@
     /// Write the specified string to the output stream.
     /// </summary>
     /// <param name="str">Input string.</param>
-    pplx::task<size_t> print(const std::basic_string<CharType>& str) const
+    pplx::task<size_t> print(const std::basic_string<CharType,traits>& str) const
     {
         pplx::task<size_t> result;
         if (!_verify_and_return_task(details::_out_stream_msg, result)) return result;
@@ -273,7 +339,7 @@
         }
         else
         {
-            auto sharedStr = std::make_shared<std::basic_string<CharType>>(str);
+            auto sharedStr = std::make_shared<std::basic_string<CharType,traits>>(str);
             return helper()->m_buffer.putn_nocopy(sharedStr->c_str(), sharedStr->size()).then([sharedStr](size_t size) {
                 return size;
             });
@@ -294,7 +360,7 @@
         if (!_verify_and_return_task(details::_out_stream_msg, result)) return result;
         // TODO in the future this could be improved to have Value2StringFormatter avoid another unnecessary copy
         // by putting the string on the heap before calling the print string overload.
-        return print(details::Value2StringFormatter<CharType>::format(val));
+        return print(details::Value2StringFormatter<CharType, traits>::format(val));
     }
 
     /// <summary>
--- a/Release/include/cpprest/astreambuf.h	(revision d17f091b5a753b33fb455e92b590fc9f4e921119)
+++ b/Release/include/cpprest/astreambuf.h	(revision 4188ad89b2cf2e8de3cc3513adcf400fbfdc5ce7)
@@ -15,6 +15,7 @@
 
 #include "cpprest/asyncrt_utils.h"
 #include "cpprest/details/basic_types.h"
+#include "cpprest/details/char_traits.h"
 #include "pplx/pplxtasks.h"
 #include <atomic>
 #include <cstring>
@@ -56,55 +57,28 @@
 /// <typeparam name="_CharType">
 /// The data type of the basic element of the stream.
 /// </typeparam>
+namespace detail
+{
 template<typename _CharType>
-struct char_traits : std::char_traits<_CharType>
+struct char_traits : utility::CanUseStdCharTraits<_CharType>::TraitsType
 {
     /// <summary>
     /// Some synchronous functions will return this value if the operation
     /// requires an asynchronous call in a given situation.
     /// </summary>
     /// <returns>An <c>int_type</c> value which implies that an asynchronous call is required.</returns>
-    static typename std::char_traits<_CharType>::int_type requires_async()
+    static typename utility::CanUseStdCharTraits<_CharType>::TraitsType::int_type requires_async()
     {
-        return std::char_traits<_CharType>::eof() - 1;
+        return utility::CanUseStdCharTraits<_CharType>::TraitsType::eof() - 1;
     }
+};
+}
+template<typename _CharType> struct char_traits : detail::char_traits<_CharType> {
 };
 #if !defined(_WIN32)
-template<>
-struct char_traits<unsigned char> : private std::char_traits<char>
-{
-public:
-    typedef unsigned char char_type;
-
-    using std::char_traits<char>::eof;
-    using std::char_traits<char>::int_type;
-    using std::char_traits<char>::off_type;
-    using std::char_traits<char>::pos_type;
-
-    static size_t length(const unsigned char* str)
-    {
-        return std::char_traits<char>::length(reinterpret_cast<const char*>(str));
-    }
-
-    static void assign(unsigned char& left, const unsigned char& right) { left = right; }
-    static unsigned char* assign(unsigned char* left, size_t n, unsigned char value)
-    {
-        return reinterpret_cast<unsigned char*>(
-            std::char_traits<char>::assign(reinterpret_cast<char*>(left), n, static_cast<char>(value)));
-    }
-
-    static unsigned char* copy(unsigned char* left, const unsigned char* right, size_t n)
-    {
-        return reinterpret_cast<unsigned char*>(
-            std::char_traits<char>::copy(reinterpret_cast<char*>(left), reinterpret_cast<const char*>(right), n));
-    }
-
-    static unsigned char* move(unsigned char* left, const unsigned char* right, size_t n)
-    {
-        return reinterpret_cast<unsigned char*>(
-            std::char_traits<char>::move(reinterpret_cast<char*>(left), reinterpret_cast<const char*>(right), n));
-    }
-
+template <> struct char_traits<unsigned char> : detail::char_traits<unsigned char> {
+    typedef typename std::char_traits<char>::int_type int_type;
+    static int_type eof() { return std::char_traits<char>::eof(); }
     static int_type requires_async() { return eof() - 1; }
 };
 #endif
new file mode 100644
--- /dev/null	(revision 4188ad89b2cf2e8de3cc3513adcf400fbfdc5ce7)
+++ b/Release/include/cpprest/details/char_traits.h	(revision 4188ad89b2cf2e8de3cc3513adcf400fbfdc5ce7)
@@ -0,0 +1,102 @@
+//
+// Created by sigsegv on 6/28/25.
+//
+
+#ifndef CPPRESTSDK_ROOT_CHAR_TRAITS_H
+#define CPPRESTSDK_ROOT_CHAR_TRAITS_H
+
+#include <type_traits>
+#include <string>
+
+namespace utility {
+
+namespace detail {
+
+template <typename T> struct IntTypeFor {
+    typedef typename std::conditional<std::is_unsigned<T>::value, unsigned long long int, long long int>::type type;
+};
+template <> struct IntTypeFor<char> {
+    typedef typename std::char_traits<char>::int_type type;
+};
+template <> struct IntTypeFor<unsigned char> {
+    typedef typename std::make_unsigned<typename std::char_traits<char>::int_type>::type type;
+};
+
+template <typename T> class DetailCharTraits
+{
+public:
+    using char_type  = T;
+    using int_type   = typename IntTypeFor<T>::type;
+    using off_type   = std::streamoff;
+    using pos_type   = std::streampos;
+    using state_type = mbstate_t;
+
+    static void assign(char_type& r, const char_type& a) noexcept { r = a; }
+    static char_type to_char_type(int_type c) noexcept { return char_type(c); }
+    static int_type  to_int_type(char_type c) noexcept { return c; }
+    static bool eq(char_type a, char_type b) noexcept { return a == b; }
+    static bool lt(char_type a, char_type b) noexcept { return a <  b; }
+    static int compare(const char_type* s1,const char_type* s2,size_t n){
+        for (; n--; ++s1, ++s2) {
+            if (!eq(*s1, *s2))
+                return lt(*s1,*s2)?-1:1;
+        }
+        return 0;
+    }
+    static size_t length(const char_type* s){
+        const char_type* p = s;
+        while (*p)
+            ++p;
+        return size_t(p - s);
+    }
+    static const char_type* find(const char_type* s,size_t n,const char_type& a){
+        for (; n--; ++s)
+        {
+            if (eq(*s, a))
+                return s;
+            return nullptr;
+        }
+    }
+    static char_type* move (char_type* r,const char_type* s,size_t n){
+        return (char_type*)memmove(r, s, n * sizeof(char_type));
+    }
+    static char_type* copy (char_type* r,const char_type* s,size_t n){
+        return (char_type*)memcpy (r, s, n * sizeof(char_type));
+    }
+    static char_type* assign(char_type* r,size_t n,char_type a){
+        if (sizeof(char_type) == 1)
+        {
+            return (char_type*)memset(r, a, n);
+        }
+        else
+        {
+            for (char_type *s = r; n--; ++s)
+            {
+                *s = a;
+            }
+        }
+    }
+    static int_type  eof() noexcept { return ~0u; }
+    static int_type  not_eof(int_type c) noexcept { return c == eof() ? 0 : c; }
+};
+
+template <typename T, typename = bool> struct CanUseStdCharTraits : public std::false_type
+{
+public:
+    typedef DetailCharTraits<T> TraitsType;
+};
+
+template <typename T> struct CanUseStdCharTraits<T, decltype(std::char_traits<T>::eq(std::declval<T>(), std::declval<T>()))> : public std::true_type
+{
+public:
+    typedef std::char_traits<T> TraitsType;
+};
+
+}
+
+template <typename T> struct CanUseStdCharTraits : detail::CanUseStdCharTraits<typename std::remove_const<typename std::remove_reference<T>::type>::type> {
+};
+
+}
+
+#endif // CPPRESTSDK_ROOT_CHAR_TRAITS_H
--- a/Release/tests/functional/streams/memstream_tests.cpp	(revision d17f091b5a753b33fb455e92b590fc9f4e921119)
+++ b/Release/tests/functional/streams/memstream_tests.cpp	(revision 6df13a8c0417ef700c0f164bcd0686ad46f66fd9)
@@ -8,6 +8,7 @@
  *
  * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  ****/
+#include "cpprest/details/char_traits.h"
 #include "stdafx.h"
 #if defined(__cplusplus_winrt)
 #include <wrl.h>
@@ -32,7 +33,7 @@
 {
     VERIFY_IS_TRUE(wbuf.can_write());
 
-    std::basic_string<typename StreamBufferType::char_type> s;
+    std::basic_string<typename StreamBufferType::char_type,typename utility::CanUseStdCharTraits<typename StreamBufferType::char_type>::TraitsType> s;
     s.push_back((typename StreamBufferType::char_type)0);
     s.push_back((typename StreamBufferType::char_type)1);
     s.push_back((typename StreamBufferType::char_type)2);
@@ -137,7 +138,7 @@
 {
     VERIFY_IS_TRUE(wbuf.can_write());
 
-    std::basic_string<typename StreamBufferType::char_type> s;
+    std::basic_string<typename StreamBufferType::char_type,typename utility::CanUseStdCharTraits<typename StreamBufferType::char_type>::TraitsType> s;
     s.push_back((typename StreamBufferType::char_type)0);
     s.push_back((typename StreamBufferType::char_type)1);
     s.push_back((typename StreamBufferType::char_type)2);
@@ -169,7 +170,7 @@
 
     typedef concurrency::streams::rawptr_buffer<CharType> StreamBufferType;
 
-    std::basic_string<CharType> s;
+    std::basic_string<CharType,typename CanUseStdCharTraits<CharType>::TraitsType> s;
     s.push_back((CharType)0);
     s.push_back((CharType)1);
     s.push_back((CharType)2);
@@ -198,7 +199,7 @@
     typedef concurrency::streams::container_buffer<CollectionType> StreamBufferType;
     typedef typename concurrency::streams::container_buffer<CollectionType>::char_type CharType;
 
-    std::basic_string<CharType> s;
+    std::basic_string<CharType, typename utility::CanUseStdCharTraits<CharType>::TraitsType> s;
     s.push_back((CharType)0);
     s.push_back((CharType)1);
     s.push_back((CharType)2);
@@ -553,7 +554,7 @@
     VERIFY_IS_TRUE(rwbuf.can_read());
     VERIFY_IS_TRUE(rwbuf.can_write());
     VERIFY_IS_FALSE(rwbuf.is_eof());
-    std::basic_string<typename StreamBufferType::char_type> s;
+    std::basic_string<typename StreamBufferType::char_type, typename utility::CanUseStdCharTraits<typename StreamBufferType::char_type>::TraitsType> s;
     s.push_back((typename StreamBufferType::char_type)0);
     s.push_back((typename StreamBufferType::char_type)1);
     s.push_back((typename StreamBufferType::char_type)2);
@@ -684,7 +685,7 @@
     VERIFY_IS_TRUE(rwbuf.can_write());
 
     // Write 4 characters
-    std::basic_string<typename StreamBufferType::char_type> s;
+    std::basic_string<typename StreamBufferType::char_type, typename utility::CanUseStdCharTraits<typename StreamBufferType::char_type>::TraitsType> s;
     s.push_back((typename StreamBufferType::char_type)0);
     s.push_back((typename StreamBufferType::char_type)1);
     s.push_back((typename StreamBufferType::char_type)2);
@@ -726,7 +727,7 @@
     VERIFY_IS_TRUE(rwbuf.can_write());
 
     // Write 4 characters
-    std::basic_string<typename StreamBufferType::char_type> s;
+    std::basic_string<typename StreamBufferType::char_type, typename utility::CanUseStdCharTraits<typename StreamBufferType::char_type>::TraitsType> s;
     s.push_back((typename StreamBufferType::char_type)0);
     s.push_back((typename StreamBufferType::char_type)1);
     s.push_back((typename StreamBufferType::char_type)2);
--- a/Release/tests/functional/streams/stdstream_tests.cpp	(revision d17f091b5a753b33fb455e92b590fc9f4e921119)
+++ b/Release/tests/functional/streams/stdstream_tests.cpp	(revision 6df13a8c0417ef700c0f164bcd0686ad46f66fd9)
@@ -13,6 +13,7 @@
 #include "cpprest/filestream.h"
 #include "cpprest/producerconsumerstream.h"
 #include "cpprest/rawptrstream.h"
+#include "cpprest/details/char_traits.h"
 
 #if (!defined(_WIN32) || !defined(CPPREST_EXCLUDE_WEBSOCKETS)) && !defined(__cplusplus_winrt)
 #include <boost/interprocess/streams/bufferstream.hpp>
@@ -303,7 +304,8 @@
 
         const std::streamsize iterations = 100;
 
-        const std::string the_alphabet("abcdefghijklmnopqrstuvwxyz");
+        const char *the_alphabet_characters = "abcdefghijklmnopqrstuvwxyz";
+        const std::basic_string<uint8_t,typename utility::CanUseStdCharTraits<uint8_t>::TraitsType> the_alphabet(reinterpret_cast<const uint8_t *>(the_alphabet_characters));
 
         auto writer = pplx::create_task([ostream, iterations, the_alphabet]() {
             auto os = ostream;
@@ -341,7 +343,8 @@
 
         const std::streamsize iterations = 100;
 
-        const std::string the_alphabet("abcdefghijklmnopqrstuvwxyz");
+        const char *the_alphabet_chars = "abcdefghijklmnopqrstuvwxyz";
+        const std::basic_string<uint8_t,typename CanUseStdCharTraits<uint8_t>::TraitsType> the_alphabet(reinterpret_cast<const uint8_t *>(the_alphabet_chars));
 
         auto writer = pplx::create_task([ostream, iterations, the_alphabet]() {
             auto os = ostream;