Report benchmark durations in natural units

(and extended StringRef to be able to report utf8 char lengths
This commit is contained in:
Phil Nash
2017-08-05 21:41:56 +01:00
parent 4421672fb8
commit 519db85758
5 changed files with 106 additions and 8 deletions

View File

@@ -64,6 +64,9 @@ namespace Catch {
auto String::size() const noexcept -> size_type {
return m_data->size;
}
auto String::numberOfCharacters() const noexcept -> size_type {
return StringRef( *this ).numberOfCharacters();
}
auto String::c_str() const noexcept -> char const* {
return m_data->chars;
}

View File

@@ -40,6 +40,8 @@ namespace Catch {
auto empty() const noexcept -> bool;
auto size() const noexcept -> size_type;
auto numberOfCharacters() const noexcept -> size_type;
auto c_str() const noexcept -> char const*;
};

View File

@@ -136,7 +136,23 @@ namespace Catch {
auto StringRef::size() const noexcept -> size_type {
return m_size;
}
auto StringRef::numberOfCharacters() const noexcept -> size_type {
size_type noChars = m_size;
// Make adjustments for uft encodings
for( size_type i=0; i < m_size; ++i ) {
char c = m_start[i];
if( ( c & 0b11000000 ) == 0b11000000 ) {
if( ( c & 0b11100000 ) == 0b11000000 )
noChars--;
else if( ( c & 0b11110000 ) == 0b11100000 )
noChars-=2;
else if( ( c & 0b11111000 ) == 0b11110000 )
noChars-=3;
}
}
return noChars;
}
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> String {
StringBuilder buf;
buf.reserve( lhs.size() + rhs.size() );

View File

@@ -56,6 +56,7 @@ namespace Catch {
public: // named queries
auto empty() const noexcept -> bool;
auto size() const noexcept -> size_type;
auto numberOfCharacters() const noexcept -> size_type;
auto c_str() const -> char const*;
public: // substrings and searches