Veja, por exemplo, estes dois fragmentos de código:
//...
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);
//...
Para este tipo de dados
(...)
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
(...)
eles funcionam e funcionam suficientemente rápido. Eles também geram avisos como:
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).
Como posso substituir a combinação aritmética strstr / ponteiro por algo que faria seu trabalho comparativamente rápido e não gerasse tais avisos? Eu tentei passar por questões semelhantes relacionadas a conversões std::string
-para- float
conversões, mas eles usaram std::stringstream
, o que é muito lento, ou presumiram que a string em questão continha apenas um valor.
editado para alterações após ativar o verificador de diretrizes básicas
Eu tentei usar std::from_chars
. Curiosidade: ainda não funciona no gcc ou clang para valores de ponto flutuante! Isso ... pode justificar não usá-lo até que o recurso esteja maduro.
Coisas para calar o verificador de diretrizes:
gsl::at
é o código da trapaça para aritmética de ponteiro. Nenhum avisostring_view
ainda tem vida infinita, mas não sinaliza se você usar literais de exibição de string ("" sv).string::find
para pesquisar e, em seguida, adicionando a posição astring::data()
using namespace std::literals;
const std::string file_string = "\n"
"v 1.000000 1.000000 -1.000000\n"
"v 1.000000 -1.000000 -1.000000\n"
"v 1.000000 1.000000 1.000000\n";
const auto tag = "v "sv;
const char* file_string_end = &gsl::at(file_string, file_string.size());
std::vector<vec::vec3<float>> vcoords;
std::string::size_type pos = 0;
while ((pos = file_string.find(tag, pos)) != std::string::npos) {
vec::vec3<float> buffer = { 0.0f };
auto [x_ptr, x_ec] = std::from_chars(
&gsl::at(file_string, pos + tag.size()),
file_string_end,
buffer.x);
if (x_ec != std::errc()) {
throw std::runtime_error("bad x");
}
std::string_view x_view(x_ptr);
auto [y_ptr, y_ec] = std::from_chars(
&gsl::at(x_view, 1),
file_string_end,
buffer.y);
if (y_ec != std::errc()) {
throw std::runtime_error("bad y");
}
std::string_view y_view(y_ptr);
auto [z_ptr, z_ec] = std::from_chars(
&gsl::at(y_view, 1),
file_string_end,
buffer.z);
if (z_ec != std::errc()) {
throw std::runtime_error("bad z");
}
vcoords.push_back(buffer);
}
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras