Commit 5b18f1d3 authored by Davis King's avatar Davis King

Added split_on_first() and split_on_last().

parent 5726c55d
......@@ -856,6 +856,74 @@ namespace dlib
return _dT(charT,"");
}
// ----------------------------------------------------------------------------------------
template <
typename charT,
typename traits,
typename alloc
>
std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_first (
const std::basic_string<charT,traits,alloc>& str,
const charT* delim = _dT(charT," \n\r\t")
)
{
typename std::basic_string<charT,traits,alloc>::size_type delim_pos = str.find_first_of(delim);
if (delim_pos != std::basic_string<charT,traits,alloc>::npos)
return std::make_pair(str.substr(0, delim_pos), str.substr(delim_pos+1));
else
return std::make_pair(str, _dT(charT,""));
}
template <
typename charT,
typename traits,
typename alloc
>
inline std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_first (
const std::basic_string<charT,traits,alloc>& str,
const std::basic_string<charT,traits,alloc>& delim
)
{
return split_on_first(str, delim.c_str());
}
// ----------------------------------------------------------------------------------------
template <
typename charT,
typename traits,
typename alloc
>
std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_last (
const std::basic_string<charT,traits,alloc>& str,
const charT* delim = _dT(charT," \n\r\t")
)
{
typename std::basic_string<charT,traits,alloc>::size_type delim_pos = str.find_last_of(delim);
if (delim_pos != std::basic_string<charT,traits,alloc>::npos)
return std::make_pair(str.substr(0, delim_pos), str.substr(delim_pos+1));
else
return std::make_pair(str, _dT(charT,""));
}
template <
typename charT,
typename traits,
typename alloc
>
inline std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_last (
const std::basic_string<charT,traits,alloc>& str,
const std::basic_string<charT,traits,alloc>& delim
)
{
return split_on_last(str, delim.c_str());
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -523,6 +523,86 @@ namespace dlib
- returns right_substr(str, std::basic_string<charT,traits,alloc>(delim))
!*/
// ----------------------------------------------------------------------------------------
template <
typename charT,
typename traits,
typename alloc
>
std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_first (
const std::basic_string<charT,traits,alloc>& str,
const charT* delim = _dT(charT, " \n\r\t")
);
/*!
ensures
- This function splits string into two parts, the split is based on the first
occurrence of any character from delim.
- let delim_pos = str.find_first_of(delim)
- if (delim_pos == std::string::npos) then
- returns make_pair(str,"")
- else
- return make_pair(str.substr(0, delim_pos), str.substr(delim_pos+1));
!*/
template <
typename charT,
typename traits,
typename alloc
>
std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_first (
const std::basic_string<charT,traits,alloc>& str,
const std::basic_string<charT,traits,alloc>& delim
);
/*!
requires
- delim == a valid null-terminated C string
ensures
- returns split_on_first(str, delim.c_str())
!*/
// ----------------------------------------------------------------------------------------
template <
typename charT,
typename traits,
typename alloc
>
std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_last (
const std::basic_string<charT,traits,alloc>& str,
const charT* delim = _dT(charT, " \n\r\t")
);
/*!
ensures
- This function splits string into two parts, the split is based on the last
occurrence of any character from delim.
- let delim_pos = str.find_last_of(delim)
- if (delim_pos == std::string::npos) then
- returns make_pair(str,"")
- else
- return make_pair(str.substr(0, delim_pos), str.substr(delim_pos+1));
!*/
template <
typename charT,
typename traits,
typename alloc
>
std::pair<std::basic_string<charT,traits,alloc>, std::basic_string<charT,traits,alloc> >
split_on_last (
const std::basic_string<charT,traits,alloc>& str,
const std::basic_string<charT,traits,alloc>& delim
);
/*!
requires
- delim == a valid null-terminated C string
ensures
- returns split_on_last(str, delim.c_str())
!*/
// ----------------------------------------------------------------------------------------
template <
......
......@@ -242,6 +242,59 @@ namespace
DLIB_TEST(wv.size() == 2);
DLIB_TEST(wv[0] == L"test");
DLIB_TEST(wv[1] == L"string");
wstr = L"test string hah";
DLIB_TEST(split_on_first(wstr).first == L"test");
DLIB_TEST(split_on_first(wstr).second == L"string hah");
DLIB_TEST(split_on_first(wstr,L"#").first == L"test string hah");
DLIB_TEST(split_on_first(wstr,L"#").second == L"");
DLIB_TEST(split_on_last(wstr).first == L"test string");
DLIB_TEST(split_on_last(wstr).second == L"hah");
DLIB_TEST(split_on_last(wstr,L"#").first == L"test string hah");
DLIB_TEST(split_on_last(wstr,L"#").second == L"");
wstr = L"";
DLIB_TEST(split_on_first(wstr).first == L"");
DLIB_TEST(split_on_first(wstr).second == L"");
str = "test string hah";
DLIB_TEST(split_on_first(str).first == "test");
DLIB_TEST(split_on_first(str).second == "string hah");
DLIB_TEST(split_on_first(str,"#").first == "test string hah");
DLIB_TEST(split_on_first(str,"#").second == "");
DLIB_TEST(split_on_last(str).first == "test string");
DLIB_TEST(split_on_last(str).second == "hah");
DLIB_TEST(split_on_last(str,"#").first == "test string hah");
DLIB_TEST(split_on_last(str,"#").second == "");
str = "";
DLIB_TEST(split_on_first(str).first == "");
DLIB_TEST(split_on_first(str).second == "");
wstr = L"test.string.hah";
DLIB_TEST(split_on_first(wstr,L".").first == L"test");
DLIB_TEST(split_on_first(wstr,L".").second == L"string.hah");
DLIB_TEST(split_on_first(wstr).first == L"test.string.hah");
DLIB_TEST(split_on_first(wstr).second == L"");
DLIB_TEST(split_on_last(wstr,L".").first == L"test.string");
DLIB_TEST(split_on_last(wstr,L".").second == L"hah");
DLIB_TEST(split_on_last(wstr).first == L"test.string.hah");
DLIB_TEST(split_on_last(wstr).second == L"");
wstr = L"";
DLIB_TEST(split_on_first(wstr).first == L"");
DLIB_TEST(split_on_first(wstr).second == L"");
str = "test.string.hah";
DLIB_TEST(split_on_first(str,".").first == "test");
DLIB_TEST(split_on_first(str,".").second == "string.hah");
DLIB_TEST(split_on_first(str).first == "test.string.hah");
DLIB_TEST(split_on_first(str).second == "");
DLIB_TEST(split_on_last(str,".").first == "test.string");
DLIB_TEST(split_on_last(str,".").second == "hah");
DLIB_TEST(split_on_last(str).first == "test.string.hah");
DLIB_TEST(split_on_last(str).second == "");
str = "";
DLIB_TEST(split_on_first(str).first == "");
DLIB_TEST(split_on_first(str).second == "");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment