mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-16 00:46:03 +08:00
Optimisation of strings
This commit is contained in:
parent
8fb4090e75
commit
31caeec248
@ -883,99 +883,6 @@ namespace etl
|
||||
|
||||
etl::transform(itr, s.end(), itr, ::tolower);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// str_n_copy
|
||||
/// Copies from src to dst until either n characters have been copied, or a
|
||||
/// terminating null is found.
|
||||
/// Null terminates the destination if less than n characters have been copied.
|
||||
/// Returns a str_n_copy_result.
|
||||
//***************************************************************************
|
||||
struct str_n_copy_result
|
||||
{
|
||||
size_t count;
|
||||
bool truncated;
|
||||
bool terminated;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
str_n_copy_result str_n_copy(const T* src, size_t n, T* dst)
|
||||
{
|
||||
if ((src == ETL_NULLPTR) || (dst == ETL_NULLPTR))
|
||||
{
|
||||
return { 0, false, false };
|
||||
}
|
||||
|
||||
size_t count = 0;
|
||||
|
||||
#if defined(__GNUC__) || defined(_MSC_VER)
|
||||
// Use restrict if available, for better optimization
|
||||
const T* __restrict s = src;
|
||||
T* __restrict d = dst;
|
||||
#else
|
||||
const T* s = src;
|
||||
T* d = dst;
|
||||
#endif
|
||||
|
||||
// Copy in blocks of 4 for loop unrolling
|
||||
count += 4;
|
||||
while (count <= n)
|
||||
{
|
||||
if (s[0] == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
d[0] = s[0];
|
||||
|
||||
if (s[1] == 0)
|
||||
{
|
||||
count += 1;
|
||||
d[1] = 0;
|
||||
return { count, false, true };
|
||||
}
|
||||
|
||||
d[1] = s[1];
|
||||
|
||||
if (s[2] == 0)
|
||||
{
|
||||
count += 2; d[2] = 0;
|
||||
return { count, false, true };
|
||||
}
|
||||
|
||||
d[2] = s[2];
|
||||
|
||||
if (s[3] == 0)
|
||||
{
|
||||
count += 3;
|
||||
d[3] = 0;
|
||||
return { count, false, true };
|
||||
}
|
||||
|
||||
d[3] = s[3];
|
||||
s += 4;
|
||||
d += 4;
|
||||
count += 4;
|
||||
}
|
||||
|
||||
// Copy remaining
|
||||
while ((count < n) && (*s != 0))
|
||||
{
|
||||
*d++ = *s++;
|
||||
++count;
|
||||
}
|
||||
|
||||
//
|
||||
if (count <= n)
|
||||
{
|
||||
*d = 0;
|
||||
return { count, false, true };
|
||||
}
|
||||
else
|
||||
{
|
||||
return { count, *s != 0, false };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#include "private/minmax_pop.h"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user