Skip to content

Commit

Permalink
Merge branch 'lematt1991-read-scalar'
Browse files Browse the repository at this point in the history
  • Loading branch information
carl committed Jun 1, 2018
2 parents e6536ed + b4090bd commit 4e8810b
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions cnpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include<iomanip>
#include<stdint.h>
#include<stdexcept>
#include <regex>

char cnpy::BigEndianTest() {
int x = 1;
Expand Down Expand Up @@ -74,15 +75,15 @@ void cnpy::parse_npy_header(unsigned char* buffer,size_t& word_size, std::vector
//shape
loc1 = header.find("(");
loc2 = header.find(")");

std::regex num_regex("[0-9][0-9]*");
std::smatch sm;
shape.clear();

std::string str_shape = header.substr(loc1+1,loc2-loc1-1);
size_t ndims;
if(str_shape[str_shape.size()-1] == ',') ndims = 1;
else ndims = std::count(str_shape.begin(),str_shape.end(),',')+1;
shape.resize(ndims);
for(size_t i = 0;i < ndims;i++) {
loc1 = str_shape.find(",");
shape[i] = atoi(str_shape.substr(0,loc1).c_str());
str_shape = str_shape.substr(loc1+1);
while(std::regex_search(str_shape, sm, num_regex)) {
shape.push_back(std::stoi(sm[0].str()));
str_shape = sm.suffix().str();
}

//endian, word size, data type
Expand Down Expand Up @@ -122,16 +123,15 @@ void cnpy::parse_npy_header(FILE* fp, size_t& word_size, std::vector<size_t>& sh
loc2 = header.find(")");
if (loc1 == std::string::npos || loc2 == std::string::npos)
throw std::runtime_error("parse_npy_header: failed to find header keyword: '(' or ')'");


std::regex num_regex("[0-9][0-9]*");
std::smatch sm;
shape.clear();

std::string str_shape = header.substr(loc1+1,loc2-loc1-1);
size_t ndims;
if(str_shape[str_shape.size()-1] == ',') ndims = 1;
else ndims = std::count(str_shape.begin(),str_shape.end(),',')+1;
shape.resize(ndims);
for(size_t i = 0;i < ndims;i++) {
loc1 = str_shape.find(",");
shape[i] = atoi(str_shape.substr(0,loc1).c_str());
str_shape = str_shape.substr(loc1+1);
while(std::regex_search(str_shape, sm, num_regex)) {
shape.push_back(std::stoi(sm[0].str()));
str_shape = sm.suffix().str();
}

//endian, word size, data type
Expand Down

0 comments on commit 4e8810b

Please sign in to comment.