groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 12/23: [libgroff]: Slightly refactor.


From: G. Branden Robinson
Subject: [groff] 12/23: [libgroff]: Slightly refactor.
Date: Thu, 23 Sep 2021 08:12:34 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit d32725974cf8e5b09d31c0d1f0a36bdb8aec164f
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Thu Sep 23 11:58:30 2021 +1000

    [libgroff]: Slightly refactor.
    
    * src/libs/libgroff/font.cpp (struct text_file): Rename member variable
      `size` to `linebufsize` for clarity (it's certainly not the size of
      the text file).
    
      (text_file::text_file): Initialize `linebufsize` in constructor.
    
      (text_file::next_line): Allocate new `buf` using `linebufsize` instead
      of magic number (which was also stored to `size`, making it only
      semi-magic, I guess).  Rename local variable `i` to `length` since it
      is in fact the length (in `char`s) of the string stored in `buf`.
---
 ChangeLog                  | 14 ++++++++++++++
 src/libs/libgroff/font.cpp | 27 ++++++++++++---------------
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 780a137..31c15a7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2021-09-23  G. Branden Robinson <g.branden.robinson@gmail.com>
 
+       [libgroff]: Slightly refactor.
+
+       * src/libs/libgroff/font.cpp (struct text_file): Rename member
+       variable `size` to `linebufsize` for clarity (it's certainly not
+       the size of the text file).
+       (text_file::text_file): Initialize `linebufsize` in constructor.
+       (text_file::next_line): Allocate new `buf` using `linebufsize`
+       instead of magic number (which was also stored to `size`, making
+       it only semi-magic, I guess).  Rename local variable `i` to
+       `length` since it is in fact the length (in `char`s) of the
+       string stored in `buf`.
+
+2021-09-23  G. Branden Robinson <g.branden.robinson@gmail.com>
+
        [libgroff]: Revisit fix for Savannah #61173.
 
        * src/libs/libgroff/font.cpp (text_file::text_file): Restore
diff --git a/src/libs/libgroff/font.cpp b/src/libs/libgroff/font.cpp
index 283bca1..a194457 100644
--- a/src/libs/libgroff/font.cpp
+++ b/src/libs/libgroff/font.cpp
@@ -68,7 +68,7 @@ struct text_file {
   FILE *fp;
   char *path;
   int lineno;
-  int size;
+  int linebufsize;
   bool recognize_comments;
   bool silent;
   char *buf;
@@ -82,7 +82,7 @@ struct text_file {
 };
 
 text_file::text_file(FILE *p, char *s) : fp(p), path(s), lineno(0),
-  size(0), recognize_comments(true), silent(false), buf(0)
+  linebufsize(128), recognize_comments(true), silent(false), buf(0)
 {
 }
 
@@ -98,13 +98,11 @@ bool text_file::next_line()
 {
   if (fp == 0)
     return false;
-  if (buf == 0) {
-    buf = new char[128];
-    size = 128;
-  }
+  if (buf == 0)
+    buf = new char[linebufsize];
   for (;;) {
     lineno++;
-    int i = 0;
+    int length = 0;
     for (;;) {
       int c = getc(fp);
       if (c == EOF)
@@ -112,22 +110,21 @@ bool text_file::next_line()
       if (invalid_input_char(c))
        error("invalid input character code %1", int(c));
       else {
-       if (i + 1 >= size) {
+       if (length + 1 >= linebufsize) {
          char *old_buf = buf;
-         buf = new char[size*2];
-         memcpy(buf, old_buf, size);
+         buf = new char[linebufsize * 2];
+         memcpy(buf, old_buf, linebufsize);
          delete[] old_buf;
-         size *= 2;
+         linebufsize *= 2;
        }
-       buf[i++] = c;
+       buf[length++] = c;
        if (c == '\n')
          break;
       }
     }
-    if (i == 0)
+    if (length == 0)
       break;
-    buf[i] = '\0';
-    lineno++;
+    buf[length] = '\0';
     char *ptr = buf;
     while (csspace(*ptr))
       ptr++;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]