Line data Source code
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
17 : namespace Util
18 : {
19 : /// \brief A collection of string operations
20 : namespace String
21 : {
22 : AMREX_FORCE_INLINE
23 : int ReplaceAll(std::string &str, const std::string before, const std::string after)
24 : {
25 682 : size_t start_pos = 0;
26 754 : while((start_pos = str.find(before, start_pos)) != std::string::npos) {
27 72 : str.replace(start_pos, before.length(), after);
28 72 : start_pos += after.length();
29 : }
30 682 : return 0;
31 : }
32 :
33 : AMREX_FORCE_INLINE
34 : int ReplaceAll(std::string &str, const char before, const std::string after)
35 : {
36 2686 : size_t start_pos = 0;
37 2686 : while((start_pos = str.find(before, start_pos)) != std::string::npos) {
38 0 : str.replace(start_pos, 1, after);
39 0 : start_pos += after.length();
40 : }
41 2686 : return 0;
42 : }
43 :
44 : AMREX_FORCE_INLINE
45 : std::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 :
82 : AMREX_FORCE_INLINE
83 : std::string Join(const std::vector<std::string> & vec, char separator = '_')
84 : {
85 17 : std::ostringstream oss;
86 51 : for (size_t i = 0; i < vec.size(); ++i) {
87 34 : oss << vec[i];
88 34 : if (i < vec.size() - 1) { // Don't add separator after the last element
89 17 : oss << separator;
90 : }
91 : }
92 17 : return oss.str();
93 17 : }
94 :
95 : AMREX_FORCE_INLINE
96 : std::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 :
108 : AMREX_FORCE_INLINE
109 : std::string Join(const Set::Matrix & vec, char separator = ' ')
110 : {
111 0 : std::ostringstream oss;
112 0 : for (unsigned int i = 0; i < AMREX_SPACEDIM; ++i) {
113 0 : for (unsigned int j = 0; j < AMREX_SPACEDIM; ++j) {
114 0 : oss << vec(i,j);
115 0 : if (i < AMREX_SPACEDIM - 1 && j < AMREX_SPACEDIM - 1 ) { // Don't add separator after the last element
116 0 : oss << separator;
117 : }
118 : }
119 : }
120 0 : return oss.str();
121 0 : }
122 :
123 : AMREX_FORCE_INLINE
124 : std::vector<std::string> Split(std::string &str, const char delim = ' ')
125 : {
126 155 : std::vector<std::string> ret;
127 156 : std::stringstream ss(str);
128 156 : std::string item;
129 607 : while (std::getline(ss, item, delim)) {
130 451 : ret.push_back(item);
131 : }
132 312 : return ret;
133 156 : }
134 :
135 : AMREX_FORCE_INLINE
136 : bool Contains(std::string &str, const std::string find)
137 : {
138 905 : if (str.find(find) != std::string::npos) return true;
139 761 : else return false;
140 : }
141 :
142 : template<class T>
143 : AMREX_FORCE_INLINE
144 : T Parse(std::string);
145 :
146 : template<>
147 : AMREX_FORCE_INLINE
148 : std::complex<int> Parse(std::string input)
149 : {
150 1 : int re=0, im=0;
151 :
152 5 : ReplaceAll(input, "+", " +");
153 5 : ReplaceAll(input, "-", " -");
154 : std::vector<std::string> tokens = Split(input,' ');
155 2 : for (unsigned int i = 0; i < tokens.size(); i++)
156 : {
157 1 : if(tokens[i]=="") continue;
158 3 : if(Contains(tokens[i],"i"))
159 : {
160 0 : ReplaceAll(tokens[i],"i","");
161 0 : im += std::stoi(tokens[i]);
162 : }
163 : else
164 : {
165 1 : re += std::stoi(tokens[i]);
166 : }
167 : }
168 2 : return std::complex<int>(re,im);
169 1 : }
170 :
171 : }
172 :
173 : }
174 : #endif
|