File: converters.cpp

package info (click to toggle)
opm-common 2025.10%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 96,920 kB
  • sloc: cpp: 291,772; python: 3,609; sh: 198; xml: 174; pascal: 136; makefile: 12
file content (22 lines) | stat: -rw-r--r-- 822 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "converters.hpp"

namespace convert {

py::array numpy_string_array(const std::vector<std::string>& input) {
    const std::size_t target_length = 99;
    using numpy_string_t = char[target_length];
    auto output =  py::array_t<numpy_string_t>(input.size());
    auto output_ptr = reinterpret_cast<numpy_string_t *>(output.request().ptr);
    for (std::size_t index = 0; index < input.size(); index++) {
        if (input[index].size() > target_length)
            throw std::invalid_argument("Current implementation only works with 99 character strings");

        std::size_t length = input[index].size();
        std::strncpy(output_ptr[index], input[index].c_str(), length);
        for (std::size_t i = length; i < target_length; i++)
            output_ptr[index][i] = '\0';
    }
    return output;
}

}