Take for example these two code fragments:
//...
file_string = strstr(file_string, "\nv ");
while (file_string = strstr(file_string, "v ")) {
vec::vec3<float> buffer = { 0.0f };
file_string += strlen("v ");
file_string = std::from_chars(file_string, file_string_end, buffer.x).ptr;
file_string++;
file_string = std::from_chars(file_string, file_string_end, buffer.y).ptr;
file_string++;
file_string = std::from_chars(file_string, file_string_end, buffer.z).ptr;
file_string++;
vcoords.push_back(std::move(buffer));
}
//...
//...
while (file_string = strstr(file_string, "v ")) {
size++;
file_string++;
}
vcoords.reserve(size);
//...
For this type of data
(...)
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
(...)
they work and they work sufficiently quickly. They also generate warnings such as:
C26481: Don't use pointer arithmetic.
C26486: Don't pass a pointer that may be invalid to a function. Parameter 0 'file_string' in call to 'strstr' may be invalid (lifetime.3).
How can I replace the strstr/pointer arithmetic mix with something that would do its job comparably fast and not generate such warnings? I tried going through similar questions related to std::string
-to-float
conversions but they either used std::stringstream
, which is very slow, or assumed that the string in question contained nothing but one value.