Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
|
||
#ifndef _STREX_STRINGS_H_ | ||
#define _STREX_STRINGS_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace strex | ||
{ | ||
std::vector<std::string> explode(const std::string& str, const std::string& delim); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,41 @@ | ||
|
||
#include "strings.h" | ||
|
||
namespace strex | ||
{ | ||
//------------------------------------------------------------------------------ | ||
std::vector<std::string> explode(const std::string& str, const std::string& delim) | ||
{ | ||
std::vector<std::string> gibs; | ||
size_t start = 0; | ||
size_t end = 0; | ||
|
||
while ((start != std::string::npos) && (start < str.size())) | ||
{ | ||
end = str.find(delim, start); | ||
|
||
std::string gib; | ||
if (end == std::string::npos) | ||
{ | ||
gib = str.substr(start); | ||
start = std::string::npos; | ||
} | ||
else | ||
{ | ||
gib = str.substr(start, end - start); | ||
start = end + delim.size(); | ||
} | ||
gibs.push_back(gib); | ||
} | ||
|
||
// special case, when the delimiter is at the end | ||
if (start == str.size()) | ||
{ | ||
gibs.push_back(std::string()); | ||
} | ||
|
||
return gibs; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
#include <UnitTest++/UnitTest++.h> | ||
|
||
#include <strex/strings.h> | ||
|
||
SUITE(lexcial_cast) | ||
{ | ||
TEST(explode) | ||
{ | ||
std::string pattern = "one;two;three"; | ||
std::vector<std::string> bits = strex::explode(pattern, ";"); | ||
|
||
CHECK_EQUAL(3, bits.size()); | ||
CHECK_EQUAL("one", bits.at(0)); | ||
CHECK_EQUAL("two", bits.at(1)); | ||
CHECK_EQUAL("three", bits.at(2)); | ||
} | ||
|
||
TEST(explode2) | ||
{ | ||
std::string pattern = ";two;three"; | ||
std::vector<std::string> bits = strex::explode(pattern, ";"); | ||
|
||
CHECK_EQUAL(3, bits.size()); | ||
CHECK_EQUAL("", bits.at(0)); | ||
CHECK_EQUAL("two", bits.at(1)); | ||
CHECK_EQUAL("three", bits.at(2)); | ||
} | ||
|
||
TEST(explode3) | ||
{ | ||
std::string pattern = "one;two;"; | ||
std::vector<std::string> bits = strex::explode(pattern, ";"); | ||
|
||
CHECK_EQUAL(3, bits.size()); | ||
CHECK_EQUAL("one", bits.at(0)); | ||
CHECK_EQUAL("two", bits.at(1)); | ||
CHECK_EQUAL("", bits.at(2)); | ||
} | ||
} |