From 400789b8f710c0696231e5e2a3e6be27b7cd8861 Mon Sep 17 00:00:00 2001 From: agustux Date: Thu, 15 Oct 2026 08:06:45 -0500 Subject: [PATCH] Fix font header generation hang (#16) bin2header.cmake looped per byte and re-expanded the full hex string each iteration (O(n^2)), so the build appeared stuck on the bundled font. Replace the loop with two regex passes. Byte values and order are preserved: the first pass rewrites each hex pair in place as 0xNN,, and the second only inserts a newline and indent after every 12 entries. The only output differences are whitespace and a trailing comma, which is valid C. --- bin2header.cmake | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/bin2header.cmake b/bin2header.cmake index 3b7e883..c8c7177 100644 --- a/bin2header.cmake +++ b/bin2header.cmake @@ -4,31 +4,18 @@ # cmake -DINPUT= -DOUTPUT= -DARRAY_NAME= -P bin2header.cmake file(READ "${INPUT}" hex HEX) -string(LENGTH "${hex}" hex_len) -set(output "// Auto-generated from ${INPUT} — do not edit.\n") -string(APPEND output "static const unsigned char ${ARRAY_NAME}[] = {\n ") +# Convert each byte pair to "0xNN," in one regex pass. A per-byte +# loop is O(n^2) in CMake because every iteration re-expands the +# whole hex string, which made the build appear to hang on the font. +string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," bytes "${hex}") -math(EXPR last_byte "${hex_len} / 2 - 1") -set(col 0) -set(i 0) -while(i LESS hex_len) - math(EXPR next "${i} + 2") - string(SUBSTRING "${hex}" ${i} 2 byte) - math(EXPR byte_idx "${i} / 2") - if(byte_idx LESS last_byte) - string(APPEND output "0x${byte}, ") - else() - string(APPEND output "0x${byte}") - endif() - math(EXPR col "${col} + 1") - if(col EQUAL 12) - string(APPEND output "\n ") - set(col 0) - endif() - set(i ${next}) -endwhile() +# Wrap every 12 bytes. CMake's regex has no {n} quantifier, so the +# 12-byte pattern is built with string(REPEAT). The trailing comma +# after the last byte is valid C, so no special case is needed. +string(REPEAT "0x[0-9a-f][0-9a-f]," 12 row) +string(REGEX REPLACE "(${row})" "\\1\n " bytes "${bytes}") -string(APPEND output "\n};\n") - -file(WRITE "${OUTPUT}" "${output}") +file(WRITE "${OUTPUT}" + "// Auto-generated from ${INPUT} — do not edit.\n" + "static const unsigned char ${ARRAY_NAME}[] = {\n ${bytes}\n};\n")