8 #include "desktopfile_exceptions.h"
10 namespace linuxdeploy {
11 namespace desktopfile {
13 * Remove leading characters from string.
14 * @param s string to edit
15 * @param to_trim character to remove
17 static inline bool ltrim(std::string& s, char to_trim = ' ') {
18 // TODO: find more efficient way to check whether elements have been removed
19 size_t initialLength = s.length();
20 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [to_trim](int ch) {
23 return s.length() < initialLength;
27 * Remove trailing characters from string.
28 * @param s string to edit
29 * @param to_trim character to remove
31 static inline bool rtrim(std::string& s, char to_trim = ' ') {
32 // TODO: find more efficient way to check whether elements have been removed
33 auto initialLength = s.length();
34 s.erase(std::find_if(s.rbegin(), s.rend(), [to_trim](int ch) {
37 return s.length() < initialLength;
41 * Remove leading and trailing characters from string.
42 * @param s string to edit
43 * @param to_trim character to remove
45 static inline bool trim(std::string& s, char to_trim = ' ') {
46 // returns true if either modifies s
47 auto ltrim_result = ltrim(s, to_trim);
48 return rtrim(s, to_trim) && ltrim_result;
52 * Relatively inefficient implementation of a C++ lexical cast.
53 * @tparam To type to convert to
54 * @tparam From input parameter type
55 * @param from value to convert
56 * @return converted value
57 * @throws BadLexicalCastError in case a type conversion is not possible
59 template<typename To, typename From>
60 To lexicalCast(From from) {
67 throw BadLexicalCastError();
72 throw BadLexicalCastError();