blob: a39afcae4b42991ad3a59b9a895411063445b6b9 [file] [log] [blame]
Szymon Dompke32305f12022-07-05 15:37:21 +02001#include "utils/string_utils.hpp"
2
3#include "utils/dbus_path_utils.hpp"
4
5#include <cmath>
6
7namespace details
8{
9constexpr std::string_view allowedCharactersInId =
10 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
11
12std::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
22std::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
30std::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
43namespace utils::string_utils
44{
45std::string getMaxPrefix()
46{
47 return details::getString(constants::maxPrefixLength);
48}
49
50std::string getMaxId()
51{
52 return details::getString(constants::maxIdNameLength);
53}
54
55std::string getMaxName()
56{
57 return details::getStringWithSpaces(constants::maxIdNameLength);
58}
59
60std::string getTooLongPrefix()
61{
62 return details::getString(constants::maxPrefixLength + 1);
63}
64
65std::string getTooLongId()
66{
67 return details::getString(constants::maxIdNameLength + 1);
68}
69
70std::string getTooLongName()
71{
72 return details::getStringWithSpaces(constants::maxIdNameLength + 1);
73}
74} // namespace utils::string_utils