M module-utils/utility/Utils.cpp => module-utils/utility/Utils.cpp +22 -19
@@ 1,4 1,4 @@
-// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
+// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "Utils.hpp"
@@ 32,24 32,6 @@ namespace utils::filesystem
return digestCrc32.getHashValue();
}
- std::string generateRandomId(std::size_t length) noexcept
- {
- const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
- std::random_device random_device;
- std::mt19937 generator(random_device());
- generator.seed(std::time(nullptr));
- std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1);
-
- std::string random_string;
-
- for (std::size_t i = 0; i < length; ++i) {
- random_string += CHARACTERS[distribution(generator)];
- }
-
- return random_string;
- }
-
std::string getline(std::FILE *stream, uint32_t length) noexcept
{
std::uint32_t currentPosition = std::ftell(stream);
@@ 99,4 81,25 @@ namespace utils
return s.str();
}
+ std::string generateRandomId(std::size_t length) noexcept
+ {
+ if (!length)
+ return {};
+
+ const std::string CHARACTERS("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
+
+ auto random_device = std::make_unique<std::random_device>();
+ auto generator = std::make_unique<std::mt19937>((*random_device)());
+ generator->seed(std::time(nullptr));
+ std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1);
+
+ std::string random_string(length, '\0');
+
+ for (std::size_t i = 0; i < length; ++i) {
+ random_string[i] = CHARACTERS[distribution(*generator)];
+ }
+
+ return random_string;
+ }
+
} // namespace utils
M module-utils/utility/Utils.hpp => module-utils/utility/Utils.hpp +2 -1
@@ 257,10 257,11 @@ namespace utils
#endif
}
+ [[nodiscard]] std::string generateRandomId(std::size_t length) noexcept;
+
namespace filesystem
{
[[nodiscard]] unsigned long computeFileCRC32(std::FILE *file) noexcept;
- [[nodiscard]] std::string generateRandomId(std::size_t length = 0) noexcept;
[[nodiscard]] std::string getline(std::FILE *stream, uint32_t length = 1024) noexcept;
} // namespace filesystem
} // namespace utils
M module-utils/utility/tests/unittest_utils.cpp => module-utils/utility/tests/unittest_utils.cpp +25 -1
@@ 1,4 1,4 @@
-// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
+// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include <cstring>
@@ 425,3 425,27 @@ TEST_CASE("Bytes to hex")
REQUIRE((ret == "01020304ff"));
}
}
+
+TEST_CASE("Generate random Id")
+{
+ SECTION("Random Id length of 0")
+ {
+ const auto expectedSize = 0;
+ auto ret = utils::generateRandomId(expectedSize);
+ REQUIRE((ret.size() == expectedSize));
+ }
+
+ SECTION("Random Id length of 1")
+ {
+ const auto expectedSize = 1;
+ auto ret = utils::generateRandomId(expectedSize);
+ REQUIRE((ret.size() == expectedSize));
+ }
+
+ SECTION("Random Id length of 16")
+ {
+ const auto expectedSize = 16;
+ auto ret = utils::generateRandomId(expectedSize);
+ REQUIRE((ret.size() == expectedSize));
+ }
+}