From 9dd500e3bf1f5aee76d26703bfd9ec8713fcd81a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fredrik=20H=C3=BCbinette=20=28Hubbe=29?= <hubbe@hubbe.net>
Date: Fri, 6 Dec 1996 19:13:27 -0800
Subject: [PATCH] memfill() function implemented and used

Rev: src/memory.c:1.3
Rev: src/memory.h:1.2
Rev: src/modules/sprintf/sprintf.c:1.8
---
 src/memory.c                  | 37 +++++++++++++++++++++++++++++++++++
 src/memory.h                  |  6 +++++-
 src/modules/sprintf/sprintf.c | 27 +++++++++++++------------
 3 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/src/memory.c b/src/memory.c
index de77677601..84a31ae092 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -280,3 +280,40 @@ char *my_memmem(char *needle,
   init_memsearch(&tmp, needle, needlelen, haystacklen);
   return memory_search(&tmp, haystack, haystacklen);
 }
+
+void memfill(char *to,
+	     INT32 tolen,
+	     char *from,
+	     INT32 fromlen,
+	     INT32 offset)
+{
+  if(fromlen==1)
+  {
+    MEMSET(to, *from, tolen);
+  }
+  else if(tolen>0)
+  {
+    INT32 tmp=MINIMUM(tolen, fromlen - offset);
+    MEMCPY(to, from + offset, tmp);
+    to+=tmp;
+    tolen-=tmp;
+
+    if(tolen > 0)
+    {
+      tmp=MINIMUM(tolen, fromlen);
+      MEMCPY(to, from, tmp);
+      from=to;
+      to+=tmp;
+      tolen-=tmp;
+      
+      while(tolen>0)
+      {
+	tmp=MINIMUM(tolen, fromlen);
+	MEMCPY(to, from, MINIMUM(tolen, fromlen));
+	fromlen+=tmp;
+	tolen-=tmp;
+	to+=tmp;
+      }
+    }
+  }
+}
diff --git a/src/memory.h b/src/memory.h
index bc7a358f62..0e749d4ab7 100644
--- a/src/memory.h
+++ b/src/memory.h
@@ -35,7 +35,6 @@ struct mem_searcher
 
 /* Prototypes begin here */
 char *xalloc(SIZE_T size);
-void reorder(char *memory,INT32 nitems,INT32 size,INT32 *order);
 void reorder(char *memory, INT32 nitems, INT32 size,INT32 *order);
 unsigned INT32 hashmem(const unsigned char *a,INT32 len,INT32 mlen);
 unsigned INT32 hashstr(const unsigned char *str,INT32 maxn);
@@ -50,6 +49,11 @@ char *my_memmem(char *needle,
 		SIZE_T needlelen,
 		char *haystack,
 		SIZE_T haystacklen);
+void memfill(char *to,
+	     INT32 tolen,
+	     char *from,
+	     INT32 fromlen,
+	     INT32 offset);
 /* Prototypes end here */
 
 #endif
diff --git a/src/modules/sprintf/sprintf.c b/src/modules/sprintf/sprintf.c
index a7e7a51c94..688808018d 100644
--- a/src/modules/sprintf/sprintf.c
+++ b/src/modules/sprintf/sprintf.c
@@ -96,7 +96,7 @@
 */
 
 #include "global.h"
-RCSID("$Id: sprintf.c,v 1.7 1996/11/27 06:01:32 hubbe Exp $");
+RCSID("$Id: sprintf.c,v 1.8 1996/12/07 03:13:27 hubbe Exp $");
 #include "error.h"
 #include "array.h"
 #include "svalue.h"
@@ -198,11 +198,11 @@ INLINE static void fix_field(char *b,
   {
     e=len;
     if(pos_pad && b[0]!='-') e++;
-    for(e=(width-e)/2;e-->0;d++)
+    e=(width-e)/2;
+    if(e>0)
     {
-      if(d==pad_length) d=0;
-      my_putchar(pad_string[d]);
-      width--;
+      memfill(make_buf_space(e), e, pad_string, pad_length, 0);
+      width-=e;
     }
     flags|=FIELD_LEFT;
   }
@@ -217,23 +217,26 @@ INLINE static void fix_field(char *b,
       len--;
       width--;
     }
-    for(d%=pad_length;width;d++,width--)
+
+    if(width>0)
     {
-      if(d>=pad_length) d=0;
-      my_putchar(pad_string[d]);
+      d%=pad_length;
+      memfill(make_buf_space(width), width, pad_string, pad_length, d);
     }
+    
     return;
   }
 
   /* Right-justification */
 
   if(pos_pad && b[0]!='-') len++;
-  for(;width>len;d++)
+  e=width-len;
+  if(e>0)
   {
-    if(d>=pad_length) d=0;
-    my_putchar(pad_string[d]);
-    width--;
+    memfill(make_buf_space(e), e, pad_string, pad_length, 0);
+    width-=e;
   }
+
   if(pos_pad && b[0]!='-' && len==width)
   {
     my_putchar(pos_pad);
-- 
GitLab