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, char 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 Set::Vector & vec, char separator = ' ')
97{
98 std::ostringstream oss;
99 for (unsigned int i = 0; i < vec.size(); ++i) {
100 oss << vec(i);
101 if (i < vec.size() - 1) { // Don't add separator after the last element
102 oss << separator;
103 }
104 }
105 return oss.str();
106}
107
108AMREX_FORCE_INLINE
109std::string Join(const Set::Matrix & vec, char separator = ' ')
110{
111 std::ostringstream oss;
112 for (unsigned int i = 0; i < AMREX_SPACEDIM; ++i) {
113 for (unsigned int j = 0; j < AMREX_SPACEDIM; ++j) {
114 oss << vec(i,j);
115 if (i < AMREX_SPACEDIM - 1 && j < AMREX_SPACEDIM - 1 ) { // Don't add separator after the last element
116 oss << separator;
117 }
118 }
119 }
120 return oss.str();
121}
122
123AMREX_FORCE_INLINE
124std::vector<std::string> Split(std::string &str, const char delim = ' ')
125{
126 std::vector<std::string> ret;
127 std::stringstream ss(str);
128 std::string item;
129 while (std::getline(ss, item, delim)) {
130 ret.push_back(item);
131 }
132 return ret;
133}
134
135AMREX_FORCE_INLINE
136bool Contains(std::string &str, const std::string find)
137{
138 if (str.find(find) != std::string::npos) return true;
139 else return false;
140}
141
142template<class T>
143AMREX_FORCE_INLINE
144T Parse(std::string);
145
146template<>
147AMREX_FORCE_INLINE
148std::complex<int> Parse(std::string input)
149{
150 int re=0, im=0;
151
152 ReplaceAll(input, "+", " +");
153 ReplaceAll(input, "-", " -");
154 std::vector<std::string> tokens = Split(input,' ');
155 for (unsigned int i = 0; i < tokens.size(); i++)
156 {
157 if(tokens[i]=="") continue;
158 if(Contains(tokens[i],"i"))
159 {
160 ReplaceAll(tokens[i],"i","");
161 im += std::stoi(tokens[i]);
162 }
163 else
164 {
165 re += std::stoi(tokens[i]);
166 }
167 }
168 return std::complex<int>(re,im);
169}
170
171}
172
173}
174#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, char 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:136
AMREX_FORCE_INLINE std::vector< std::string > Split(std::string &str, const char delim=' ')
Definition String.H:124
A collection of utility routines.
Definition Set.cpp:8