Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
5b18f1d3
Commit
5b18f1d3
authored
Apr 16, 2014
by
Davis King
Browse files
Added split_on_first() and split_on_last().
parent
5726c55d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
201 additions
and
0 deletions
+201
-0
dlib/string/string.h
dlib/string/string.h
+68
-0
dlib/string/string_abstract.h
dlib/string/string_abstract.h
+80
-0
dlib/test/string.cpp
dlib/test/string.cpp
+53
-0
No files found.
dlib/string/string.h
View file @
5b18f1d3
...
...
@@ -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
<
...
...
dlib/string/string_abstract.h
View file @
5b18f1d3
...
...
@@ -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
<
...
...
dlib/test/string.cpp
View file @
5b18f1d3
...
...
@@ -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
==
""
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment