Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 1 | #include "utils/string_utils.hpp" |
| 2 | |
| 3 | #include "utils/dbus_path_utils.hpp" |
| 4 | |
| 5 | #include <cmath> |
| 6 | |
| 7 | namespace details |
| 8 | { |
| 9 | constexpr std::string_view allowedCharactersInId = |
| 10 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
| 11 | |
| 12 | std::string repeat(size_t n) |
| 13 | { |
| 14 | std::string result; |
| 15 | for (size_t i = 0; i < n; i++) |
| 16 | { |
| 17 | result += allowedCharactersInId; |
| 18 | } |
| 19 | return result; |
| 20 | } |
| 21 | |
| 22 | std::string getString(size_t length) |
| 23 | { |
| 24 | return details::repeat( |
| 25 | std::ceil(static_cast<double>(length) / |
| 26 | static_cast<double>(allowedCharactersInId.length()))) |
| 27 | .substr(0, length); |
| 28 | } |
| 29 | |
| 30 | std::string getStringWithSpaces(size_t length) |
| 31 | { |
| 32 | std::string result = getString(length); |
| 33 | size_t idx = 1; |
| 34 | while (idx < length) |
| 35 | { |
| 36 | result[idx] = ' '; |
| 37 | idx += 5; |
| 38 | } |
| 39 | return result; |
| 40 | } |
| 41 | } // namespace details |
| 42 | |
| 43 | namespace utils::string_utils |
| 44 | { |
| 45 | std::string getMaxPrefix() |
| 46 | { |
| 47 | return details::getString(constants::maxPrefixLength); |
| 48 | } |
| 49 | |
| 50 | std::string getMaxId() |
| 51 | { |
| 52 | return details::getString(constants::maxIdNameLength); |
| 53 | } |
| 54 | |
| 55 | std::string getMaxName() |
| 56 | { |
| 57 | return details::getStringWithSpaces(constants::maxIdNameLength); |
| 58 | } |
| 59 | |
| 60 | std::string getTooLongPrefix() |
| 61 | { |
| 62 | return details::getString(constants::maxPrefixLength + 1); |
| 63 | } |
| 64 | |
| 65 | std::string getTooLongId() |
| 66 | { |
| 67 | return details::getString(constants::maxIdNameLength + 1); |
| 68 | } |
| 69 | |
| 70 | std::string getTooLongName() |
| 71 | { |
| 72 | return details::getStringWithSpaces(constants::maxIdNameLength + 1); |
| 73 | } |
Patrick Williams | 3a1c297 | 2023-05-10 07:51:04 -0500 | [diff] [blame] | 74 | } // namespace utils::string_utils |