Added from_string; added remove_accents

This adds a dependency on libunistring.
This commit is contained in:
Bob Polis 2021-07-02 11:30:13 +02:00
parent 6e0c828404
commit ea9bcb2fee
4 changed files with 42 additions and 2 deletions

View File

@ -1,6 +1,6 @@
LIBNAME := $(shell basename $$(pwd)) LIBNAME := $(shell basename $$(pwd))
MAJOR := 1 MAJOR := 1
MINOR := 1.0 MINOR := 2.0
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
@ -35,7 +35,7 @@ else
CXXFLAGS += -D NDEBUG -O3 CXXFLAGS += -D NDEBUG -O3
endif endif
LDLIBS := -lscerror LDLIBS := -lscerror -lunistring
RM := /bin/rm -f RM := /bin/rm -f
INSTALL := /usr/bin/install -c INSTALL := /usr/bin/install -c

19
generics.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef GENERICS_H_
#define GENERICS_H_
#include <string>
#include <sstream>
namespace sc {
template<typename T>
T from_string(const std::string& val) {
T result;
std::istringstream iss {val};
iss >> result;
return result;
}
}
#endif // GENERICS_H_

View File

@ -10,6 +10,7 @@
#include <libscerror.hpp> #include <libscerror.hpp>
#include <sys/stat.h> #include <sys/stat.h>
#include <limits.h> #include <limits.h>
#include <uninorm.h>
#include <cerrno> #include <cerrno>
#include <fstream> #include <fstream>
#include <algorithm> #include <algorithm>
@ -264,3 +265,21 @@ string sc::real_path(const string& path) {
} }
return ""; return "";
} }
string sc::remove_accents(const string& text) {
vector<char> buf;
buf.resize(text.size() * 2);
size_t bufsize {buf.size()};
u8_normalize(UNINORM_NFD,
reinterpret_cast<const uint8_t*>(text.data()),
text.size(),
reinterpret_cast<uint8_t*>(buf.data()),
&bufsize);
// hack: now remove all bytes with a value higher than 127
auto it = remove_if(buf.begin(), buf.end(), [](uint8_t c) {
return c > 127;
});
buf.erase(it, buf.end());
return {buf.data()};
}

View File

@ -64,6 +64,8 @@ namespace sc {
bool is_valid_utf8 (const std::string& str); bool is_valid_utf8 (const std::string& str);
std::string real_path (const std::string& path); std::string real_path (const std::string& path);
std::string remove_accents (const std::string& text);
} }
#endif // _string_utils_ #endif // _string_utils_