/* * How many columns do we need to show line numbers, authors, * and filenames? */ static void find_alignment(struct blame_scoreboard *sb, int *option) { int longest_src_lines = 0; int longest_dst_lines = 0; unsigned largest_score = 0; struct blame_entry *e; int compute_auto_abbrev = (abbrev < 0); int auto_abbrev = DEFAULT_ABBREV; for (e = sb->ent; e; e = e->next) { struct blame_origin *suspect = e->suspect; int num; if (compute_auto_abbrev) auto_abbrev = update_auto_abbrev(auto_abbrev, suspect); if (strcmp(suspect->path, sb->path)) *option |= OUTPUT_SHOW_NAME; num = strlen(suspect->path); if (longest_file < num) longest_file = num; if (!(suspect->commit->object.flags & METAINFO_SHOWN)) { struct commit_info ci; suspect->commit->object.flags |= METAINFO_SHOWN; get_commit_info(suspect->commit, &ci, 1); if (*option & OUTPUT_SHOW_EMAIL) num = utf8_strwidth(ci.author_mail.buf); else num = utf8_strwidth(ci.author.buf); if (longest_author < num) longest_author = num; commit_info_destroy(&ci); } num = e->s_lno + e->num_lines; if (longest_src_lines < num) longest_src_lines = num; num = e->lno + e->num_lines; if (longest_dst_lines < num) longest_dst_lines = num; if (largest_score < blame_entry_score(sb, e)) largest_score = blame_entry_score(sb, e); } max_orig_digits = decimal_width(longest_src_lines); max_digits = decimal_width(longest_dst_lines); max_score_digits = decimal_width(largest_score); if (compute_auto_abbrev) /* one more abbrev length is needed for the boundary commit */ abbrev = auto_abbrev + 1; }
static void output(struct string_list *a, struct string_list *b, struct diff_options *diffopt) { struct strbuf buf = STRBUF_INIT, dashes = STRBUF_INIT; int patch_no_width = decimal_width(1 + (a->nr > b->nr ? a->nr : b->nr)); int i = 0, j = 0; /* * We assume the user is really more interested in the second argument * ("newer" version). To that end, we print the output in the order of * the RHS (the `b` parameter). To put the LHS (the `a` parameter) * commits that are no longer in the RHS into a good place, we place * them once we have shown all of their predecessors in the LHS. */ while (i < a->nr || j < b->nr) { struct patch_util *a_util, *b_util; a_util = i < a->nr ? a->items[i].util : NULL; b_util = j < b->nr ? b->items[j].util : NULL; /* Skip all the already-shown commits from the LHS. */ while (i < a->nr && a_util->shown) a_util = ++i < a->nr ? a->items[i].util : NULL; /* Show unmatched LHS commit whose predecessors were shown. */ if (i < a->nr && a_util->matching < 0) { output_pair_header(diffopt, patch_no_width, &buf, &dashes, a_util, NULL); i++; continue; } /* Show unmatched RHS commits. */ while (j < b->nr && b_util->matching < 0) { output_pair_header(diffopt, patch_no_width, &buf, &dashes, NULL, b_util); b_util = ++j < b->nr ? b->items[j].util : NULL; } /* Show matching LHS/RHS pair. */ if (j < b->nr) { a_util = a->items[b_util->matching].util; output_pair_header(diffopt, patch_no_width, &buf, &dashes, a_util, b_util); if (!(diffopt->output_format & DIFF_FORMAT_NO_OUTPUT)) patch_diff(a->items[b_util->matching].string, b->items[j].string, diffopt); a_util->shown = 1; j++; } } strbuf_release(&buf); strbuf_release(&dashes); }
int main() { int i, j; int c, state; int counter; int max; int width, len; int buf[MAX_WORD_LEN]; /* * Dirty hacks. * Cause I don't need to count the zero length word and I'm a miser. * But there is one more varible(a pointer, maybe 4 bytes on 32-bit OS * and 8 bytes on 64-bit) I needed to use. I pray it would be allocated * in registers. */ int *histogram = &buf[-1]; for (i = 1; i <= MAX_WORD_LEN; ++i) histogram[i] = 0; counter = 0; state = OUT; while ((c = getchar()) != EOF) { if (c == ' ' || c == '\n' || c == '\t') { if (state == IN) inc_histogram(histogram, counter); counter = 0; state = OUT; } else { ++counter; state = IN; } } /* Count the last word */ if (state == IN) inc_histogram(histogram, counter); /* * Calcuate the max lengths of words * For the width of printf() */ for (i = MAX_WORD_LEN; i >= 1; --i) { if (histogram[i]) { len = i; break; } } width = decimal_width(len); /* Horizontal histogram */ printf("Word length horizontal histogram\n"); printf("--------------------------------\n"); for (i = 1; i <= MAX_WORD_LEN; ++i) { if (histogram[i]) { printf("%*d: ", width, i); for (j = 1; j <= histogram[i]; ++j) printf("="); printf("\n"); } } printf("\n"); /* Vertical histogram */ max = 0; for (i = 1; i <= MAX_WORD_LEN; ++i) { if (max < histogram[i]) max = histogram[i]; } printf("Word length vertical histogram\n"); printf("------------------------------\n"); for (i = max; i >= 1; --i) { for (j = 1; j <= MAX_WORD_LEN; ++j) { if (histogram[j]) { if (histogram[j] >= i) printf("%-*c ", width, '*'); else printf("%-*c ", width, ' '); } } printf("\n"); } for (i = 1; i <= MAX_WORD_LEN; ++i) { if (histogram[i]) { len = decimal_width(i); /* C99 VLA, I really don't want to use it. */ char str[len + 1]; memset(str, '-', len); str[len] = '\0'; printf("%-*s ", width, str); } } printf("\n"); for (i = 1; i <= MAX_WORD_LEN; ++i) { if (histogram[i]) printf("%-*d ", width, i); } printf("\n"); return 0; }