[2875] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

Re: More glib than your BarnOwl has room for

daemon@ATHENA.MIT.EDU (Nelson Elhage)
Sat Jan 29 13:59:07 2011

Date: Sat, 29 Jan 2011 13:59:02 -0500
From: Nelson Elhage <nelhage@ksplice.com>
To: David A Benjamin <davidben@mit.edu>
Cc: barnowl-dev@mit.edu
In-Reply-To: <alpine.DEB.1.10.1101250028310.3613@dr-wily.mit.edu>

lgtm, and I like these cleanups, modulo a few comments on the fmtext
commit.

- Nelson

On Tue, Jan 25, 2011 at 12:32:32AM -0500, David A Benjamin wrote:
> I should probably write a bunch more unit tests to cover all this
> code, but anyway. Dealing with reallocs and string lengths is hard,
> and all those +5s in the code made me sad. So let's go use all the
> stuff glib gives us.
> 
> The following changes since commit e06f413cb21f9b1aa3a993e0028180c7ec614b24:
> 
>   Stick the local realm onto the argument of viewuser if needed (2011-01-24 22:57:20 -0500)
> 
> are available in the git repository at:
>   git://github.com/davidben/barnowl.git more-glib
> 
> David Benjamin (12):
>       Use g_strjoinv in owl_global_set_startupargs
>       Use a GPtrArray to manage the growing list in owl_parseline
>       strndup is pretty awesome and doesn't involve adding 5
>       Be more accurate in allocating argument vector in owl_command_view
>       Don't stick random NULs when waddnstr exists
>       Replace owl_fmtext's buffer management with GString

/* Clear the data from an fmtext, but don't deallocate memory. This
    fmtext can then be appended to again. */
 void owl_fmtext_clear(owl_fmtext *f)
 {
-  f->textlen = 0;
-  f->textbuff[0] = 0;
+  g_string_free(f->buff, true);
+  f->buff = g_string_new("");
   f->default_attrs = OWL_FMTEXT_ATTR_NONE;
   f->default_fgcolor = OWL_COLOR_DEFAULT;
   f->default_bgcolor = OWL_COLOR_DEFAULT;
 }

To be pedantic, g_string_truncate() is semantically the same as what
we had before, and matches the comment. I'm not sure whether "clear
and then add a bunch more stuff" is an operation we do a lot; If so,
leaving the memory allocated probably makes sense; If not, let's also
adjust the comment.

   if (a)
-    strncat(f->textbuff, attrbuff,
-	    g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff));
+    g_string_append_len(f->buff, attrbuff,
+			g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff));

These can all become g_string_append_unichar, right? Which is
infinitely clearer.

>       No need to NULL-terminate argument vectors
>       Replace realloc logic in owl_zephyr_getsubs with GString
>       Use GString instead of allocating a giant buffer in owl_parseline
>       Use g_strjoinv in parsing zwrite lines
>       Use GString in owl_cmddict_execute_argv
>       Make owl_text_indent return a new string
> 
>  cmd.c       |   30 ++++------
>  commands.c  |    3 +-
>  fmtext.c    |  180 +++++++++++++++++++++++------------------------------------
>  functions.c |    3 -
>  global.c    |   17 +-----
>  owl.c       |   11 ++--
>  owl.h       |    4 +-
>  style.c     |    3 +-
>  tester.c    |    2 +
>  text.c      |   14 +++--
>  util.c      |   45 ++++++---------
>  zephyr.c    |   52 +++++------------
>  zwrite.c    |   17 +-----
>  13 files changed, 137 insertions(+), 244 deletions(-)

home help back first fref pref prev next nref lref last post