Skip to content

Commit

Permalink
Merge pull request #1951 from ApexAI/iox-1942-introduce-further-seman…
Browse files Browse the repository at this point in the history
…tic-string-types

Iox 1942 introduce further semantic string types
  • Loading branch information
elfenpiff authored Apr 4, 2023
2 parents b013052 + 4a00712 commit 85de9bd
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 16 deletions.
2 changes: 2 additions & 0 deletions doc/website/release-notes/iceoryx-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
- Implement PolymorphicHandler [\#1640](https://github.com/eclipse-iceoryx/iceoryx/issues/1640)
- 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
4 changes: 4 additions & 0 deletions iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ iox_add_library(

posix/time/source/adaptive_wait.cpp
posix/time/source/deadline_timer.cpp
posix/vocabulary/source/file_name.cpp
posix/vocabulary/source/file_path.cpp
posix/vocabulary/source/group_name.cpp
posix/vocabulary/source/path.cpp
posix/vocabulary/source/user_name.cpp
)

Expand Down
45 changes: 45 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/include/iox/file_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_POSIX_VOCABULARY_FILE_NAME_HPP
#define IOX_HOOFS_POSIX_VOCABULARY_FILE_NAME_HPP

#include "iox/semantic_string.hpp"

namespace iox
{
namespace details
{
bool file_name_does_contain_invalid_characters(const string<platform::IOX_MAX_FILENAME_LENGTH>& value) noexcept;
bool file_name_does_contain_invalid_content(const string<platform::IOX_MAX_FILENAME_LENGTH>& value) noexcept;
} // namespace details

/// @brief Represents a single file name. It is not allowed to contain any path elements
/// like "./some_file" or "path/to/file". Just a plain old simple "my_file.bla".
class FileName : public SemanticString<FileName,
platform::IOX_MAX_FILENAME_LENGTH,
details::file_name_does_contain_invalid_content,
details::file_name_does_contain_invalid_characters>
{
using Parent = SemanticString<FileName,
platform::IOX_MAX_FILENAME_LENGTH,
details::file_name_does_contain_invalid_content,
details::file_name_does_contain_invalid_characters>;
using Parent::Parent;
};
} // namespace iox

#endif
46 changes: 46 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/include/iox/file_path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_POSIX_VOCABULARY_FILE_PATH_HPP
#define IOX_HOOFS_POSIX_VOCABULARY_FILE_PATH_HPP

#include "iox/semantic_string.hpp"

namespace iox
{
namespace details
{
bool file_path_does_contain_invalid_characters(const string<platform::IOX_MAX_PATH_LENGTH>& value) noexcept;
bool file_path_does_contain_invalid_content(const string<platform::IOX_MAX_PATH_LENGTH>& value) noexcept;
} // namespace details

/// @brief Represents a path to a file. It is not allowed to end with a path separator
/// since this would then be a path to a directory. A valid file path is for
/// instance "path/to/file" but not "path/to/file/".
class FilePath : public SemanticString<FilePath,
platform::IOX_MAX_PATH_LENGTH,
details::file_path_does_contain_invalid_content,
details::file_path_does_contain_invalid_characters>
{
using Parent = SemanticString<FilePath,
platform::IOX_MAX_PATH_LENGTH,
details::file_path_does_contain_invalid_content,
details::file_path_does_contain_invalid_characters>;
using Parent::Parent;
};
} // namespace iox

#endif
45 changes: 45 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/include/iox/group_name.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_POSIX_VOCABULARY_GROUP_NAME_HPP
#define IOX_HOOFS_POSIX_VOCABULARY_GROUP_NAME_HPP

#include "iox/semantic_string.hpp"
#include "iox/user_name.hpp"

namespace iox
{
namespace details
{
bool group_name_does_contain_invalid_characters(const string<platform::MAX_GROUP_NAME_LENGTH>& value) noexcept;
bool group_name_does_contain_invalid_content(const string<platform::MAX_GROUP_NAME_LENGTH>& value) noexcept;
} // namespace details

/// @brief Represents a POSIX group name
class GroupName : public SemanticString<GroupName,
platform::MAX_GROUP_NAME_LENGTH,
details::group_name_does_contain_invalid_content,
details::group_name_does_contain_invalid_characters>
{
using Parent = SemanticString<GroupName,
platform::MAX_GROUP_NAME_LENGTH,
details::group_name_does_contain_invalid_content,
details::group_name_does_contain_invalid_characters>;
using Parent::Parent;
};
} // namespace iox

#endif
44 changes: 44 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/include/iox/path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_HOOFS_POSIX_VOCABULARY_PATH_HPP
#define IOX_HOOFS_POSIX_VOCABULARY_PATH_HPP

#include "iox/file_path.hpp"
#include "iox/semantic_string.hpp"

namespace iox
{
namespace details
{
bool path_does_contain_invalid_content(const string<platform::IOX_MAX_PATH_LENGTH>& value) noexcept;
} // namespace details

/// @brief Represents a path to a file or a directory.
class Path : public SemanticString<Path,
platform::IOX_MAX_PATH_LENGTH,
details::path_does_contain_invalid_content,
details::file_path_does_contain_invalid_characters>
{
using Parent = SemanticString<Path,
platform::IOX_MAX_PATH_LENGTH,
details::path_does_contain_invalid_content,
details::file_path_does_contain_invalid_characters>;
using Parent::Parent;
};
} // namespace iox

#endif
1 change: 1 addition & 0 deletions iceoryx_hoofs/posix/vocabulary/include/iox/user_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bool user_name_does_contain_invalid_characters(const string<platform::MAX_USER_N
bool user_name_does_contain_invalid_content(const string<platform::MAX_USER_NAME_LENGTH>& value) noexcept;
} // namespace details

/// @brief Represents a POSIX user name
class UserName : public SemanticString<UserName,
platform::MAX_USER_NAME_LENGTH,
details::user_name_does_contain_invalid_content,
Expand Down
53 changes: 53 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/source/file_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

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

namespace iox
{
namespace details
{
bool file_name_does_contain_invalid_characters(const string<platform::IOX_MAX_FILENAME_LENGTH>& value) noexcept
{
const auto valueSize = value.size();

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.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};

if ((!isSmallLetter && !isCapitalLetter) && (!isNumber && !isSpecialCharacter))
{
return true;
}
}

return false;
}

bool file_name_does_contain_invalid_content(const string<platform::IOX_MAX_FILENAME_LENGTH>& value) noexcept
{
return (value.empty() || value == "." || value == "..");
}
} // namespace details
} // namespace iox
64 changes: 64 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/source/file_path.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

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

namespace iox
{
namespace details
{
bool file_path_does_contain_invalid_characters(const string<platform::IOX_MAX_PATH_LENGTH>& value) noexcept
{
const auto valueSize = value.size();

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.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 isPathSeparator{[&] {
for (const auto separator : platform::IOX_PATH_SEPARATORS)
{
if (c == separator)
{
return true;
}
}
return false;
}()};

if ((!isSmallLetter && !isCapitalLetter) && (!isNumber && !isSpecialCharacter) && !isPathSeparator)
{
return true;
}
}

return false;
}

bool file_path_does_contain_invalid_content(const string<platform::IOX_MAX_PATH_LENGTH>& value) noexcept
{
return !isValidPathToFile(value);
}
} // namespace details
} // namespace iox
59 changes: 59 additions & 0 deletions iceoryx_hoofs/posix/vocabulary/source/group_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

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

namespace iox
{
namespace details
{
bool group_name_does_contain_invalid_characters(const string<platform::MAX_GROUP_NAME_LENGTH>& value) noexcept
{
for (uint64_t i = 0; i < value.size(); ++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)
{
return true;
}
}

return false;
}

bool group_name_does_contain_invalid_content(const string<platform::MAX_GROUP_NAME_LENGTH>& value) noexcept
{
// group name is not allowed to be empty
if (value.empty())
{
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
return (c == internal::ASCII_DASH || (internal::ASCII_0 <= c && c <= internal::ASCII_9));
}
} // namespace details
} // namespace iox
Loading

0 comments on commit 85de9bd

Please sign in to comment.