Alamo
String.H
Go to the documentation of this file.
1#ifndef UTIL_STRING_H
2#define UTIL_STRING_H
3
4#include <iostream>
5#include <sys/ioctl.h>
6#include <unistd.h>
7#include <complex>
8#include <stdlib.h>
9
10#include <string>
11
12#include "AMReX_Config.H"
13#include "Set/Base.H"
14#include "Util/Util.H"
15
16/// \brief A collection of utility routines
17namespace Util
18{
19/// \brief A collection of string operations
20namespace String
21{
22AMREX_FORCE_INLINE
23int ReplaceAll(std::string &str, const std::string before, const std::string after)
24{
25 size_t start_pos = 0;
26 while((start_pos = str.find(before, start_pos)) != std::string::npos) {
27 str.replace(start_pos, before.length(), after);
28 start_pos += after.length();
29 }
30 return 0;
31}
32
33AMREX_FORCE_INLINE
34int ReplaceAll(std::string &str, const char before, const std::string after)
35{
36 size_t start_pos = 0;
37 while((start_pos = str.find(before, start_pos)) != std::string::npos) {
38 str.replace(start_pos, 1, after);
39 start_pos += after.length();
40 }
41 return 0;
42}
43
44AMREX_FORCE_INLINE
45std::string Wrap(std::string text, unsigned per_line)
46{
47 unsigned line_begin = 0;
48
49 while (line_begin < text.size())
50 {
51 const unsigned ideal_end = line_begin + per_line ;
52 unsigned line_end = ideal_end <= text.size() ? ideal_end : text.size()-1;
53
54 if (line_end == text.size() - 1)
55 ++line_end;
56 else if (std::isspace(text[line_end]))
57 {
58 text[line_end] = '\n';
59 ++line_end;
60 }
61 else // backtrack
62 {
63 unsigned end = line_end;
64 while ( end > line_begin && !std::isspace(text[end]))
65 --end;
66
67 if (end != line_begin)
68 {
69 line_end = end;
70 text[line_end++] = '\n';
71 }
72 else
73 text.insert(line_end++, 1, '\n');
74 }
75
76 line_begin = line_end;
77 }
78
79 return text;
80}
81
82AMREX_FORCE_INLINE
83std::string Join(const std::vector<std::string> & vec, std::string separator = "_")
84{
85 std::ostringstream oss;
86 for (size_t i = 0; i < vec.size(); ++i) {
87 oss << vec[i];
88 if (i < vec.size() - 1) { // Don't add separator after the last element
89 oss << separator;
90 }
91 }
92 return oss.str();
93}
94
95AMREX_FORCE_INLINE
96std::string Join(const std::vector<std::string> & vec, char separator)
97{
98 return Join(vec,std::to_string(separator));
99}
100
101AMREX_FORCE_INLINE
102std::string Join(const std::vector<double> & vec, std::string separator = " ")
103{
104 std::vector<std::string> strvec;
105 for (auto v : vec) strvec.push_back(std::to_string(v));
106 return Join(strvec,separator);
107}
108
109AMREX_FORCE_INLINE
110std::string Join(const Set::Vector & vec, char separator = ' ')
111{
112 std::ostringstream oss;
113 for (unsigned int i = 0; i < vec.size(); ++i) {
114 oss << vec(i);
115 if (i < vec.size() - 1) { // Don't add separator after the last element
116 oss << separator;
117 }
118 }
119 return oss.str();
120}
121
122AMREX_FORCE_INLINE
123std::string Join(const Set::Matrix & vec, char separator = ' ')
124{
125 std::ostringstream oss;
126 for (unsigned int i = 0; i < AMREX_SPACEDIM; ++i) {
127 for (unsigned int j = 0; j < AMREX_SPACEDIM; ++j) {
128 oss << vec(i,j);
129 if (i < AMREX_SPACEDIM - 1 && j < AMREX_SPACEDIM - 1 ) { // Don't add separator after the last element
130 oss << separator;
131 }
132 }
133 }
134 return oss.str();
135}
136
137AMREX_FORCE_INLINE
138std::vector<std::string> Split(std::string &str, const char delim = ' ')
139{
140 std::vector<std::string> ret;
141 std::stringstream ss(str);
142 std::string item;
143 while (std::getline(ss, item, delim)) {
144 ret.push_back(item);
145 }
146 return ret;
147}
148
149AMREX_FORCE_INLINE
150bool Contains(std::string &str, const std::string find)
151{
152 if (str.find(find) != std::string::npos) return true;
153 else return false;
154}
155
156template<class T>
157AMREX_FORCE_INLINE
158T Parse(std::string);
159
160template<>
161AMREX_FORCE_INLINE
162std::complex<int> Parse(std::string input)
163{
164 int re=0, im=0;
165
166 ReplaceAll(input, "+", " +");
167 ReplaceAll(input, "-", " -");
168 std::vector<std::string> tokens = Split(input,' ');
169 for (unsigned int i = 0; i < tokens.size(); i++)
170 {
171 if(tokens[i]=="") continue;
172 if(Contains(tokens[i],"i"))
173 {
174 ReplaceAll(tokens[i],"i","");
175 im += std::stoi(tokens[i]);
176 }
177 else
178 {
179 re += std::stoi(tokens[i]);
180 }
181 }
182 return std::complex<int>(re,im);
183}
184
185}
186
187}
188#endif
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition Base.H:19
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, AMREX_SPACEDIM > Matrix
Definition Base.H:22
AMREX_FORCE_INLINE int ReplaceAll(std::string &str, const std::string before, const std::string after)
Definition String.H:23
AMREX_FORCE_INLINE std::string Wrap(std::string text, unsigned per_line)
Definition String.H:45
AMREX_FORCE_INLINE std::string Join(const std::vector< std::string > &vec, std::string separator="_")
Definition String.H:83
AMREX_FORCE_INLINE T Parse(std::string)
AMREX_FORCE_INLINE bool Contains(std::string &str, const std::string find)
Definition String.H:150
AMREX_FORCE_INLINE std::vector< std::string > Split(std::string &str, const char delim=' ')
Definition String.H:138
A collection of utility routines.
Definition Set.cpp:8