22
33namespace runcpp2
44{
5- void TrimLeft (std::string& str)
5+ void Internal:: TrimLeft (std::string& str)
66 {
77 // Remove spaces from left
88 while (!str.empty () && str.at (0 ) == ' ' )
@@ -13,7 +13,7 @@ namespace runcpp2
1313 str.erase (0 , 1 );
1414 }
1515
16- void TrimRight (std::string& str)
16+ void Internal:: TrimRight (std::string& str)
1717 {
1818 // Remove spaces from right
1919 while (!str.empty () && str.at (str.size () - 1 ) == ' ' )
@@ -25,12 +25,80 @@ namespace runcpp2
2525 }
2626
2727 // Trim string from both sides
28- void Trim (std::string& str)
28+ void Internal:: Trim (std::string& str)
2929 {
3030 // Remove spaces from left
3131 TrimLeft (str);
3232
3333 // Remove spaces from right
3434 TrimRight (str);
3535 }
36+
37+ void Internal::SplitString ( const std::string& stringToSplit,
38+ const std::string& splitter,
39+ std::vector<std::string>& outStrings)
40+ {
41+ if (splitter.empty ())
42+ {
43+ outStrings.push_back (stringToSplit);
44+ return ;
45+ }
46+
47+ std::string curOutString;
48+ std::vector<std::string> tempStringsToCheckForSplit;
49+
50+ for (int i = 0 ; i < stringToSplit.size (); ++i)
51+ {
52+ curOutString += stringToSplit.at (i);
53+
54+ // Check existing string matches
55+ for (int j = 0 ; j < tempStringsToCheckForSplit.size (); ++j)
56+ {
57+ if ( tempStringsToCheckForSplit.at (j).size () >= splitter.size () - 1 ||
58+ stringToSplit.at (i) != splitter.at (tempStringsToCheckForSplit.at (j).size ()))
59+ {
60+ tempStringsToCheckForSplit.erase (tempStringsToCheckForSplit.begin () + j);
61+ --j;
62+ continue ;
63+ }
64+ else
65+ {
66+ tempStringsToCheckForSplit.at (j) += stringToSplit.at (i);
67+
68+ // If there's a match, clear record
69+ if (tempStringsToCheckForSplit.size () == splitter.size ())
70+ {
71+ curOutString.erase (curOutString.size () - splitter.size ());
72+ outStrings.push_back (curOutString);
73+ curOutString.clear ();
74+ tempStringsToCheckForSplit.clear ();
75+ break ;
76+ }
77+ }
78+ }
79+
80+ // If the current character fits the first character of splitter, add it to the record
81+ if (stringToSplit.at (i) == splitter.front ())
82+ {
83+ // Check special case if the splitter is only 1 character
84+ if (splitter.size () == 1 )
85+ {
86+ curOutString.erase (curOutString.size () - splitter.size ());
87+ outStrings.push_back (curOutString);
88+ curOutString.clear ();
89+ }
90+ // Otherwise push to string matches
91+ else
92+ {
93+ tempStringsToCheckForSplit.push_back (std::string ());
94+ tempStringsToCheckForSplit.back () += stringToSplit.at (i);
95+
96+ }
97+ }
98+ }
99+
100+ // Push the remaining string
101+ if (!curOutString.empty ())
102+ outStrings.push_back (curOutString);
103+ }
36104}
0 commit comments