Remove the ill-considered StringRef::numberOfCharacters

It never counted characters, only codepoints. If you think these
are interchangeable, you should not touch non-ascii text.
This commit is contained in:
Martin Hořeňovský 2019-09-07 20:13:22 +02:00
parent f2c2711bdc
commit fe967b1f41
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
9 changed files with 6 additions and 95 deletions

View File

@ -17,12 +17,6 @@
#include <cstring> #include <cstring>
#include <cstdint> #include <cstdint>
namespace {
const uint32_t byte_2_lead = 0xC0;
const uint32_t byte_3_lead = 0xE0;
const uint32_t byte_4_lead = 0xF0;
}
namespace Catch { namespace Catch {
StringRef::StringRef( char const* rawChars ) noexcept StringRef::StringRef( char const* rawChars ) noexcept
: StringRef( rawChars, static_cast<StringRef::size_type>(std::strlen(rawChars) ) ) : StringRef( rawChars, static_cast<StringRef::size_type>(std::strlen(rawChars) ) )
@ -82,22 +76,6 @@ namespace Catch {
return m_start[index]; return m_start[index];
} }
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 & byte_2_lead ) == byte_2_lead ) {
noChars--;
if (( c & byte_3_lead ) == byte_3_lead )
noChars--;
if( ( c & byte_4_lead ) == byte_4_lead )
noChars--;
}
}
return noChars;
}
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string { auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string {
std::string str; std::string str;
str.reserve( lhs.size() + rhs.size() ); str.reserve( lhs.size() + rhs.size() );

View File

@ -96,7 +96,6 @@ namespace Catch {
return m_size; return m_size;
} }
auto numberOfCharacters() const noexcept -> size_type;
auto c_str() const -> char const*; auto c_str() const -> char const*;
public: // substrings and searches public: // substrings and searches

View File

@ -321,8 +321,7 @@ public:
friend TablePrinter& operator << (TablePrinter& tp, ColumnBreak) { friend TablePrinter& operator << (TablePrinter& tp, ColumnBreak) {
auto colStr = tp.m_oss.str(); auto colStr = tp.m_oss.str();
// This takes account of utf8 encodings const auto strSize = colStr.size();
auto strSize = Catch::StringRef(colStr).numberOfCharacters();
tp.m_oss.str(""); tp.m_oss.str("");
tp.open(); tp.open();
if (tp.m_currentColumn == static_cast<int>(tp.m_columnInfos.size() - 1)) { if (tp.m_currentColumn == static_cast<int>(tp.m_columnInfos.size() - 1)) {

View File

@ -1045,9 +1045,6 @@ String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringre
String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11 String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11
String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringref" == "a stringref" String.tests.cpp:<line number>: passed: stdStr == "a stringref" for: "a stringref" == "a stringref"
String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11 String.tests.cpp:<line number>: passed: stdStr.size() == sr.size() for: 11 == 11
String.tests.cpp:<line number>: passed: ascii.numberOfCharacters() == ascii.size() for: 39 == 39
String.tests.cpp:<line number>: passed: simpleu8.numberOfCharacters() == 30 for: 30 == 30
String.tests.cpp:<line number>: passed: emojis.numberOfCharacters() == 9 for: 9 == 9
ToStringChrono.tests.cpp:<line number>: passed: minute == seconds for: 1 m == 60 s ToStringChrono.tests.cpp:<line number>: passed: minute == seconds for: 1 m == 60 s
ToStringChrono.tests.cpp:<line number>: passed: hour != seconds for: 1 h != 60 s ToStringChrono.tests.cpp:<line number>: passed: hour != seconds for: 1 h != 60 s
ToStringChrono.tests.cpp:<line number>: passed: micro != milli for: 1 us != 1 ms ToStringChrono.tests.cpp:<line number>: passed: micro != milli for: 1 us != 1 ms

View File

@ -1381,5 +1381,5 @@ due to unexpected exception with message:
=============================================================================== ===============================================================================
test cases: 300 | 226 passed | 70 failed | 4 failed as expected test cases: 300 | 226 passed | 70 failed | 4 failed as expected
assertions: 1562 | 1410 passed | 131 failed | 21 failed as expected assertions: 1559 | 1407 passed | 131 failed | 21 failed as expected

View File

@ -7837,28 +7837,6 @@ String.tests.cpp:<line number>: PASSED:
with expansion: with expansion:
11 == 11 11 == 11
-------------------------------------------------------------------------------
StringRef
Counting utf-8 codepoints
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( ascii.numberOfCharacters() == ascii.size() )
with expansion:
39 == 39
String.tests.cpp:<line number>: PASSED:
REQUIRE( simpleu8.numberOfCharacters() == 30 )
with expansion:
30 == 30
String.tests.cpp:<line number>: PASSED:
REQUIRE( emojis.numberOfCharacters() == 9 )
with expansion:
9 == 9
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Stringifying std::chrono::duration helpers Stringifying std::chrono::duration helpers
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -12484,5 +12462,5 @@ Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 300 | 210 passed | 86 failed | 4 failed as expected test cases: 300 | 210 passed | 86 failed | 4 failed as expected
assertions: 1579 | 1410 passed | 148 failed | 21 failed as expected assertions: 1576 | 1407 passed | 148 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="17" failures="132" tests="1580" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="132" tests="1577" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties> <properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/>
<property name="random-seed" value="1"/> <property name="random-seed" value="1"/>
@ -729,7 +729,6 @@ Matchers.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/implicitly constructed" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef/to std::string/implicitly constructed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/explicitly constructed" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef/to std::string/explicitly constructed" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/to std::string/assigned" time="{duration}"/> <testcase classname="<exe-name>.global" name="StringRef/to std::string/assigned" time="{duration}"/>
<testcase classname="<exe-name>.global" name="StringRef/Counting utf-8 codepoints" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration helpers" time="{duration}"/> <testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration helpers" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration with weird ratios" time="{duration}"/> <testcase classname="<exe-name>.global" name="Stringifying std::chrono::duration with weird ratios" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Stringifying std::chrono::time_point&lt;system_clock>" time="{duration}"/> <testcase classname="<exe-name>.global" name="Stringifying std::chrono::time_point&lt;system_clock>" time="{duration}"/>

View File

@ -9667,33 +9667,6 @@ Message from section two
</Section> </Section>
<OverallResults successes="2" failures="0" expectedFailures="0"/> <OverallResults successes="2" failures="0" expectedFailures="0"/>
</Section> </Section>
<Section name="Counting utf-8 codepoints" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
ascii.numberOfCharacters() == ascii.size()
</Original>
<Expanded>
39 == 39
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
simpleu8.numberOfCharacters() == 30
</Original>
<Expanded>
30 == 30
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
emojis.numberOfCharacters() == 9
</Original>
<Expanded>
9 == 9
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="Stringifying std::chrono::duration helpers" tags="[chrono][toString]" filename="projects/<exe-name>/UsageTests/ToStringChrono.tests.cpp" > <TestCase name="Stringifying std::chrono::duration helpers" tags="[chrono][toString]" filename="projects/<exe-name>/UsageTests/ToStringChrono.tests.cpp" >
@ -14861,7 +14834,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1410" failures="149" expectedFailures="21"/> <OverallResults successes="1407" failures="149" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="1410" failures="148" expectedFailures="21"/> <OverallResults successes="1407" failures="148" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -159,18 +159,6 @@ TEST_CASE( "StringRef", "[Strings][StringRef]" ) {
REQUIRE( stdStr.size() == sr.size() ); REQUIRE( stdStr.size() == sr.size() );
} }
} }
SECTION( "Counting utf-8 codepoints" ) {
StringRef ascii = "just a plain old boring ascii string...";
REQUIRE(ascii.numberOfCharacters() == ascii.size());
StringRef simpleu8 = u8"Trocha češtiny nikoho nezabila";
REQUIRE(simpleu8.numberOfCharacters() == 30);
StringRef emojis = u8"Here be 👾";
REQUIRE(emojis.numberOfCharacters() == 9);
}
} }
TEST_CASE( "replaceInPlace", "[Strings][StringManip]" ) { TEST_CASE( "replaceInPlace", "[Strings][StringManip]" ) {