MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / parse_header

Function parse_header

lite/example/cpp_example/npy.h:338–378  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

336} // namespace pyparse
337
338inline void parse_header(std::string header, std::string& descr) {
339 /*
340 The first 6 bytes are a magic string: exactly "x93NUMPY".
341 The next 1 byte is an unsigned byte: the major version number of the file
342 format, e.g. x01. The next 1 byte is an unsigned byte: the minor version
343 number of the file format, e.g. x00. Note: the version of the file format
344 is not tied to the version of the numpy package. The next 2 bytes form a
345 little-endian unsigned short int: the length of the header data
346 HEADER_LEN. The next HEADER_LEN bytes form the header data describing the
347 array's format. It is an ASCII string which contains a Python literal
348 expression of a dictionary. It is terminated by a newline ('n') and
349 padded with spaces
350 ('x20') to make the total length of the magic string + 4 + HEADER_LEN be
351 evenly divisible by 16 for alignment purposes. The dictionary contains
352 three keys:
353
354 "descr" : dtype.descr
355 An object that can be passed as an argument to the numpy.dtype()
356 constructor to create the array's dtype. For repeatability and
357 readability, this dictionary is formatted using pprint.pformat() so the
358 keys are in alphabetic order.
359 */
360
361 // remove trailing newline
362 if (header.back() != '\n')
363 fprintf(stderr, "invalid header");
364 header.pop_back();
365
366 // parse the dictionary
367 std::vector<std::string> keys{"descr"};
368 auto dict_map = npy::pyparse::parse_dict(header, keys);
369
370 if (dict_map.size() == 0)
371 fprintf(stderr, "invalid dictionary in header");
372
373 std::string descr_s = dict_map["descr"];
374 parse_typestring(descr_s);
375 // remove
376 descr = npy::pyparse::parse_str(descr_s);
377 return;
378}
379
380inline void parse_header(
381 std::string header, std::string& descr, bool& fortran_order,

Callers 1

LoadArrayFromNumpyFunction · 0.70

Calls 9

backMethod · 0.80
parse_dictFunction · 0.70
parse_typestringFunction · 0.70
parse_strFunction · 0.70
parse_boolFunction · 0.70
parse_tupleFunction · 0.70
pop_backMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected