Changeset 7e111f4


Ignore:
Timestamp:
Feb 4, 2011, 3:49:44 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
9de316d1
Parents:
d3c318b
git-author:
David Benjamin <davidben@mit.edu> (01/24/11 20:58:41)
git-committer:
David Benjamin <davidben@mit.edu> (02/04/11 15:49:44)
Message:
Replace owl_fmtext's buffer management with GString

reallocs are hard. Let's go shopping^Wuse GString!
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    rd3c318b r7e111f4  
    66void owl_fmtext_init_null(owl_fmtext *f)
    77{
    8   f->textlen = 0;
    9   f->bufflen = 5;
    10   f->textbuff = owl_malloc(5);
    11   f->textbuff[0] = 0;
     8  f->buff = g_string_new("");
    129  f->default_attrs = OWL_FMTEXT_ATTR_NONE;
    1310  f->default_fgcolor = OWL_COLOR_DEFAULT;
     
    1916void owl_fmtext_clear(owl_fmtext *f)
    2017{
    21   f->textlen = 0;
    22   f->textbuff[0] = 0;
     18  g_string_truncate(f->buff, 0);
    2319  f->default_attrs = OWL_FMTEXT_ATTR_NONE;
    2420  f->default_fgcolor = OWL_COLOR_DEFAULT;
    2521  f->default_bgcolor = OWL_COLOR_DEFAULT;
    26 }
    27 
    28 static void _owl_fmtext_realloc(owl_fmtext *f, int newlen)
    29 {
    30     if(newlen + 1 > f->bufflen) {
    31       f->textbuff = owl_realloc(f->textbuff, newlen + 1);
    32       f->bufflen = newlen+1;
    33   }
    3422}
    3523
     
    4533void owl_fmtext_append_attr(owl_fmtext *f, const char *text, char attr, short fgcolor, short bgcolor)
    4634{
    47   char attrbuff[6];
    48   int newlen, a = 0, fg = 0, bg = 0;
     35  int a = 0, fg = 0, bg = 0;
    4936 
    5037  if (attr != OWL_FMTEXT_ATTR_NONE) a=1;
     
    5239  if (bgcolor != OWL_COLOR_DEFAULT) bg=1;
    5340
    54   /* Plane-16 characters in UTF-8 are 4 bytes long. */
    55   newlen = strlen(f->textbuff) + strlen(text) + (8 * (a + fg + bg));
    56   _owl_fmtext_realloc(f, newlen);
    57 
    5841  /* Set attributes */
    5942  if (a)
    60     strncat(f->textbuff, attrbuff,
    61             g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff));
     43    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr);
    6244  if (fg)
    63     strncat(f->textbuff, attrbuff,
    64             g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff));
     45    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor);
    6546  if (bg)
    66     strncat(f->textbuff, attrbuff,
    67             g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff));
    68  
    69   strcat(f->textbuff, text);
     47    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor);
     48
     49  g_string_append(f->buff, text);
    7050
    7151  /* Reset attributes */
    72   if (bg) strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT);
    73   if (fg) strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT);
    74   if (a)  strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE);
    75   f->textlen=newlen;
     52  if (bg) g_string_append(f->buff, OWL_FMTEXT_UTF8_BGDEFAULT);
     53  if (fg) g_string_append(f->buff, OWL_FMTEXT_UTF8_FGDEFAULT);
     54  if (a)  g_string_append(f->buff, OWL_FMTEXT_UTF8_ATTR_NONE);
    7655}
    7756
     
    168147{
    169148  const char *p;
    170   p = strchr(f->textbuff, OWL_FMTEXT_UC_STARTBYTE_UTF8);
    171   while (p && p < f->textbuff + start) {
     149  p = strchr(f->buff->str, OWL_FMTEXT_UC_STARTBYTE_UTF8);
     150  while (p && p < f->buff->str + start) {
    172151    _owl_fmtext_update_attributes(g_utf8_get_char(p), attr, fgcolor, bgcolor);
    173152    p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
     
    181160static void _owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in, int start, int stop)
    182161{
    183   char attrbuff[6];
    184   int newlen, a = 0, fg = 0, bg = 0;
     162  int a = 0, fg = 0, bg = 0;
    185163  char attr = 0;
    186164  short fgcolor = OWL_COLOR_DEFAULT;
     
    192170  if (bgcolor != OWL_COLOR_DEFAULT) bg=1;
    193171
    194   /* We will reset to defaults after appending the text. We may need
    195      to set initial attributes. */
    196   newlen=strlen(f->textbuff)+(stop-start) + (4 * (a + fg + bg)) + 12;
    197   _owl_fmtext_realloc(f, newlen);
    198 
    199172  if (a)
    200     strncat(f->textbuff, attrbuff,
    201             g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff));
     173    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr);
    202174  if (fg)
    203     strncat(f->textbuff, attrbuff,
    204             g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff));
     175    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor);
    205176  if (bg)
    206     strncat(f->textbuff, attrbuff,
    207             g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff));
    208 
    209   strncat(f->textbuff, in->textbuff+start, stop-start);
     177    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor);
     178
     179  g_string_append_len(f->buff, in->buff->str+start, stop-start);
    210180
    211181  /* Reset attributes */
    212   strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT);
    213   strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT);
    214   strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE);
    215 
    216   f->textbuff[newlen]='\0';
    217   f->textlen=newlen;
     182  g_string_append(f->buff, OWL_FMTEXT_UTF8_BGDEFAULT);
     183  g_string_append(f->buff, OWL_FMTEXT_UTF8_FGDEFAULT);
     184  g_string_append(f->buff, OWL_FMTEXT_UTF8_ATTR_NONE);
    218185}
    219186
     
    221188void owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in)
    222189{
    223   _owl_fmtext_append_fmtext(f, in, 0, in->textlen);
     190  _owl_fmtext_append_fmtext(f, in, 0, in->buff->len);
    224191
    225192}
     
    239206char *owl_fmtext_print_plain(const owl_fmtext *f)
    240207{
    241   return owl_strip_format_chars(f->textbuff);
     208  return owl_strip_format_chars(f->buff->str);
    242209}
    243210
     
    281248  }
    282249
    283   s = f->textbuff;
     250  s = f->buff->str;
    284251  /* Set default attributes. */
    285252  attr = f->default_attrs;
     
    375342  out->default_bgcolor = in->default_bgcolor;
    376343
    377   for (ptr = in->textbuff;
    378        ptr < in->textbuff + in->textlen;
     344  for (ptr = in->buff->str;
     345       ptr < in->buff->str + in->buff->len;
    379346       ptr = g_utf8_next_char(ptr)) {
    380347    gunichar c = g_utf8_get_char(ptr);
     
    382349    if (c == '\t') {
    383350      /* Copy up to this tab */
    384       _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->textbuff);
     351      _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->buff->str);
    385352      /* and then copy spaces for the tab. */
    386353      chwidth = OWL_TAB_WIDTH - (col % OWL_TAB_WIDTH);
    387354      owl_fmtext_append_spaces(out, chwidth);
    388355      col += chwidth;
    389       numcopied = g_utf8_next_char(ptr) - in->textbuff;
     356      numcopied = g_utf8_next_char(ptr) - in->buff->str;
    390357    } else {
    391358      /* Just update col. We'll append later. */
     
    398365  }
    399366  /* Append anything we've missed. */
    400   if (numcopied < in->textlen)
    401     _owl_fmtext_append_fmtext(out, in, numcopied, in->textlen);
     367  if (numcopied < in->buff->len)
     368    _owl_fmtext_append_fmtext(out, in, numcopied, in->buff->len);
    402369}
    403370
     
    411378 
    412379  /* find the starting line */
    413   ptr1 = in->textbuff;
     380  ptr1 = in->buff->str;
    414381  for (i = 0; i < aline; i++) {
    415382    ptr1 = strchr(ptr1, '\n');
     
    429396
    430397  for (i = 0; i < lines; i++) {
    431     offset = ptr1 - in->textbuff;
     398    offset = ptr1 - in->buff->str;
    432399    ptr2 = strchr(ptr1, '\n');
    433400    if (!ptr2) {
    434401      /* Copy to the end of the buffer. */
    435       _owl_fmtext_append_fmtext(out, in, offset, in->textlen);
     402      _owl_fmtext_append_fmtext(out, in, offset, in->buff->len);
    436403      return(-1);
    437404    }
     
    454421  out->default_bgcolor = in->default_bgcolor;
    455422
    456   last=in->textbuff+in->textlen-1;
    457   ptr_s=in->textbuff;
     423  last = in->buff->str + in->buff->len - 1;
     424  ptr_s = in->buff->str;
    458425  while (ptr_s <= last) {
    459426    ptr_e=strchr(ptr_s, '\n');
     
    497464      if (ptr_c == ptr_e) {
    498465        /* We made it to the newline. Append up to, and including it. */
    499         _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff + 1);
     466        _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str + 1);
    500467      }
    501468      else if (chwidth > 1) {
    502469        /* Last char is wide, truncate. */
    503         _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff);
     470        _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str);
    504471        owl_fmtext_append_normal(out, "\n");
    505472      }
     
    508475         * sure we get it all. */
    509476        ptr_c = g_utf8_next_char(ptr_c);
    510         _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff);
     477        _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str);
    511478      }
    512479    }
     
    535502
    536503  /* _owl_fmtext_truncate_cols_internal cannot handle tabs. */
    537   if (strchr(in->textbuff, '\t')) {
     504  if (strchr(in->buff->str, '\t')) {
    538505    owl_fmtext_init_null(&notabs);
    539506    owl_fmtext_expand_tabs(in, &notabs, 0);
     
    552519
    553520  lines=0;
    554   lastbreak = f->textbuff;
    555   for (i=0; i<f->textlen; i++) {
    556     if (f->textbuff[i]=='\n') {
    557       lastbreak = f->textbuff + i;
     521  lastbreak = f->buff->str;
     522  for (i = 0; i < f->buff->len; i++) {
     523    if (f->buff->str[i]=='\n') {
     524      lastbreak = f->buff->str + i;
    558525      lines++;
    559526    }
     
    562529  /* Check if there's a trailing line; formatting characters don't count. */
    563530  for (p = g_utf8_next_char(lastbreak);
    564        p < f->textbuff + f->textlen;
     531       p < f->buff->str + f->buff->len;
    565532       p = g_utf8_next_char(p)) {
    566533    if (!owl_fmtext_is_format_char(g_utf8_get_char(p))) {
     
    581548{
    582549  int i, lineno = 0;
    583   if (offset >= f->textlen)
    584     offset = f->textlen - 1;
     550  if (offset >= f->buff->len)
     551    offset = f->buff->len - 1;
    585552  for (i = 0; i < offset; i++) {
    586     if (f->textbuff[i] == '\n')
     553    if (f->buff->str[i] == '\n')
    587554      lineno++;
    588555  }
     
    596563  int start, end;
    597564  char *newline;
    598   for (start = 0; lineno > 0 && start < f->textlen; start++) {
    599     if (f->textbuff[start] == '\n')
     565  for (start = 0; lineno > 0 && start < f->buff->len; start++) {
     566    if (f->buff->str[start] == '\n')
    600567      lineno--;
    601568  }
    602   newline = strchr(f->textbuff + start, '\n');
     569  newline = strchr(f->buff->str + start, '\n');
    603570  /* Include the newline, if it is there. */
    604   end = newline ? newline - f->textbuff + 1 : f->textlen;
     571  end = newline ? newline - f->buff->str + 1 : f->buff->len;
    605572  if (o_start) *o_start = start;
    606573  if (o_end) *o_end = end;
     
    609576const char *owl_fmtext_get_text(const owl_fmtext *f)
    610577{
    611   return(f->textbuff);
     578  return f->buff->str;
    612579}
    613580
    614581int owl_fmtext_num_bytes(const owl_fmtext *f)
    615582{
    616   return(f->textlen);
     583  return f->buff->len;
    617584}
    618585
     
    622589void owl_fmtext_set_char(owl_fmtext *f, int index, char ch)
    623590{
    624   if ((index < 0) || (index > f->textlen-1)) return;
     591  if ((index < 0) || (index > f->buff->len - 1)) return;
    625592  /* NOT ASCII*/
    626   if (f->textbuff[index] & 0x80 || ch & 0x80) return;
    627   f->textbuff[index]=ch;
     593  if (f->buff->str[index] & 0x80 || ch & 0x80) return;
     594  f->buff->str[index] = ch;
    628595}
    629596
     
    631598void owl_fmtext_copy(owl_fmtext *dst, const owl_fmtext *src)
    632599{
    633   int mallocsize;
    634 
    635   if (src->textlen==0) {
    636     mallocsize=5;
    637   } else {
    638     mallocsize=src->textlen+2;
    639   }
    640   dst->textlen=src->textlen;
    641   dst->bufflen=mallocsize;
    642   dst->textbuff=owl_malloc(mallocsize);
    643   memcpy(dst->textbuff, src->textbuff, src->textlen+1);
     600  dst->buff = g_string_new(src->buff->str);
    644601  dst->default_attrs = src->default_attrs;
    645602  dst->default_fgcolor = src->default_fgcolor;
     
    654611{
    655612  int offset;
    656   if (start > f->textlen ||
    657       owl_regex_compare(re, f->textbuff + start, &offset, NULL) != 0)
     613  if (start > f->buff->len ||
     614      owl_regex_compare(re, f->buff->str + start, &offset, NULL) != 0)
    658615    return -1;
    659616  return offset + start;
     
    897854void owl_fmtext_cleanup(owl_fmtext *f)
    898855{
    899   if (f->textbuff) owl_free(f->textbuff);
     856  if (f->buff) g_string_free(f->buff, true);
     857  f->buff = NULL;
    900858}
    901859
  • owl.h

    rb03c714 r7e111f4  
    279279
    280280typedef struct _owl_fmtext {
    281   int textlen;
    282   int bufflen;
    283   char *textbuff;
     281  GString *buff;
    284282  char default_attrs;
    285283  short default_fgcolor;
Note: See TracChangeset for help on using the changeset viewer.