Merge branch 'rs/optim-text-wrap' into maint
authorJunio C Hamano <gitster@pobox.com>
Fri, 5 Mar 2010 06:26:33 +0000 (22:26 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 5 Mar 2010 06:26:33 +0000 (22:26 -0800)
* rs/optim-text-wrap:
utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
utf8.c: remove strbuf_write()
utf8.c: remove print_spaces()
utf8.c: remove print_wrapped_text()

1  2 
builtin-shortlog.c
utf8.c
utf8.h
diff --combined builtin-shortlog.c
index b3b055f68ce59b6b91ef6949bd8c4bd0bed68b55,d96858f9adcf185e65d6028634bd981430a0f193..ecd2d45a00b1aacf9d3a19e2f934a14ca7e58c35
@@@ -139,12 -139,8 +139,12 @@@ static void read_from_stdin(struct shor
  void shortlog_add_commit(struct shortlog *log, struct commit *commit)
  {
        const char *author = NULL, *buffer;
 +      struct strbuf buf = STRBUF_INIT;
 +      struct strbuf ufbuf = STRBUF_INIT;
 +      struct pretty_print_context ctx = {0};
  
 -      buffer = commit->buffer;
 +      pretty_print_commit(CMIT_FMT_RAW, commit, &buf, &ctx);
 +      buffer = buf.buf;
        while (*buffer && *buffer != '\n') {
                const char *eol = strchr(buffer, '\n');
  
                die("Missing author: %s",
                    sha1_to_hex(commit->object.sha1));
        if (log->user_format) {
 -              struct strbuf buf = STRBUF_INIT;
                struct pretty_print_context ctx = {0};
                ctx.abbrev = DEFAULT_ABBREV;
                ctx.subject = "";
                ctx.after_subject = "";
                ctx.date_mode = DATE_NORMAL;
 -              pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf, &ctx);
 -              insert_one_record(log, author, buf.buf);
 -              strbuf_release(&buf);
 -              return;
 -      }
 -      if (*buffer)
 +              pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx);
 +              buffer = ufbuf.buf;
 +      } else if (*buffer) {
                buffer++;
 +      }
        insert_one_record(log, author, !*buffer ? "<none>" : buffer);
 +      strbuf_release(&ufbuf);
 +      strbuf_release(&buf);
  }
  
  static void get_from_rev(struct rev_info *rev, struct shortlog *log)
@@@ -304,9 -301,19 +304,19 @@@ parse_done
        return 0;
  }
  
+ static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
+                                    const struct shortlog *log)
+ {
+       int col = strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
+       if (col != log->wrap)
+               strbuf_addch(sb, '\n');
+ }
  void shortlog_output(struct shortlog *log)
  {
        int i, j;
+       struct strbuf sb = STRBUF_INIT;
        if (log->sort_by_number)
                qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
                        compare_by_number);
                                const char *msg = onelines->items[j].string;
  
                                if (log->wrap_lines) {
-                                       int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
-                                       if (col != log->wrap)
-                                               putchar('\n');
+                                       strbuf_reset(&sb);
+                                       add_wrapped_shortlog_msg(&sb, msg, log);
+                                       fwrite(sb.buf, sb.len, 1, stdout);
                                }
                                else
                                        printf("      %s\n", msg);
                log->list.items[i].util = NULL;
        }
  
+       strbuf_release(&sb);
        log->list.strdup_strings = 1;
        string_list_clear(&log->list, 1);
        clear_mailmap(&log->mailmap);
diff --combined utf8.c
index ab326ac83e0d9e81b06abff58b00a98341adcd36,6db9cd9a078266538d8289a98b49d4ec82d39ae7..84cfc72e6db144880febad0a4ffa64800919fb6d
--- 1/utf8.c
--- 2/utf8.c
+++ b/utf8.c
@@@ -163,7 -163,7 +163,7 @@@ static int git_wcwidth(ucs_char_t ch
   * If the string was not a valid UTF-8, *start pointer is set to NULL
   * and the return value is undefined.
   */
 -ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p)
 +static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p)
  {
        unsigned char *s = (unsigned char *)*start;
        ucs_char_t ch;
@@@ -280,22 -280,11 +280,11 @@@ int is_utf8(const char *text
        return 1;
  }
  
- static inline void strbuf_write(struct strbuf *sb, const char *buf, int len)
+ static void strbuf_addchars(struct strbuf *sb, int c, size_t n)
  {
-       if (sb)
-               strbuf_insert(sb, sb->len, buf, len);
-       else
-               fwrite(buf, len, 1, stdout);
- }
- static void print_spaces(struct strbuf *buf, int count)
- {
-       static const char s[] = "                    ";
-       while (count >= sizeof(s)) {
-               strbuf_write(buf, s, sizeof(s) - 1);
-               count -= sizeof(s) - 1;
-       }
-       strbuf_write(buf, s, count);
+       strbuf_grow(sb, n);
+       memset(sb->buf + sb->len, c, n);
+       strbuf_setlen(sb, sb->len + n);
  }
  
  static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
                const char *eol = strchrnul(text, '\n');
                if (*eol == '\n')
                        eol++;
-               print_spaces(buf, indent);
-               strbuf_write(buf, text, eol - text);
+               strbuf_addchars(buf, ' ', indent);
+               strbuf_add(buf, text, eol - text);
                text = eol;
                indent = indent2;
        }
@@@ -335,16 -324,21 +324,21 @@@ static size_t display_mode_esc_sequence
   * consumed (and no extra indent is necessary for the first line).
   */
  int strbuf_add_wrapped_text(struct strbuf *buf,
-               const char *text, int indent, int indent2, int width)
+               const char *text, int indent1, int indent2, int width)
  {
-       int w = indent, assume_utf8 = is_utf8(text);
-       const char *bol = text, *space = NULL;
+       int indent, w, assume_utf8 = 1;
+       const char *bol, *space, *start = text;
+       size_t orig_len = buf->len;
  
        if (width <= 0) {
-               strbuf_add_indented_text(buf, text, indent, indent2);
+               strbuf_add_indented_text(buf, text, indent1, indent2);
                return 1;
        }
  
+ retry:
+       bol = text;
+       w = indent = indent1;
+       space = NULL;
        if (indent < 0) {
                w = -indent;
                space = text;
                                if (space)
                                        start = space;
                                else
-                                       print_spaces(buf, indent);
-                               strbuf_write(buf, start, text - start);
+                                       strbuf_addchars(buf, ' ', indent);
+                               strbuf_add(buf, start, text - start);
                                if (!c)
                                        return w;
                                space = text;
                                else if (c == '\n') {
                                        space++;
                                        if (*space == '\n') {
-                                               strbuf_write(buf, "\n", 1);
+                                               strbuf_addch(buf, '\n');
                                                goto new_line;
                                        }
                                        else if (!isalnum(*space))
                                                goto new_line;
                                        else
-                                               strbuf_write(buf, " ", 1);
+                                               strbuf_addch(buf, ' ');
                                }
                                w++;
                                text++;
                        }
                        else {
  new_line:
-                               strbuf_write(buf, "\n", 1);
+                               strbuf_addch(buf, '\n');
                                text = bol = space + isspace(*space);
                                space = NULL;
                                w = indent = indent2;
                        }
                        continue;
                }
-               if (assume_utf8)
+               if (assume_utf8) {
                        w += utf8_width(&text, NULL);
-               else {
+                       if (!text) {
+                               assume_utf8 = 0;
+                               text = start;
+                               strbuf_setlen(buf, orig_len);
+                               goto retry;
+                       }
+               } else {
                        w++;
                        text++;
                }
        }
  }
  
- int print_wrapped_text(const char *text, int indent, int indent2, int width)
- {
-       return strbuf_add_wrapped_text(NULL, text, indent, indent2, width);
- }
  int is_encoding_utf8(const char *name)
  {
        if (!name)
diff --combined utf8.h
index c9738d83d991d89bbdd4c5a6442fcad2fdaaa4df,b09687d500b89d4f3a0644fea8b1eff420e0748a..ebc4d2fa85113c971ab4c4eaa52537a688a03745
--- 1/utf8.h
--- 2/utf8.h
+++ b/utf8.h
@@@ -3,12 -3,12 +3,11 @@@
  
  typedef unsigned int ucs_char_t;  /* assuming 32bit int */
  
 -ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p);
  int utf8_width(const char **start, size_t *remainder_p);
  int utf8_strwidth(const char *string);
  int is_utf8(const char *text);
  int is_encoding_utf8(const char *name);
  
- int print_wrapped_text(const char *text, int indent, int indent2, int len);
  int strbuf_add_wrapped_text(struct strbuf *buf,
                const char *text, int indent, int indent2, int width);