26 big_endian = (descr.
front() ==
'>');
32 static const regex::regex field_expr(
"\\('([^']*)',\\s*'([^']*)'\\)");
35 auto start = descr.
begin();
36 auto end = descr.
end();
38 while (regex::regex_search(start, end, match, field_expr)) {
45 big_endian = endian_aux;
46 }
else if (dtype != dtype_aux || big_endian != endian_aux) {
51 start = match[0].second;
57 auto loc = header.
find(
"fortran_order") + 16;
58 fortran_order = (header.
substr(loc, 4) ==
"True");
60 loc = header.
find(
"descr") + 8;
62 if (header[loc] ==
'\'') {
63 auto end = header.
find(
'\'', loc + 1);
65 }
else if (header[loc] ==
'[') {
66 auto end = header.
find(
']', loc + 1);
72 loc = header.
find(
"shape") + 9;
73 auto loc2 = header.
find(
')', loc);
74 auto shape_str = header.
substr(loc, loc2 - loc);
75 if (shape_str.back() ==
',')
76 shape_str.resize(shape_str.size() - 1);
77 shape = stringToVector<size_t>(shape_str);
85 input.
read(magic,
sizeof(magic));
91 little_uint32_t header_len;
92 little_uint16_t version;
93 input.
read(
reinterpret_cast<char*
>(&version),
sizeof(version));
96 }
else if (version.data()[0] == 1) {
99 input.
read(
reinterpret_cast<char*
>(&aux),
sizeof(aux));
103 input.
read(
reinterpret_cast<char*
>(&header_len),
sizeof(header_len));
108 input.
read(&header[0], header_len);
111 bool fortran_order, big_endian;
112 parseNpyDict(header, fortran_order, big_endian, dtype, shape, attrs, n_elements);
117 if ((big_endian && (BYTE_ORDER != BIG_ENDIAN)) || (!big_endian && (BYTE_ORDER != LITTLE_ENDIAN)))
T emplace_back(T... args)
void parseSingleValue(const std::string &descr, bool &big_endian, std::string &dtype)
void readNpyHeader(std::istream &input, std::string &dtype, std::vector< size_t > &shape, std::vector< std::string > &attrs, size_t &n_elements)
void parseFieldValues(const std::string &descr, bool &big_endian, std::vector< std::string > &attrs, std::string &dtype)
void parseNpyDict(const std::string &header, bool &fortran_order, bool &big_endian, std::string &dtype, std::vector< size_t > &shape, std::vector< std::string > &attrs, size_t &n_elements)
constexpr const char NPY_MAGIC[]