00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "base/split.h"
00015 #if defined(_MSC_VER)
00016 #include <iterator>
00017 #endif // _MSC_VER
00018
00019 namespace operations_research {
00020
00021
00022
00023
00024
00025
00026
00027
00028 template <typename ITR>
00029 static inline
00030 void SplitStringToIteratorUsing(const std::string& full,
00031 const char* delim,
00032 ITR& result) {
00033
00034 if (delim[0] != '\0' && delim[1] == '\0') {
00035 char c = delim[0];
00036 const char* p = full.data();
00037 const char* end = p + full.size();
00038 while (p != end) {
00039 if (*p == c) {
00040 ++p;
00041 } else {
00042 const char* start = p;
00043 while (++p != end && *p != c);
00044 *result++ = std::string(start, p - start);
00045 }
00046 }
00047 return;
00048 }
00049
00050 std::string::size_type begin_index, end_index;
00051 begin_index = full.find_first_not_of(delim);
00052 while (begin_index != std::string::npos) {
00053 end_index = full.find_first_of(delim, begin_index);
00054 if (end_index == std::string::npos) {
00055 *result++ = full.substr(begin_index);
00056 return;
00057 }
00058 *result++ = full.substr(begin_index, (end_index - begin_index));
00059 begin_index = full.find_first_not_of(delim, end_index);
00060 }
00061 }
00062
00063 void SplitStringUsing(const std::string& full,
00064 const char* delim,
00065 std::vector<std::string>* result) {
00066 std::back_insert_iterator< std::vector<std::string> > it(*result);
00067 SplitStringToIteratorUsing(full, delim, it);
00068 }
00069 }