Move string helpers to utils

This commit is contained in:
Luis Díaz Más 2022-04-13 15:37:35 +02:00
parent 1a3e93856b
commit d5742f449b
6 changed files with 55 additions and 18 deletions

View File

@ -18,6 +18,7 @@
#include "pngimage.hpp"
#include "tiffimage.hpp"
#include "types.hpp"
#include "utils.hpp"
#include <array>
#include <iostream>
@ -171,12 +172,6 @@ static bool tEXtToDataBuf(const byte* bytes, long length, DataBuf& result) {
return true;
}
std::string upper(const std::string& str) {
std::string result;
transform(str.begin(), str.end(), std::back_inserter(result), toupper);
return result;
}
std::string::size_type findi(const std::string& str, const std::string& substr) {
return upper(str).find(upper(substr));
}

View File

@ -1,7 +1,24 @@
#include "utils.hpp"
#include <algorithm>
#include <cctype>
#include <iterator>
namespace Exiv2 {
bool startsWith(const std::string_view& s, const std::string_view& start) {
bool startsWith(std::string_view s, std::string_view start) {
return s.find(start) == 0;
}
std::string upper(const std::string& str) {
std::string result;
std::transform(str.begin(), str.end(), std::back_inserter(result), ::toupper);
return result;
}
std::string lower(const std::string& a) {
std::string b = a;
std::transform(a.begin(), a.end(), b.begin(), ::tolower);
return b;
}
} // namespace Exiv2

View File

@ -1,10 +1,18 @@
#ifndef EXIV2_UTILS_HPP
#define EXIV2_UTILS_HPP
#include <string>
#include <string_view>
namespace Exiv2 {
bool startsWith(const std::string_view& s, const std::string_view& start);
}
bool startsWith(std::string_view s, std::string_view start);
/// @brief Returns the uppercase version of \b str
std::string upper(const std::string& str);
/// @brief Returns the lowercase version of \b str
std::string lower(const std::string& str);
} // namespace Exiv2
#endif // EXIV2_UTILS_HPP

View File

@ -7,6 +7,7 @@
#include "error.hpp"
#include "futils.hpp"
#include "image.hpp"
#include "utils.hpp"
#include "xmp_exiv2.hpp"
#include <iostream>
@ -82,16 +83,8 @@ void XmpSidecar::readMetadata() {
copyXmpToExif(xmpData_, exifData_);
} // XmpSidecar::readMetadata
// lower case string
/// \todo very similar function in pngimage (upper). We should move those things to a string utilities file
static std::string toLowerCase(const std::string& a) {
std::string b = a;
std::transform(a.begin(), a.end(), b.begin(), ::tolower);
return b;
}
static bool matchi(const std::string& key, const char* substr) {
return toLowerCase(key).find(substr) != std::string::npos;
return lower(key).find(substr) != std::string::npos;
}
void XmpSidecar::writeMetadata() {

View File

@ -25,6 +25,7 @@ add_executable(unit_tests
test_tiffheader.cpp
test_types.cpp
test_TimeValue.cpp
test_utils.cpp
test_XmpKey.cpp
$<TARGET_OBJECTS:exiv2lib_int>
)

23
unitTests/test_utils.cpp Normal file
View File

@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#include "utils.hpp"
#include <gtest/gtest.h>
using namespace Exiv2;
TEST(stringUtils, startsWithReturnsTrue) {
ASSERT_TRUE(startsWith("Exiv2 rocks", "Exiv2"));
}
TEST(stringUtils, startsWithReturnsFlase) {
ASSERT_FALSE(startsWith("Exiv2 rocks", "exiv2"));
}
TEST(stringUtils, upperTransformStringToUpperCase) {
ASSERT_EQ("EXIV2 ROCKS", upper("Exiv2 rocks"));
}
TEST(stringUtils, lowerTransformStringToLowerCase) {
ASSERT_EQ("exiv2 rocks", lower("EXIV2 ROCKS"));
}