Skip to content

Commit

Permalink
Use base::paths type in base::has_file_extension() function
Browse files Browse the repository at this point in the history
Instead of using a CSV string with each string separated by comma (,)
we can use the new base::paths type.
  • Loading branch information
dacap committed Feb 20, 2018
1 parent 2d295fd commit b8af51a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
20 changes: 6 additions & 14 deletions base/fs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2001-2016 David Capello
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -201,21 +201,13 @@ std::string normalize_path(const std::string& filename)
return fn;
}

bool has_file_extension(const std::string& filename, const std::string& csv_extensions)
bool has_file_extension(const std::string& filename, const base::paths& extensions)
{
if (!filename.empty()) {
std::string ext = base::string_to_lower(get_file_extension(filename));

int extsz = (int)ext.size();
std::string::const_iterator p =
std::search(csv_extensions.begin(),
csv_extensions.end(),
ext.begin(), ext.end());

if ((p != csv_extensions.end()) &&
((p+extsz) == csv_extensions.end() || *(p+extsz) == ',') &&
(p == csv_extensions.begin() || *(p-1) == ','))
return true;
const std::string ext = get_file_extension(filename);
for (const auto& e : extensions)
if (utf8_icmp(ext, e) == 0)
return true;
}
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions base/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ namespace base {
std::string normalize_path(const std::string& filename);

// Returns true if the filename contains one of the specified
// extensions. The cvs_extensions parameter must be a set of
// possible extensions separated by comma.
bool has_file_extension(const std::string& filename, const std::string& csv_extensions);
// extensions. The "extensions" parameter must be a set of possible
// extensions.
bool has_file_extension(const std::string& filename, const base::paths& extensions);

int compare_filenames(const std::string& a, const std::string& b);

Expand Down
22 changes: 13 additions & 9 deletions base/fs_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (c) 2001-2016 David Capello
// Copyright (c) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -143,14 +143,18 @@ TEST(FS, RemovePathSeparator)

TEST(FS, HasFileExtension)
{
EXPECT_TRUE (has_file_extension("hi.png", "png"));
EXPECT_FALSE(has_file_extension("hi.png", "pngg"));
EXPECT_FALSE(has_file_extension("hi.png", "ppng"));
EXPECT_TRUE (has_file_extension("hi.jpeg", "jpg,jpeg"));
EXPECT_TRUE (has_file_extension("hi.jpg", "jpg,jpeg"));
EXPECT_FALSE(has_file_extension("hi.ase", "jpg,jpeg"));
EXPECT_TRUE (has_file_extension("hi.ase", "jpg,jpeg,ase"));
EXPECT_TRUE (has_file_extension("hi.ase", "ase,jpg,jpeg"));
EXPECT_TRUE (has_file_extension("hi.png", base::paths{"png"}));
EXPECT_FALSE(has_file_extension("hi.png", base::paths{"pngg"}));
EXPECT_FALSE(has_file_extension("hi.png", base::paths{"ppng"}));
EXPECT_TRUE (has_file_extension("hi.jpeg", base::paths{"jpg","jpeg"}));
EXPECT_TRUE (has_file_extension("hi.jpg", base::paths{"jpg","jpeg"}));
EXPECT_FALSE(has_file_extension("hi.ase", base::paths{"jpg","jpeg"}));
EXPECT_TRUE (has_file_extension("hi.ase", base::paths{"jpg","jpeg","ase"}));
EXPECT_TRUE (has_file_extension("hi.ase", base::paths{"ase","jpg","jpeg"}));

EXPECT_TRUE (has_file_extension("hi.png", base::paths{"Png"}));
EXPECT_TRUE (has_file_extension("hi.pnG", base::paths{"bmp","PNg"}));
EXPECT_TRUE (has_file_extension("hi.bmP", base::paths{"bMP","PNg"}));
}

TEST(FS, CompareFilenames)
Expand Down

0 comments on commit b8af51a

Please sign in to comment.