string list joining, and a test case for that and splitting.
This commit is contained in:
parent
8cb716fe8e
commit
7984f055e2
@ -278,6 +278,19 @@ namespace simgear {
|
|||||||
return v1parts.size() - v2parts.size();
|
return v1parts.size() - v2parts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string join(const string_list& l, const string& joinWith)
|
||||||
|
{
|
||||||
|
string result;
|
||||||
|
unsigned int count = l.size();
|
||||||
|
for (unsigned int i=0; i < count; ++i) {
|
||||||
|
result += l[i];
|
||||||
|
if (i < (count - 1)) {
|
||||||
|
result += joinWith;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
} // end namespace strutils
|
} // end namespace strutils
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
typedef std::vector < std::string > string_list;
|
||||||
|
|
||||||
namespace simgear {
|
namespace simgear {
|
||||||
namespace strutils {
|
namespace strutils {
|
||||||
@ -93,11 +94,17 @@ namespace simgear {
|
|||||||
* resulting in at most maxsplit+1 words.
|
* resulting in at most maxsplit+1 words.
|
||||||
* @return Array of words.
|
* @return Array of words.
|
||||||
*/
|
*/
|
||||||
std::vector<std::string>
|
string_list
|
||||||
split( const std::string& s,
|
split( const std::string& s,
|
||||||
const char* sep = 0,
|
const char* sep = 0,
|
||||||
int maxsplit = 0 );
|
int maxsplit = 0 );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a single string by joining the elements of a list with
|
||||||
|
* another string.
|
||||||
|
*/
|
||||||
|
std::string join(const string_list& l, const std::string& joinWith = "");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if a string starts with a string
|
* Test if a string starts with a string
|
||||||
*
|
*
|
||||||
|
@ -63,6 +63,20 @@ int main (int ac, char ** av)
|
|||||||
// since we compare numerically, leasing zeros shouldn't matter
|
// since we compare numerically, leasing zeros shouldn't matter
|
||||||
VERIFY(compare_versions("0.06.7", "0.6.07") == 0);
|
VERIFY(compare_versions("0.06.7", "0.6.07") == 0);
|
||||||
|
|
||||||
|
string_list la = split("zero one two three four five");
|
||||||
|
COMPARE(la[2], "two");
|
||||||
|
COMPARE(la[5], "five");
|
||||||
|
COMPARE(la.size(), 6);
|
||||||
|
|
||||||
|
string_list lb = split("alpha:beta:gamma:delta", ":", 2);
|
||||||
|
COMPARE(lb.size(), 3);
|
||||||
|
COMPARE(lb[0], "alpha");
|
||||||
|
COMPARE(lb[1], "beta");
|
||||||
|
COMPARE(lb[2], "gamma:delta");
|
||||||
|
|
||||||
|
std::string j = join(la, "&");
|
||||||
|
COMPARE(j, "zero&one&two&three&four&five");
|
||||||
|
|
||||||
cout << "all tests passed successfully!" << endl;
|
cout << "all tests passed successfully!" << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user