Skip to content

Commit

Permalink
[fix] csubstr::compare(char): refactor to avoid warning from VS2022
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Aug 25, 2022
1 parent d89c7f2 commit b8f2110
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/current.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
- Always use the fastest implementation available: `std::from_chars()` if available (C++17 or higher standard, with later compilers), `fast_float::from_chars()` otherwise. On Visual Studio, `fast_float::from_chars()` is preferred over `std::from_chars()`.
- If `std::from_chars()` is not available and `C4CORE_NO_FAST_FLOAT` is defined, then the fallback is based on `sscanf()`.
- Ensure hexadecimal floats are accepted. The current fast_float implementation does not accept hexadecimal floats, so an hexfloat scanner was added.
- Fix [#84](https://github.com/biojppm/c4core/issues/84): `csubstr::compare(char)`: refactor to avoid false-positive warning from VS2022.
9 changes: 4 additions & 5 deletions src/c4/substr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,10 @@ struct C4CORE_EXPORT basic_substring
int compare(C const c) const
{
C4_XASSERT((str != nullptr) || len == 0);
if( ! len)
if(C4_LIKELY(str != nullptr && len > 0))
return (*str != c) ? *str - c : (static_cast<int>(len) - 1);
else
return -1;
if(*str == c)
return static_cast<int>(len - 1);
return *str - c;
}

int compare(const char *that, size_t sz) const
Expand All @@ -241,7 +240,7 @@ struct C4CORE_EXPORT basic_substring
ret = len < sz ? -1 : 1;
return ret;
}
if((!str && !that) || (len == sz))
else if((!str && !that) || (len == sz))
{
C4_XASSERT(len == 0 && sz == 0);
return 0;
Expand Down
4 changes: 4 additions & 0 deletions test/test_substr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,10 @@ TEST_CASE("substr.compare_null")

TEST_CASE("substr.compare_vs_char")
{
CHECK_EQ(csubstr().compare('1'), -1); // str==null, len==0
CHECK_EQ(csubstr("0123").first(0).compare('1'), -1); // str!=null, len==0
CHECK_EQ(csubstr("0123").first(1).compare('1'), -1);

CHECK_EQ(csubstr("-"), '-');
CHECK_NE(csubstr("+"), '-');

Expand Down

0 comments on commit b8f2110

Please sign in to comment.