Skip to content

Commit

Permalink
iox-#1942 Use ascii constexpr and rename variables for better semanti…
Browse files Browse the repository at this point in the history
…c context

Signed-off-by: Christian Eltzschig <[email protected]>
  • Loading branch information
elfenpiff committed Apr 3, 2023
1 parent 59e29aa commit 404204d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
1 change: 1 addition & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- Implement SemanticString as base class for strong string types. [\#1942](https://github.com/eclipse-iceoryx/iceoryx/issues/1942)
- Implement UserName as strong string type to represent posix user names. [\#1942](https://github.com/eclipse-iceoryx/iceoryx/issues/1942)
- Implement FileName, GroupName, Path, FilePath as strong string types. [\#1942](https://github.com/eclipse-iceoryx/iceoryx/issues/1942)
- Add string::unchecked_at to access character without bound checks. [\#1942](https://github.com/eclipse-iceoryx/iceoryx/issues/1942)

**Bugfixes:**

Expand Down
12 changes: 7 additions & 5 deletions iceoryx_hoofs/posix/vocabulary/source/file_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "iox/file_name.hpp"
#include "iox/filesystem.hpp"

namespace iox
{
Expand All @@ -27,12 +28,13 @@ bool file_name_does_contain_invalid_characters(const string<platform::IOX_MAX_FI
for (uint64_t i{0}; i < valueSize; ++i)
{
// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{value[i]};
const char c{value.unchecked_at(i)};

const bool isSmallLetter{'a' <= c && c <= 'z'};
const bool isCapitalLetter{'A' <= c && c <= 'Z'};
const bool isNumber{'0' <= c && c <= '9'};
const bool isSpecialCharacter{c == '-' || c == '.' || c == ':' || c == '_'};
const bool isSmallLetter{internal::ASCII_A <= c && c <= internal::ASCII_Z};
const bool isCapitalLetter{internal::ASCII_CAPITAL_A <= c && c <= internal::ASCII_CAPITAL_Z};
const bool isNumber{internal::ASCII_0 <= c && c <= internal::ASCII_9};
const bool isSpecialCharacter{c == internal::ASCII_DASH || c == internal::ASCII_DOT
|| c == internal::ASCII_COLON || c == internal::ASCII_UNDERSCORE};

if ((!isSmallLetter && !isCapitalLetter) && (!isNumber && !isSpecialCharacter))
{
Expand Down
12 changes: 7 additions & 5 deletions iceoryx_hoofs/posix/vocabulary/source/file_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ bool file_path_does_contain_invalid_characters(const string<platform::IOX_MAX_PA
for (uint64_t i{0}; i < valueSize; ++i)
{
// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{value[i]};
const char c{value.unchecked_at(i)};

const bool isSmallLetter{internal::ASCII_A <= c && c <= internal::ASCII_Z};
const bool isCapitalLetter{internal::ASCII_CAPITAL_A <= c && c <= internal::ASCII_CAPITAL_Z};
const bool isNumber{internal::ASCII_0 <= c && c <= internal::ASCII_9};
const bool isSpecialCharacter{c == internal::ASCII_DASH || c == internal::ASCII_DOT
|| c == internal::ASCII_COLON || c == internal::ASCII_UNDERSCORE};

const bool isSmallLetter{'a' <= c && c <= 'z'};
const bool isCapitalLetter{'A' <= c && c <= 'Z'};
const bool isNumber{'0' <= c && c <= '9'};
const bool isSpecialCharacter{c == '-' || c == '.' || c == ':' || c == '_'};
const bool isPathSeparator{[&] {
for (const auto separator : platform::IOX_PATH_SEPARATORS)
{
Expand Down
19 changes: 10 additions & 9 deletions iceoryx_hoofs/posix/vocabulary/source/group_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_platform/platform_settings.hpp"
#include "iox/filesystem.hpp"
#include "iox/string.hpp"

namespace iox
Expand All @@ -25,9 +26,12 @@ bool group_name_does_contain_invalid_characters(const string<platform::MAX_GROUP
{
for (uint64_t i = 0; i < value.size(); ++i)
{
const bool contains_a_to_z = 'a' <= value[i] && value[i] <= 'z';
const bool contains_0_to_9 = '0' <= value[i] && value[i] <= '9';
const bool contains_dash = value[i] == '-';
// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{value.unchecked_at(i)};

const bool contains_a_to_z = internal::ASCII_A <= c && c <= internal::ASCII_Z;
const bool contains_0_to_9 = internal::ASCII_0 <= c && c <= internal::ASCII_9;
const bool contains_dash = c == internal::ASCII_DASH;

if (!contains_a_to_z && !contains_0_to_9 && !contains_dash)
{
Expand All @@ -46,13 +50,10 @@ bool group_name_does_contain_invalid_content(const string<platform::MAX_GROUP_NA
return true;
}

// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{value.unchecked_at(0)};
// a group name is not allowed to start with a number or dash
if (value[0] == '-' || ('0' <= value[0] && value[0] <= '9'))
{
return true;
}

return false;
return (c == internal::ASCII_DASH || (internal::ASCII_0 <= c && c <= internal::ASCII_9));
}
} // namespace details
} // namespace iox
19 changes: 10 additions & 9 deletions iceoryx_hoofs/posix/vocabulary/source/user_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_platform/platform_settings.hpp"
#include "iox/filesystem.hpp"
#include "iox/string.hpp"

namespace iox
Expand All @@ -25,9 +26,12 @@ bool user_name_does_contain_invalid_characters(const string<platform::MAX_USER_N
{
for (uint64_t i = 0; i < value.size(); ++i)
{
const bool contains_a_to_z = 'a' <= value[i] && value[i] <= 'z';
const bool contains_0_to_9 = '0' <= value[i] && value[i] <= '9';
const bool contains_dash = value[i] == '-';
// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{value.unchecked_at(i)};

const bool contains_a_to_z = internal::ASCII_A <= c && c <= internal::ASCII_Z;
const bool contains_0_to_9 = internal::ASCII_0 <= c && c <= internal::ASCII_9;
const bool contains_dash = c == internal::ASCII_DASH;

if (!contains_a_to_z && !contains_0_to_9 && !contains_dash)
{
Expand All @@ -46,13 +50,10 @@ bool user_name_does_contain_invalid_content(const string<platform::MAX_USER_NAME
return true;
}

// AXIVION Next Construct AutosarC++19_03-A3.9.1: Not used as an integer but as actual character
const char c{value.unchecked_at(0)};
// a user name is not allowed to start with a number or dash
if (value[0] == '-' || ('0' <= value[0] && value[0] <= '9'))
{
return true;
}

return false;
return (c == internal::ASCII_DASH || (internal::ASCII_0 <= c && c <= internal::ASCII_9));
}
} // namespace details
} // namespace iox
30 changes: 16 additions & 14 deletions iceoryx_hoofs/test/moduletests/test_vocabulary_semantic_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ struct TestValues
static const std::string GREATER_VALID_VALUE;
static const std::string SMALLER_VALID_VALUE;
static const std::string MAX_CAPACITY_VALUE;
static const std::vector<std::string> INVALID_CONTENT_ADD_BEGIN;
static const std::vector<std::string> INVALID_CONTENT_ADD_END;
static const std::vector<std::string> ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN;
static const std::vector<std::string> ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END;
};

///////////////////
Expand All @@ -71,9 +71,10 @@ const std::string TestValues<UserName>::SMALLER_VALID_VALUE{"alfons-alf"};
template <>
const std::string TestValues<UserName>::MAX_CAPACITY_VALUE{"all-glory-to-the-incredible-and-legendary-hypno-toad"};
template <>
const std::vector<std::string> TestValues<UserName>::INVALID_CONTENT_ADD_BEGIN{"-bla", "81923"};
const std::vector<std::string> TestValues<UserName>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN{"-bla",
"81923"};
template <>
const std::vector<std::string> TestValues<UserName>::INVALID_CONTENT_ADD_END{};
const std::vector<std::string> TestValues<UserName>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END{};
///////////////////
// END: UserName
///////////////////
Expand All @@ -99,9 +100,10 @@ const std::string TestValues<GroupName>::SMALLER_VALID_VALUE{"alfons-alf"};
template <>
const std::string TestValues<GroupName>::MAX_CAPACITY_VALUE{"all-glory-to-the-incredible-and-legendary-hypno-toad"};
template <>
const std::vector<std::string> TestValues<GroupName>::INVALID_CONTENT_ADD_BEGIN{"-fuu", "8number"};
const std::vector<std::string> TestValues<GroupName>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN{"-fuu",
"8number"};
template <>
const std::vector<std::string> TestValues<GroupName>::INVALID_CONTENT_ADD_END{};
const std::vector<std::string> TestValues<GroupName>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END{};
///////////////////
// END: GroupName
///////////////////
Expand Down Expand Up @@ -129,9 +131,9 @@ const std::string TestValues<FileName>::SMALLER_VALID_VALUE{"0.me.too.be.file"};
template <>
const std::string TestValues<FileName>::MAX_CAPACITY_VALUE{std::string(platform::IOX_MAX_FILENAME_LENGTH, 'b')};
template <>
const std::vector<std::string> TestValues<FileName>::INVALID_CONTENT_ADD_BEGIN{};
const std::vector<std::string> TestValues<FileName>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN{};
template <>
const std::vector<std::string> TestValues<FileName>::INVALID_CONTENT_ADD_END{};
const std::vector<std::string> TestValues<FileName>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END{};
///////////////////
// END: FileName
///////////////////
Expand Down Expand Up @@ -172,9 +174,9 @@ const std::string TestValues<FilePath>::SMALLER_VALID_VALUE{"0.me.too.be.file"};
template <>
const std::string TestValues<FilePath>::MAX_CAPACITY_VALUE{std::string(platform::IOX_MAX_PATH_LENGTH, 'b')};
template <>
const std::vector<std::string> TestValues<FilePath>::INVALID_CONTENT_ADD_BEGIN{};
const std::vector<std::string> TestValues<FilePath>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN{};
template <>
const std::vector<std::string> TestValues<FilePath>::INVALID_CONTENT_ADD_END{};
const std::vector<std::string> TestValues<FilePath>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END{};
///////////////////
// END: FilePath
///////////////////
Expand Down Expand Up @@ -217,9 +219,9 @@ const std::string TestValues<Path>::SMALLER_VALID_VALUE{"0.me.too.be.file/whoop/
template <>
const std::string TestValues<Path>::MAX_CAPACITY_VALUE{std::string(platform::IOX_MAX_PATH_LENGTH, 'b')};
template <>
const std::vector<std::string> TestValues<Path>::INVALID_CONTENT_ADD_BEGIN{};
const std::vector<std::string> TestValues<Path>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN{};
template <>
const std::vector<std::string> TestValues<Path>::INVALID_CONTENT_ADD_END{};
const std::vector<std::string> TestValues<Path>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END{};
///////////////////
// END: Path
///////////////////
Expand Down Expand Up @@ -427,7 +429,7 @@ TYPED_TEST(SemanticString_test, GenerateInvalidContentWithAppend)

for (auto& value : TestValues<SutType>::VALID_VALUES)
{
for (auto& invalid_value : TestValues<SutType>::INVALID_CONTENT_ADD_END)
for (auto& invalid_value : TestValues<SutType>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_END)
{
auto sut = SutType::create(string<SutType::capacity()>(TruncateToCapacity, value.c_str()));
ASSERT_THAT(sut.has_error(), Eq(false));
Expand All @@ -450,7 +452,7 @@ TYPED_TEST(SemanticString_test, GenerateInvalidContentWithInsert)

for (auto& value : TestValues<SutType>::VALID_VALUES)
{
for (auto& invalid_value : TestValues<SutType>::INVALID_CONTENT_ADD_BEGIN)
for (auto& invalid_value : TestValues<SutType>::ADD_VALID_CHARS_TO_CREATE_INVALID_CONTENT_AT_BEGIN)
{
auto sut = SutType::create(string<SutType::capacity()>(TruncateToCapacity, value.c_str()));
ASSERT_THAT(sut.has_error(), Eq(false));
Expand Down

0 comments on commit 404204d

Please sign in to comment.