@@ -47,10 +47,15 @@ void parse_expression(const std::string& fn, std::vector<T>& vec)
47
47
{
48
48
ModuleBase::TITLE (" Input_Conv" , " parse_expression" );
49
49
int count = 0 ;
50
- std::string pattern (" ([-+]?[0-9]+\\ *[-+]?[0-9.]+|[-+]?[0-9,.]+)" );
50
+
51
+ // Update the regex pattern to handle scientific notation
52
+ std::string pattern (" ([-+]?[0-9]+\\ *[-+]?[0-9.eE+-]+|[-+]?[0-9,.eE+-]+)" );
53
+
51
54
std::vector<std::string> str;
52
55
std::stringstream ss (fn);
53
56
std::string section;
57
+
58
+ // Split the input string into substrings by spaces
54
59
while (ss >> section)
55
60
{
56
61
int index = 0 ;
@@ -64,24 +69,14 @@ void parse_expression(const std::string& fn, std::vector<T>& vec)
64
69
section.erase (0 , index );
65
70
str.push_back (section);
66
71
}
67
- // std::string::size_type pos1, pos2;
68
- // std::string c = " ";
69
- // pos2 = fn.find(c);
70
- // pos1 = 0;
71
- // while (std::string::npos != pos2)
72
- // {
73
- // str.push_back(fn.substr(pos1, pos2 - pos1));
74
- // pos1 = pos2 + c.size();
75
- // pos2 = fn.find(c, pos1);
76
- // }
77
- // if (pos1 != fn.length())
78
- // {
79
- // str.push_back(fn.substr(pos1));
80
- // }
72
+
73
+ // Compile the regular expression
81
74
regex_t reg;
82
75
regcomp (®, pattern.c_str (), REG_EXTENDED);
83
76
regmatch_t pmatch[1 ];
84
77
const size_t nmatch = 1 ;
78
+
79
+ // Loop over each section and apply regex to extract numbers
85
80
for (size_t i = 0 ; i < str.size (); ++i)
86
81
{
87
82
if (str[i] == " " )
@@ -90,42 +85,46 @@ void parse_expression(const std::string& fn, std::vector<T>& vec)
90
85
}
91
86
int status = regexec (®, str[i].c_str (), nmatch, pmatch, 0 );
92
87
std::string sub_str = " " ;
88
+
89
+ // Extract the matched substring
93
90
for (size_t j = pmatch[0 ].rm_so ; j != pmatch[0 ].rm_eo ; ++j)
94
91
{
95
92
sub_str += str[i][j];
96
93
}
94
+
95
+ // Check if the substring contains multiplication (e.g., "2*3.14")
97
96
std::string sub_pattern (" \\ *" );
98
97
regex_t sub_reg;
99
98
regcomp (&sub_reg, sub_pattern.c_str (), REG_EXTENDED);
100
99
regmatch_t sub_pmatch[1 ];
101
100
const size_t sub_nmatch = 1 ;
101
+
102
102
if (regexec (&sub_reg, sub_str.c_str (), sub_nmatch, sub_pmatch, 0 ) == 0 )
103
103
{
104
104
size_t pos = sub_str.find (" *" );
105
105
int num = stoi (sub_str.substr (0 , pos));
106
- assert (num>= 0 );
106
+ assert (num >= 0 );
107
107
T occ = stof (sub_str.substr (pos + 1 , sub_str.size ()));
108
- // std::vector<double> ocp_temp(num, occ);
109
- // const std::vector<double>::iterator dest = vec.begin() + count;
110
- // copy(ocp_temp.begin(), ocp_temp.end(), dest);
111
- // count += num;
108
+
109
+ // Add the value to the vector `num` times
112
110
for (size_t k = 0 ; k != num; k++)
113
111
{
114
112
vec.emplace_back (occ);
115
113
}
116
114
}
117
115
else
118
116
{
119
- // vec[count] = stof(sub_str);
120
- // count += 1;
117
+ // Handle scientific notation and convert to T
121
118
std::stringstream convert;
122
119
convert << sub_str;
123
120
T occ;
124
121
convert >> occ;
125
122
vec.emplace_back (occ);
126
123
}
124
+
127
125
regfree (&sub_reg);
128
126
}
127
+
129
128
regfree (®);
130
129
}
131
130
0 commit comments