/** * Sorting should be pretty rare and should usually only include a few chunks. * We need to minimize the number of swaps, as those are expensive. * So, we do a min sort. */ static void do_the_sort(chunk_t **chunks, int num_chunks) { int start_idx, min_idx, idx; LOG_FMT(LSORT, "%s: %d chunks:", __func__, num_chunks); for (idx = 0; idx < num_chunks; idx++) { LOG_FMT(LSORT, " [%s]", chunks[idx]->str.c_str()); } LOG_FMT(LSORT, "\n"); for (start_idx = 0; start_idx < (num_chunks - 1); start_idx++) { /* Find the index of the minimum value */ min_idx = start_idx; for (idx = start_idx + 1; idx < num_chunks; idx++) { if (compare_chunks(chunks[idx], chunks[min_idx]) < 0) { min_idx = idx; } } /* Swap the lines if the minimum isn't the first entry */ if (min_idx != start_idx) { chunk_swap_lines(chunks[start_idx], chunks[min_idx]); /* Don't need to swap, since we only want the side-effects */ chunks[min_idx] = chunks[start_idx]; } } }
static void move_case_break(void) { LOG_FUNC_ENTRY(); chunk_t *prev = NULL; for (chunk_t *pc = chunk_get_head(); pc != NULL; pc = chunk_get_next_ncnl(pc)) { if ((pc->type == CT_BREAK) && (prev != NULL) && (prev->type == CT_BRACE_CLOSE) && (prev->parent_type == CT_CASE)) { if (chunk_is_newline(chunk_get_prev(pc)) && chunk_is_newline(chunk_get_prev(prev))) { chunk_swap_lines(prev, pc); } } prev = pc; } }