Skip to content

Commit

Permalink
use fast_float to parse int
Browse files Browse the repository at this point in the history
  • Loading branch information
bb1950328 committed Jan 8, 2024
1 parent e2c81e2 commit b20f68b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
7 changes: 2 additions & 5 deletions src/connection/ldcad_meta/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "magic_enum.hpp"
#include "base.h"
#include <array>
#include <charconv>
#include <optional>
#include <vector>

Expand All @@ -21,10 +20,8 @@ namespace bricksim::connection::ldcad_meta::parse {
|| std::is_same_v<uint64_t, T>;

template<anyIntType T>
std::from_chars_result intFromString(std::string_view sv, T& value) {
const char* begin = &sv.front();
const char* end = &sv.back() + 1;
return std::from_chars(begin, end, value);
void intFromString(std::string_view sv, T& value) {
fast_float::from_chars(sv.front(), sv.back() - 1, value);
}

template<std::size_t N>
Expand Down
8 changes: 4 additions & 4 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ namespace bricksim::controller {
return 1;
}

openFile("test_files/bricks_test.ldr");
//openFile("test_files/bricks_test.ldr");
//openFile("test_files/connections.ldr");
//openFile("test_files/triangle_test.ldr");
//openFile("test_files/mpd_test.mpd");
Expand All @@ -484,10 +484,10 @@ namespace bricksim::controller {
//openFile("test_files/subfile_ref.ldr");
//openFile("3001.dat");
//openFile("car.ldr");
//openFile("~/ldraw/models/car_fixed.ldr");
//openFile(util::replaceSpecialPaths("~/ldraw/models/car_fixed.ldr"));
//openFile("10197.dat");
//openFile("~/Downloads/datsville.ldr");
//openFile("~/Downloads/42131_Bulldozer.mpd");
openFile(util::replaceSpecialPaths("~/Downloads/datsville.ldr"));
//openFile(util::replaceSpecialPaths("~/Downloads/42131_Bulldozer.mpd"));

std::vector<uint64_t> lastEditorRootNodeVersions;

Expand Down
36 changes: 21 additions & 15 deletions src/ldr/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ namespace bricksim::ldr {
CommentOrMetaElement::CommentOrMetaElement(const std::string_view line) :
content(line) {}

inline void parseNextInt(const std::string_view line, size_t& start, size_t& end, int& result) {
start = line.find_first_not_of(LDR_WHITESPACE, end);
end = std::min(line.size(), line.find_first_of(LDR_WHITESPACE, start));
fast_float::from_chars(line.data() + start, line.data() + end, result);
}

inline void parseNextFloat(const std::string_view line, size_t& start, size_t& end, float& result) {
start = line.find_first_not_of(LDR_WHITESPACE, end);
end = std::min(line.size(), line.find_first_of(LDR_WHITESPACE, start));
Expand All @@ -198,28 +204,28 @@ namespace bricksim::ldr {

SubfileReference::SubfileReference(const std::string_view line, const bool bfcInverted) :
bfcInverted(bfcInverted) {
size_t start = line.find_first_not_of(LDR_WHITESPACE);
size_t end = line.find_first_of(LDR_WHITESPACE, start);
std::from_chars(&line[start], &line[end], color.code);
std::size_t start;
std::size_t end = 0;
parseNextInt(line, start, end, color.code);
for (size_t n = 0; n < std::size(numbers); ++n) {
parseNextFloat(line, start, end, numbers[n]);
}
filename = stringutil::trim(line.substr(end + 1));
}

Line::Line(const std::string_view line) {
size_t start = line.find_first_not_of(LDR_WHITESPACE);
size_t end = line.find_first_of(LDR_WHITESPACE, start);
std::from_chars(&line[start], &line[end], color.code);
std::size_t start;
std::size_t end = 0;
parseNextInt(line, start, end, color.code);
for (size_t n = 0; n < std::size(coords); ++n) {
parseNextFloat(line, start, end, coords[n]);
}
}

Triangle::Triangle(const std::string_view line, const WindingOrder order) {
size_t start = line.find_first_not_of(LDR_WHITESPACE);
size_t end = line.find_first_of(LDR_WHITESPACE, start);
std::from_chars(&line[start], &line[end], color.code);
std::size_t start;
std::size_t end = 0;
parseNextInt(line, start, end, color.code);

parseNextThreeFloats(line, start, end, &coords[0]);//p1

Expand All @@ -233,9 +239,9 @@ namespace bricksim::ldr {
}

Quadrilateral::Quadrilateral(const std::string_view line, const WindingOrder order) {
size_t start = line.find_first_not_of(LDR_WHITESPACE);
size_t end = line.find_first_of(LDR_WHITESPACE, start);
std::from_chars(&line[start], &line[end], color.code);
std::size_t start;
std::size_t end = 0;
parseNextInt(line, start, end, color.code);

parseNextThreeFloats(line, start, end, &coords[0]);//p1

Expand All @@ -251,9 +257,9 @@ namespace bricksim::ldr {
}

OptionalLine::OptionalLine(const std::string_view line) {
size_t start = line.find_first_not_of(LDR_WHITESPACE);
size_t end = line.find_first_of(LDR_WHITESPACE, start);
std::from_chars(&line[start], &line[end], color.code);
std::size_t start;
std::size_t end = 0;
parseNextInt(line, start, end, color.code);

for (size_t i = 0; i < std::size(coords); ++i) {
parseNextFloat(line, start, end, coords[i]);
Expand Down

0 comments on commit b20f68b

Please sign in to comment.