Skip to content

Commit

Permalink
Make TabularData::contentIdentityOf identify more aggressively
Browse files Browse the repository at this point in the history
  • Loading branch information
timangus committed Aug 15, 2023
1 parent 186e50f commit 987f579
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions source/shared/loading/tabulardata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ QString TabularData::contentIdentityOf(const QUrl& url)

const int maxLines = 50;
int numLinesScanned = 0;
std::map<char, size_t> counts;

std::istream* is = nullptr;
do
Expand All @@ -256,7 +257,6 @@ QString TabularData::contentIdentityOf(const QUrl& url)

is = &u::getline(file, line);

std::map<char, size_t> counts;
bool inQuotes = false;

for(auto character : line)
Expand All @@ -278,25 +278,24 @@ QString TabularData::contentIdentityOf(const QUrl& url)
}
}

if(!counts.empty())
{
auto maxCount = std::max_element(counts.begin(), counts.end(),
[](const auto& a, const auto& b) { return a.second < b.second; });
numLinesScanned++;
} while(!is->fail() && !is->eof() &&
numLinesScanned < maxLines);

auto character = maxCount->first;
if(!counts.empty())
{
auto maxCount = std::max_element(counts.begin(), counts.end(),
[](const auto& a, const auto& b) { return a.second < b.second; });

switch(character)
{
case ',': identity = u"CSV"_s; break;
case ';': identity = u"SSV"_s; break;
case '\t': identity = u"TSV"_s; break;
}
}
auto character = maxCount->first;

numLinesScanned++;
} while(identity.isEmpty() &&
!is->fail() && !is->eof() &&
numLinesScanned < maxLines);
switch(character)
{
case ',': identity = u"CSV"_s; break;
case ';': identity = u"SSV"_s; break;
case '\t': identity = u"TSV"_s; break;
}
}

return identity;
}

0 comments on commit 987f579

Please sign in to comment.