Speed up encoding check (#10873)

Update encoding check
This commit is contained in:
Ocraftyone
2026-05-15 11:45:39 -04:00
committed by GitHub
parent 2167378bbe
commit 4154785025
2 changed files with 31 additions and 25 deletions

View File

@@ -31,16 +31,11 @@ function(encoding_check TARGET)
# Define top-level encoding check target for this ${TARGET}
add_custom_target(encoding-check-${TARGET}
DEPENDS encoding-check ${T_SOURCES}
COMMAND $<TARGET_FILE:encoding-check> ${TARGET} ${T_SOURCES}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Checking source files encodings for target ${TARGET}"
)
# Add checking of each source file as a subcommand of encoding-check-${TARGET}
foreach(file ${T_SOURCES})
add_custom_command(TARGET encoding-check-${TARGET} PRE_BUILD
COMMAND $<TARGET_FILE:encoding-check> ${TARGET} ${file}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endforeach()
# This adds dependency on encoding-check-${TARGET} to ${TARET}
# via the global-encoding-check

View File

@@ -69,28 +69,24 @@ unsigned char *utf8_check(unsigned char *s)
}
int main(int argc, char const *argv[])
static const char* target;
void error_exit(const char* error, const char* filename)
{
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <program/library> <file>" << std::endl;
return -1;
}
std::cerr << "\n\tError: " << error << ": " << filename << "\n"
<< "\tTarget: " << target << "\n"
<< std::endl;
std::exit(-2);
}
const char* target = argv[1];
const char* filename = argv[2];
const auto error_exit = [=](const char* error) {
std::cerr << "\n\tError: " << error << ": " << filename << "\n"
<< "\tTarget: " << target << "\n"
<< std::endl;
std::exit(-2);
};
void utf8_check_file(const char* filename)
{
std::ifstream file(filename, std::ios::binary | std::ios::ate);
const auto size = file.tellg();
if (size == 0) {
return 0;
return;
}
file.seekg(0, std::ios::beg);
@@ -101,7 +97,7 @@ int main(int argc, char const *argv[])
// Check UTF-8 validity
if (utf8_check(reinterpret_cast<unsigned char*>(buffer.data())) != nullptr) {
error_exit("Source file does not contain (valid) UTF-8");
error_exit("Source file does not contain (valid) UTF-8", filename);
}
// Check against a BOM mark
@@ -109,10 +105,25 @@ int main(int argc, char const *argv[])
&& buffer[0] == '\xef'
&& buffer[1] == '\xbb'
&& buffer[2] == '\xbf') {
error_exit("Source file is valid UTF-8 but contains a BOM mark");
error_exit("Source file is valid UTF-8 but contains a BOM mark", filename);
}
} else {
error_exit("Could not read source file");
error_exit("Could not read source file", filename);
}
}
int main(int argc, char const *argv[])
{
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <program/library> <file[s]>" << std::endl;
return -1;
}
target = argv[1];
for (int i = 2; i < argc; i++) {
utf8_check_file(argv[i]);
}
return 0;