Skip to content

Commit

Permalink
Wrappers: fix toSnakeCase for IPV4
Browse files Browse the repository at this point in the history
Previously, the `toSnakeCase` function used in the Wrappers would
convert `IPV4` to `ip_v4`. This conflicts with the `_to_snake_case`
function used in the datamodel code generator, which would return
`ipv4`. The latter is the version used in the Zivid SDK, so this commit
fixes the `toSnakeCase` function so that it uses the latter version.

Previously, the `String::toSnakeCase` function would insert a "_" in the
following two cases:
  1. If an upper case letter is preceded by any character that is not an
     upper case letter.
  2. If an upper case letter is preceded by an upper case letter and
     succeeded by any character that is not an upper case letter.

These rules are a little ambigous due to using the negation of the
`isupper` function. This commit instead uses the `islower` function in
the places where the negation of `isupper` was used.

This new version inserts a "_" in the following two cases:
  1. If an upper case letter is preceded by a lower case letter.
  2. If an upper case letter is preceded by any character that is not a
     lower case letter and is succeeded by a lower case letter.

MISC
  • Loading branch information
johningve committed Nov 29, 2023
1 parent 100fb89 commit 157979e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/include/ZividPython/Wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ namespace ZividPython
throw std::invalid_argument{ "String is empty." };
}

if(!isupper(upperCamelCase[0]))
if(!isupper(upperCamelCase.front()))
{
std::stringstream msg;
msg << "First character of string: '" << upperCamelCase << "' is not capitalized";
throw std::invalid_argument{ msg.str() };
}

std::stringstream ss;
ss << char(tolower(upperCamelCase[0]));
ss << char(std::tolower(upperCamelCase.front()));

for(size_t i = 1; i < upperCamelCase.size(); ++i)
for(auto it = std::next(upperCamelCase.begin()); it != upperCamelCase.end(); it++)
{
if(isupper(upperCamelCase[i]))
if(std::isupper(*it))
{
auto previous = i - 1;
auto next = i + 1;
const auto prev = it - 1;
const auto next = it + 1;

if(!isupper(upperCamelCase[previous])
|| (next < upperCamelCase.size() && !isupper(upperCamelCase[next])))
if(std::islower(*prev) || (next != upperCamelCase.end() && std::islower(*next))
{
ss << "_";
}
Expand Down

0 comments on commit 157979e

Please sign in to comment.