Added from_string; added remove_accents
This adds a dependency on libunistring.
This commit is contained in:
parent
6e0c828404
commit
ea9bcb2fee
4
Makefile
4
Makefile
@ -1,6 +1,6 @@
|
||||
LIBNAME := $(shell basename $$(pwd))
|
||||
MAJOR := 1
|
||||
MINOR := 1.0
|
||||
MINOR := 2.0
|
||||
|
||||
UNAME_S := $(shell uname -s)
|
||||
|
||||
@ -35,7 +35,7 @@ else
|
||||
CXXFLAGS += -D NDEBUG -O3
|
||||
endif
|
||||
|
||||
LDLIBS := -lscerror
|
||||
LDLIBS := -lscerror -lunistring
|
||||
|
||||
RM := /bin/rm -f
|
||||
INSTALL := /usr/bin/install -c
|
||||
|
19
generics.hpp
Normal file
19
generics.hpp
Normal 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_
|
@ -10,6 +10,7 @@
|
||||
#include <libscerror.hpp>
|
||||
#include <sys/stat.h>
|
||||
#include <limits.h>
|
||||
#include <uninorm.h>
|
||||
#include <cerrno>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
@ -264,3 +265,21 @@ string sc::real_path(const string& path) {
|
||||
}
|
||||
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()};
|
||||
}
|
||||
|
@ -64,6 +64,8 @@ namespace sc {
|
||||
bool is_valid_utf8 (const std::string& str);
|
||||
|
||||
std::string real_path (const std::string& path);
|
||||
|
||||
std::string remove_accents (const std::string& text);
|
||||
}
|
||||
|
||||
#endif // _string_utils_
|
||||
|
Loading…
x
Reference in New Issue
Block a user