8 std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
10 std::ifstream::pos_type fileSize = ifs.tellg();
11 ifs.seekg(0, std::ios::beg);
13 std::vector<char> bytes(fileSize);
14 ifs.read(bytes.data(), fileSize);
16 return std::string(bytes.data(), fileSize);
25 for (; i < str.size(); ++i)
28 if (c ==
'\r' or c ==
'\n')
34 std::string_view
result = str.substr(0, last);
35 str = str.substr(last+1);
48 if (not isdigit(str[begin_loc]))
return 1;
50 size_t loc = begin_loc;
51 while (loc < end_loc and isdigit(str[loc]))
83 if (not isdigit(str[begin_loc]))
return result = 1;
85 size_t loc = begin_loc;
86 while (loc < end_loc and isdigit(str[loc]))
102 if (begin_loc >= end_loc or not isalpha(str[begin_loc]))
return -1;
103 size_t loc = begin_loc;
104 while (loc < end_loc and (isdigit(str[loc]) or isalpha(str[loc]) or str[loc] ==
'_'))
108 std::string_view iden = str.substr(begin_loc, loc-begin_loc);
110 return map.
find(iden);
115 std::vector<std::string>
result;
116 for (
auto i = 0; i < line.size(); ++i)
119 if (not isalpha(c))
continue;
121 while (loc < line.size() and (isalpha(line[loc]) or isdigit(line[loc]) or line[loc] ==
'_'))
125 std::string iden {line.substr(i, loc-i)};
139 size_t begin_loc = 0;
140 size_t end_loc = str.size();
144 if (end_loc > begin_loc and str[begin_loc] ==
'[')
148 while (end_loc > begin_loc and str[end_loc-1] ==
' ') --end_loc;
149 if (end_loc > begin_loc and str[end_loc-1] ==
',') --end_loc;
150 if (end_loc > begin_loc and str[end_loc-1] ==
':') --end_loc;
151 if (end_loc > begin_loc and str[end_loc-1] ==
']') --end_loc;
153 if (begin_loc == end_loc)
return;
155 while (end_loc > begin_loc)
160 if (str[begin_loc] ==
'+')
165 if (str[begin_loc] ==
'-')
172 if (sign == -1) coeff = -coeff;
173 result.mCoefficients.push_back(coeff);
176 long loc =
result.mMonomials.size();
177 result.mMonomials.push_back(1);
182 while (end_loc > begin_loc)
184 char c = str[begin_loc];
185 if (c ==
'-' or c ==
'+')
190 if (begin_loc == end_loc)
201 throw parsing_error(
"expected an identifier at position " + std::to_string(begin_loc));
204 auto prev_loc = begin_loc;
208 throw parsing_error(
"expected a variable name at position " + std::to_string(prev_loc));
212 if (end_loc > begin_loc and str[begin_loc] ==
'^')
216 if (begin_loc >= end_loc or not std::isdigit(str[begin_loc]))
218 throw parsing_error(
"expected a digit at position " + std::to_string(begin_loc));
225 result.mMonomials.push_back(v);
226 result.mMonomials.push_back(e);
227 result.mMonomials[loc] += 2;
235 std::string_view str { poly};
245 std::string_view fileView { contents };
247 while (fileView.size() > 0)
249 std::string_view thisline =
next_line(fileView);
251 if (thisline.size() == 0 or thisline[0] ==
'#')
275 std::string varHeader {
"#variable order:"};
276 if (line.compare(0, varHeader.size(), varHeader) != 0)
278 line.remove_prefix(varHeader.size());
288 std::string_view fileView { contents };
291 while (fileView.size() > 0)
293 std::string_view thisline =
next_line(fileView);
303 idenMap = { idenList };
307 if (thisline.size() == 0 or thisline[0] ==
'#' or thisline[0] ==
']')
std::vector< BasicPoly > BasicPolyList
int readIdentifier(const std::string_view &str, const IdentifierHash &map, size_t &begin_loc, size_t end_loc)
bool lineContainsVars(std::string_view &line)
mpz_class readInteger_mpz_class(const std::string_view &str, size_t &begin_loc, size_t end_loc)
std::string_view next_line(std::string_view &str)
BasicPolyList parseMsolveFile(std::string filename)
std::string readEntireFile(const std::string &fileName)
std::vector< std::string > readIdentifierList(const std::string_view line)
void parseBasicPoly(const std::string_view &str, const IdentifierHash &idenHash, BasicPoly &result)
This version is a potentially faster alternative when reading many polynomials.
long readInteger_long(const std::string_view &str, size_t &begin_loc, size_t end_loc)
BasicPolyList parseMsolveFromString(std::string contents)
BasicPolyList parseBasicPolyListFromString(std::string contents, const IdentifierHash &idenMap)
Parsers from text (string or file) into a BasicPolyList, including the Msolve input format.
Standalone, self-contained polynomial representation independent of any engine Ring — coefficients ar...
auto find(std::string_view s) const -> int
IdentifierHash: used to facilitate parsing of polynomials from strings and files.
VALGRIND_MAKE_MEM_DEFINED & result(result)