Skip to content

Commit a73ee46

Browse files
authored
Merge pull request diffblue#224 from diffblue/feature/file_utils_add_parsing_of_file_extension
Add parsing of file extension from file path-name.
2 parents 421f4eb + 6d9f029 commit a73ee46

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/util/file_util.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,26 @@ std::string fileutl_parse_name_in_pathname(std::string const &file_pathname)
201201
return file_pathname.substr(fileutl_parse_last_dir_pos(file_pathname));
202202
}
203203

204+
std::string fileutl_parse_extension_in_pathname(const std::string &pathname)
205+
{
206+
const std::size_t idx=pathname.find_last_of('.');
207+
# if defined(WIN32)
208+
const std::size_t fwd_slash_idx=pathname.find_last_of('/');
209+
const std::size_t bwd_slash_idx=pathname.find_last_of('\\');
210+
const std::size_t slash_idx=
211+
fwd_slash_idx==std::string::npos ? bwd_slash_idx :
212+
bwd_slash_idx==std::string::npos ? fwd_slash_idx :
213+
std::max(fwd_slash_idx, bwd_slash_idx);
214+
# elif defined(__linux__) || defined(__APPLE__)
215+
const std::size_t slash_idx=pathname.find_last_of('/');
216+
# else
217+
# error "Unsuported platform."
218+
# endif
219+
return
220+
idx==std::string::npos || (slash_idx!=std::string::npos && idx<slash_idx) ?
221+
std::string() : pathname.substr(idx);
222+
}
223+
204224
std::string fileutl_parse_path_in_pathname(std::string const &file_pathname)
205225
{
206226
return file_pathname.substr(0U, fileutl_parse_last_dir_pos(file_pathname));

src/util/file_util.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ uint64_t fileutl_file_size(std::string const &file_pathname);
3535

3636
std::string fileutl_parse_name_in_pathname(std::string const &file_pathname);
3737

38+
std::string fileutl_parse_extension_in_pathname(const std::string &pathname);
39+
3840
std::string fileutl_parse_path_in_pathname(std::string const &file_pathname);
3941

4042
std::string fileutl_remove_extension(std::string const &filename);

0 commit comments

Comments
 (0)