From a1367fc05f1a7bc176961d239de78c08ddeaac27 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 4 Jun 2025 20:05:41 +0200 Subject: [PATCH] Conditionally use wchar.h (#1115) --- include/etl/basic_string.h | 90 +++++++++++++++++++++++--------------- include/etl/platform.h | 10 +++++ 2 files changed, 65 insertions(+), 35 deletions(-) diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index bae3d516..a5470e40 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -35,7 +35,6 @@ SOFTWARE. #include "algorithm.h" #include "iterator.h" #include "functional.h" -#include "char_traits.h" #include "alignment.h" #include "array.h" #include "type_traits.h" @@ -50,7 +49,10 @@ SOFTWARE. #include #include #include -#include + +#if ETL_USING_LIBC_WCHAR_H + #include +#endif #if ETL_USING_STL && ETL_USING_CPP17 #include @@ -2697,39 +2699,6 @@ namespace etl return to; } - //********************************************************************* - /// get_string_length, optimised for sizeof(U) == sizeof(char). - //********************************************************************* - template - static - typename etl::enable_if::type - get_string_length(const U* src) - { - return ::strlen(reinterpret_cast(src)); - } - - //********************************************************************* - /// get_string_length, optimised for sizeof(U) == sizeof(wchar_t). - //********************************************************************* - template - static - typename etl::enable_if::type - get_string_length(const U* src) - { - return ::wcslen(reinterpret_cast(src)); - } - - //********************************************************************* - /// get_string_length, optimised for anything else. - //********************************************************************* - template - static - typename etl::enable_if<(sizeof(U) != sizeof(char)) && (sizeof(U) != sizeof(wchar_t)), size_t>::type - get_string_length(const U* src) - { - return etl::strlen(src); - } - //********************************************************************* /// Common implementation for 'assign' and 'append' for iterators. //********************************************************************* @@ -2860,6 +2829,57 @@ namespace etl return size() - sz - etl::distance(rbegin(), iposition); } } + + //********************************************************************* + /// get_string_length, optimised for sizeof(U) == sizeof(char). + //********************************************************************* + template + static + typename etl::enable_if::type + get_string_length(const U* str) + { + return ::strlen(reinterpret_cast(str)); + } + +#if ETL_USING_LIBC_WCHAR_H + //********************************************************************* + /// get_string_length, optimised for sizeof(U) == sizeof(wchar_t). + //********************************************************************* + template + static + typename etl::enable_if::type + get_string_length(const U* str) + { + return ::wcslen(reinterpret_cast(str)); + } +#endif + + //********************************************************************* + /// get_string_length, optimised for anything else. + //********************************************************************* + template + static +#if ETL_USING_LIBC_WCHAR_H + typename etl::enable_if<(sizeof(U) != sizeof(char)) && (sizeof(U) != sizeof(wchar_t)), size_t>::type +#else + typename etl::enable_if<(sizeof(U) != sizeof(char)), size_t>::type +#endif + get_string_length(const U* str) + { + if (str == ETL_NULLPTR) + { + return 0; + } + + const U* end = str; + + while (*end++ != 0) + { + // Do nothing. + } + + return size_t(end - str) - 1; + } }; //*************************************************************************** diff --git a/include/etl/platform.h b/include/etl/platform.h index 045a59c1..7edc017a 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -449,6 +449,16 @@ SOFTWARE. #endif #endif +//************************************* +// Determine if the ETL can use libc's wchar.h +#if defined(ETL_NO_LIBC_WCHAR_H) + #define ETL_USING_LIBC_WCHAR_H 0 + #define ETL_NOT_USING_LIBC_WCHAR_H 1 +#else + #define ETL_USING_LIBC_WCHAR_H 1 + #define ETL_NOT_USING_LIBC_WCHAR_H 0 +#endif + //************************************* // Determine if the ETL should support atomics. #if defined(ETL_NO_ATOMICS) || \