@@ -33,48 +33,60 @@ void OvTools::Utils::String::ReplaceAll(std::string& p_target, const std::string
3333
3434std::string OvTools::Utils::String::GenerateUnique (const std::string& p_source, std::function<bool (std::string)> p_isAvailable)
3535{
36- auto suffixlessSource = p_source;
37-
38- auto suffixOpeningParenthesisPos = std::string::npos;
39- auto suffixClosingParenthesisPos = std::string::npos;
40-
41- // Keep track of the current character position when iterating onto `p_source`
42- auto currentPos = decltype (std::string::npos){p_source.length () - 1 };
43-
44- // Here we search for `(` and `)` positions. (Needed to extract the number between those parenthesis)
45- for (auto it = p_source.rbegin (); it < p_source.rend (); ++it, --currentPos)
46- {
47- const auto c = *it;
48-
49- if (suffixClosingParenthesisPos == std::string::npos && c == ' )' ) suffixClosingParenthesisPos = currentPos;
50- if (suffixClosingParenthesisPos != std::string::npos && c == ' (' ) suffixOpeningParenthesisPos = currentPos;
51- }
52-
53- // We need to declare our `counter` here to store the number between found parenthesis OR 1 (In the case no parenthesis, AKA, suffix, has been found)
54- auto counter = uint32_t { 1 };
55-
56- // If the two parenthis have been found AND the closing parenthesis is the last character AND there is a space before the opening parenthesis
57- if (suffixOpeningParenthesisPos != std::string::npos && suffixClosingParenthesisPos == p_source.length () - 1 && suffixOpeningParenthesisPos > 0 && p_source[suffixOpeningParenthesisPos - 1 ] == ' ' )
58- {
59- // Extract the string between those parenthesis
60- const auto between = p_source.substr (suffixOpeningParenthesisPos + 1 , suffixClosingParenthesisPos - suffixOpeningParenthesisPos - 1 );
61-
62- // If the `between` string is composed of digits (AKA, `between` is a number)
63- if (!between.empty () && std::find_if (between.begin (), between.end (), [](unsigned char c) { return !std::isdigit (c); }) == between.end ())
64- {
65- counter = static_cast <uint32_t >(std::atoi (between.c_str ()));
66- suffixlessSource = p_source.substr (0 , suffixOpeningParenthesisPos - 1 );
67- }
68- }
69-
70- auto result = suffixlessSource;
71-
72- // While `result` isn't available, we keep generating new strings
73- while (!p_isAvailable (result))
74- {
75- // New strings are composed of the `suffixlessSource` (Ex: "Foo (1)" without suffix is "Foo")
76- result = suffixlessSource + " (" + std::to_string (counter++) + " )" ;
77- }
78-
79- return result;
36+ auto suffixlessSource = p_source;
37+
38+ auto suffixOpeningParenthesisPos = std::string::npos;
39+ auto suffixClosingParenthesisPos = std::string::npos;
40+
41+ // Keep track of the current character position when iterating onto `p_source`
42+ auto currentPos = decltype (std::string::npos){p_source.length () - 1 };
43+
44+ // Here we search for `(` and `)` positions. (Needed to extract the number between those parenthesis)
45+ for (auto it = p_source.rbegin (); it < p_source.rend (); ++it, --currentPos)
46+ {
47+ const auto c = *it;
48+
49+ if (suffixClosingParenthesisPos == std::string::npos && c == ' )' ) suffixClosingParenthesisPos = currentPos;
50+ if (suffixClosingParenthesisPos != std::string::npos && c == ' (' ) suffixOpeningParenthesisPos = currentPos;
51+ }
52+
53+ // We need to declare our `counter` here to store the number between found parenthesis OR 1 (In the case no parenthesis, AKA, suffix, has been found)
54+ auto counter = uint32_t { 1 };
55+
56+ // If the two parenthis have been found AND the closing parenthesis is the last character AND there is a space before the opening parenthesis
57+ if (suffixOpeningParenthesisPos != std::string::npos && suffixClosingParenthesisPos == p_source.length () - 1 && suffixOpeningParenthesisPos > 0 && p_source[suffixOpeningParenthesisPos - 1 ] == ' ' )
58+ {
59+ // Extract the string between those parenthesis
60+ const auto between = p_source.substr (suffixOpeningParenthesisPos + 1 , suffixClosingParenthesisPos - suffixOpeningParenthesisPos - 1 );
61+
62+ // If the `between` string is composed of digits (AKA, `between` is a number)
63+ if (!between.empty () && std::find_if (between.begin (), between.end (), [](unsigned char c) { return !std::isdigit (c); }) == between.end ())
64+ {
65+ counter = static_cast <uint32_t >(std::atoi (between.c_str ()));
66+ suffixlessSource = p_source.substr (0 , suffixOpeningParenthesisPos - 1 );
67+ }
68+ }
69+
70+ auto result = suffixlessSource;
71+
72+ // While `result` isn't available, we keep generating new strings
73+ while (!p_isAvailable (result))
74+ {
75+ // New strings are composed of the `suffixlessSource` (Ex: "Foo (1)" without suffix is "Foo")
76+ result = suffixlessSource + " (" + std::to_string (counter++) + " )" ;
77+ }
78+
79+ return result;
80+ }
81+
82+ void OvTools::Utils::String::Trim (std::string& p_str, const TrimOptions p_trimOptions)
83+ {
84+ if (p_trimOptions.left )
85+ {
86+ p_str.erase (0 , p_str.find_first_not_of (" \t\n\r\f\v " ));
87+ }
88+ if (p_trimOptions.right )
89+ {
90+ p_str.erase (p_str.find_last_not_of (" \t\n\r\f\v " ) + 1 );
91+ }
8092}
0 commit comments