PolyFEM
Loading...
Searching...
No Matches
StringUtils.cpp
Go to the documentation of this file.
1#include "StringUtils.hpp"
2#include <iomanip>
3#include <algorithm>
4#include <functional>
5
6#include <filesystem>
7
8// Split a string into tokens
9std::vector<std::string> polyfem::utils::StringUtils::split(const std::string &str, const std::string &delimiters)
10{
11 // Skip delimiters at beginning.
12 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
13 // Find first "non-delimiter".
14 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
15
16 std::vector<std::string> tokens;
17 while (std::string::npos != pos || std::string::npos != lastPos)
18 {
19 // Found a token, add it to the vector.
20 tokens.push_back(str.substr(lastPos, pos - lastPos));
21 // Skip delimiters. Note the "not_of"
22 lastPos = str.find_first_not_of(delimiters, pos);
23 // Find next "non-delimiter"
24 pos = str.find_first_of(delimiters, lastPos);
25 }
26
27 return tokens;
28}
29
30// Skip comments in a stream
31std::istream &polyfem::utils::StringUtils::skip(std::istream &in, char x)
32{
33 std::string dummy;
34 while ((in >> std::ws).peek() == std::char_traits<char>::to_int_type(x))
35 {
36 std::getline(in, dummy);
37 }
38 return in;
39}
40
41// Tests whether a string starts with a given prefix
42bool polyfem::utils::StringUtils::startswith(const std::string &str, const std::string &prefix)
43{
44 return (str.compare(0, prefix.size(), prefix) == 0);
45}
46
47// Tests whether a string ends with a given suffix
48bool polyfem::utils::StringUtils::endswith(const std::string &str, const std::string &suffix)
49{
50 if (str.length() >= suffix.length())
51 {
52 return (0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix));
53 }
54 else
55 {
56 return false;
57 }
58}
59
60// Replace extension after the last "dot"
61std::string polyfem::utils::StringUtils::replace_ext(const std::string &filename, const std::string &newext)
62{
63 std::string ext = "";
64 if (!newext.empty())
65 {
66 ext = (newext[0] == '.' ? newext : "." + newext);
67 }
68 size_t lastdot = filename.find_last_of(".");
69 if (lastdot == std::string::npos)
70 {
71 return filename + ext;
72 }
73 return filename.substr(0, lastdot) + ext;
74}
75
76namespace
77{
78 // trim from start (in place)
79 inline std::string ltrim(const std::string &s)
80 {
81 static const std::string WHITESPACE = " \n\r\t";
82
83 size_t startpos = s.find_first_not_of(WHITESPACE);
84 return (startpos == std::string::npos) ? "" : s.substr(startpos);
85 }
86
87 // trim from end (in place)
88 inline std::string rtrim(const std::string &s)
89 {
90 static const std::string WHITESPACE = " \n\r\t";
91
92 size_t endpos = s.find_last_not_of(WHITESPACE);
93 return (endpos == std::string::npos) ? "" : s.substr(0, endpos + 1);
94 }
95} // namespace
96
97// trim from both ends (copying)
98std::string polyfem::utils::StringUtils::trim(const std::string &string)
99{
100 return rtrim(ltrim(string));
101}
102
104 const std::string &path,
105 const std::string &input_file_path,
106 const bool only_if_exists)
107{
108 if (path.empty())
109 {
110 return path;
111 }
112
113 std::filesystem::path resolved_path(path);
114 if (resolved_path.is_absolute())
115 {
116 return resolved_path.string();
117 }
118 else if (std::filesystem::exists(resolved_path))
119 {
120 return std::filesystem::weakly_canonical(resolved_path).string();
121 }
122
123 std::filesystem::path input_dir_path(input_file_path);
124 if (!std::filesystem::is_directory(input_dir_path))
125 input_dir_path = input_dir_path.parent_path();
126
127 resolved_path = std::filesystem::weakly_canonical(input_dir_path / resolved_path);
128
129 if (only_if_exists && !std::filesystem::exists(resolved_path))
130 {
131 return path; // return path unchanged
132 }
133 return resolved_path.string();
134}
int x
std::istream & skip(std::istream &in, char x='#')
std::string trim(const std::string &string)
bool startswith(const std::string &str, const std::string &prefix)
std::vector< std::string > split(const std::string &str, const std::string &delimiters=" ")
bool endswith(const std::string &str, const std::string &suffix)
std::string replace_ext(const std::string &filename, const std::string &newext)
std::string resolve_path(const std::string &path, const std::string &input_file_path, const bool only_if_exists=false)