Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ size_t find_split_points(const char *content, size_t limit, size_t *split_points
size_t content_length = strlen(content);
size_t current = 0;
size_t found_splits = 0;
size_t marker_len = strlen("\n### 📄");

while (current + limit < content_length && found_splits < max_splits) {
// Start looking for a split point well before the limit to ensure we don't split a file
Expand All @@ -424,7 +425,8 @@ size_t find_split_points(const char *content, size_t limit, size_t *split_points
// Find the next occurrence of "### 📄" which indicates the start of a file
const char *file_marker = NULL;
for (size_t i = search_start; i < current + limit && i < content_length; i++) {
if (i + 6 < content_length && strncmp(content + i, "\n### 📄", 6) == 0) {
if (i + marker_len < content_length &&
strncmp(content + i, "\n### 📄", marker_len) == 0) {
file_marker = content + i;
break;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/test_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "gitignore.h"
#include "stats.h"

// Forward declaration from writer.c for direct unit testing
size_t find_split_points(const char *content, size_t limit, size_t *split_points, size_t max_splits);

/**
* @brief Test that verifies smart splitting preserves documented files.
*/
Expand Down Expand Up @@ -99,9 +102,35 @@ void test_smart_split() {
printf("✔ test_smart_split passed\n");
}

/**
* @brief Ensure splitting only triggers on the exact UTF-8 marker.
*/
void test_split_marker_length() {
// Build a long prefix so the false marker is inside the search window
char prefix[70];
memset(prefix, 'A', sizeof(prefix) - 1);
prefix[sizeof(prefix) - 1] = '\0';

char content[512];
snprintf(content, sizeof(content),
"%s\n### 📝 Wrong marker\nSome filler text to extend length\n\n### 📄 Correct marker\nEnd\n",
prefix);

size_t points[2];
size_t splits = find_split_points(content, 120, points, 2);

const char *wrong = strstr(content, "\n### 📝");
assert(wrong != NULL);
assert(splits == 1);
assert(points[0] > (size_t)(wrong - content + 1));

printf("✔ test_split_marker_length passed\n");
}

// Run function for the split tests
void run_split_tests() {
printf("Running split tests...\n");
test_split_marker_length();
test_smart_split();
printf("All split tests passed!\n");
}
Expand Down