Skip to content

Commit 7574c3b

Browse files
Added generalized upstream fix for wcslen() in basic_string.h
1 parent 1bedac5 commit 7574c3b

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

libs/3rdparty/etl/include/etl/basic_string.h

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ SOFTWARE.
3535
#include "algorithm.h"
3636
#include "iterator.h"
3737
#include "functional.h"
38-
#include "char_traits.h"
3938
#include "alignment.h"
4039
#include "array.h"
4140
#include "type_traits.h"
@@ -51,6 +50,18 @@ SOFTWARE.
5150
#include <stdint.h>
5251
#include <string.h>
5352

53+
#if defined(__has_include)
54+
#if __has_include(<wchar.h>)
55+
#define ETL_USING_WCHAR_T_H 1
56+
#include <wchar.h>
57+
#else
58+
#define ETL_USING_WCHAR_T_H 0
59+
#endif
60+
#else
61+
#define ETL_USING_WCHAR_T_H 1
62+
#include <wchar.h>
63+
#endif
64+
5465
#if ETL_USING_STL && ETL_USING_CPP17
5566
#include <string_view>
5667
#endif
@@ -2696,28 +2707,6 @@ namespace etl
26962707
return to;
26972708
}
26982709

2699-
//*********************************************************************
2700-
/// get_string_length, optimised for sizeof(U) == sizeof(char).
2701-
//*********************************************************************
2702-
template <typename U>
2703-
static
2704-
typename etl::enable_if<sizeof(U) == sizeof(char), size_t>::type
2705-
get_string_length(const U* src)
2706-
{
2707-
return ::strlen(reinterpret_cast<const char*>(src));
2708-
}
2709-
2710-
//*********************************************************************
2711-
/// get_string_length, optimised for anything else.
2712-
//*********************************************************************
2713-
template <typename U>
2714-
static
2715-
typename etl::enable_if<(sizeof(U) != sizeof(char)), size_t>::type
2716-
get_string_length(const U* src)
2717-
{
2718-
return etl::strlen(src);
2719-
}
2720-
27212710
//*********************************************************************
27222711
/// Common implementation for 'assign' and 'append' for iterators.
27232712
//*********************************************************************
@@ -2848,6 +2837,57 @@ namespace etl
28482837
return size() - sz - etl::distance(rbegin(), iposition);
28492838
}
28502839
}
2840+
2841+
//*********************************************************************
2842+
/// get_string_length, optimised for sizeof(U) == sizeof(char).
2843+
//*********************************************************************
2844+
template <typename U>
2845+
static
2846+
typename etl::enable_if<sizeof(U) == sizeof(char), size_t>::type
2847+
get_string_length(const U* str)
2848+
{
2849+
return ::strlen(reinterpret_cast<const char*>(str));
2850+
}
2851+
2852+
#if ETL_USING_WCHAR_T_H
2853+
//*********************************************************************
2854+
/// get_string_length, optimised for sizeof(U) == sizeof(wchar_t).
2855+
//*********************************************************************
2856+
template <typename U>
2857+
static
2858+
typename etl::enable_if<sizeof(U) == sizeof(wchar_t), size_t>::type
2859+
get_string_length(const U* str)
2860+
{
2861+
return ::wcslen(reinterpret_cast<const wchar_t*>(str));
2862+
}
2863+
#endif
2864+
2865+
//*********************************************************************
2866+
/// get_string_length, optimised for anything else.
2867+
//*********************************************************************
2868+
template <typename U>
2869+
static
2870+
#if ETL_USING_WCHAR_T_H
2871+
typename etl::enable_if<(sizeof(U) != sizeof(char)) && (sizeof(U) != sizeof(wchar_t)), size_t>::type
2872+
#else
2873+
typename etl::enable_if<(sizeof(U) != sizeof(char)), size_t>::type
2874+
#endif
2875+
get_string_length(const U* str)
2876+
{
2877+
if (str == ETL_NULLPTR)
2878+
{
2879+
return 0;
2880+
}
2881+
2882+
const U* end = str;
2883+
2884+
while (*end++ != 0)
2885+
{
2886+
// Do nothing.
2887+
}
2888+
2889+
return size_t(end - str) - 1;
2890+
}
28512891
};
28522892

28532893
//***************************************************************************
@@ -3105,6 +3145,8 @@ namespace etl
31053145
#endif
31063146
}
31073147

3148+
#undef ETL_USING_WCHAR_T_H
3149+
31083150
#include "private/minmax_pop.h"
31093151

31103152
#endif

0 commit comments

Comments
 (0)