commit ff1e231a420b11213e8afa284567f4371123c6fe
parent 6f993fdfac0fb7686cbf5ba164dcba67f8c2b61c
Auteur: Selve <selve@asteride.xyz>
Date: Sat, 20 Jan 2024 17:04:27 -0500
réorganisation des assertions
Diffstat:
M | tampon.c | | | 62 | +++++++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 35 insertions(+), 27 deletions(-)
diff --git a/tampon.c b/tampon.c
@@ -3,6 +3,17 @@
#include "tampon.h"
+#include <stdio.h>
+
+#define INVARIANTS_TP(t) \
+ (assert((t) != NULL), \
+ assert((t)->tp != NULL), \
+ assert((t)->pc != NULL), \
+ assert((t)->tll >= 1), \
+ assert((t)->pc - (t)->tp >= 0), \
+ assert((size_t) ((t)->pc - (t)->tp) <= (t)->tll), \
+ assert((t)->tll * TP_FACT_AGR > (t)->tll))
+
static int tp_plein(char, struct tampon *);
/* initialiser le tampon tp de taille tll */
@@ -12,9 +23,12 @@ tp_init(struct tampon *tp, size_t tll)
assert(tll >= 1);
assert(tp != NULL);
- if ((tp->tp = tp->pc = malloc(tll)) == NULL)
+ if ((tp->pc = tp->tp = malloc(tll)) == NULL)
return -1;
tp->tll = tll;
+
+ INVARIANTS_TP(tp);
+
return 0;
}
@@ -22,11 +36,7 @@ tp_init(struct tampon *tp, size_t tll)
int
tp_ecr(char c, struct tampon *tp)
{
- assert(tp != NULL);
- assert(tp->tp != NULL);
- assert(tp->pc != NULL);
- assert(tp->pc - tp->tp >= 0);
- assert((size_t) (tp->pc - tp->tp) <= tp->tll);
+ INVARIANTS_TP(tp);
if ((size_t) (tp->pc - tp->tp) == tp->tll) /* le tampon est-il plein? */
return tp_plein(c, tp);
@@ -39,58 +49,56 @@ tp_plein(char c, struct tampon *tp)
{
size_t diff;
- assert(tp != NULL);
- assert(tp->tp != NULL);
- assert(tp->pc != NULL);
- assert(tp->pc - tp->tp >= 0);
- assert(tp->tll >= 1);
- assert(tp->tll * TP_FACT_AGR > tp->tll); /* doit grandir */
+ INVARIANTS_TP(tp);
diff = tp->pc - tp->tp;
if ((tp->tp = realloc(tp->tp, tp->tll * TP_FACT_AGR)) == NULL)
return -1;
- tp->tll *= TP_FACT_AGR;
- tp->pc = tp->tp + diff; /* tp->tp pourrait avoir changé */
- return (unsigned char) (*tp->pc++ = c);
+ tp->tll *= TP_FACT_AGR;
+ tp->pc = tp->tp + diff; /* tp->tp pourrait avoir changé */
+ *tp->pc++ = c;
+
+ INVARIANTS_TP(tp);
+
+ return (unsigned char) c;
}
/* effacer le contenu du tampon tp */
void
tp_eff(struct tampon *tp)
{
- assert(tp != NULL);
- assert(tp->tp != NULL);
- assert(tp->pc != NULL);
+ INVARIANTS_TP(tp);
tp->pc = tp->tp;
+
+ INVARIANTS_TP(tp);
}
+/* effacer les caractères du tampon tp jusqu'à la dernière nouvelle ligne,
+ * inclusivement */
void
tp_bloc_rec(struct tampon *tp)
{
- assert(tp != NULL);
- assert(tp->tp != NULL);
- assert(tp->pc != NULL);
+ INVARIANTS_TP(tp);
while (tp->pc > tp->tp) {
if (*--tp->pc == 'n' && *--tp->pc == '\\')
break;
}
+
+ INVARIANTS_TP(tp);
}
/* supprimer le domaine cadet */
int
tp_etq_rec(struct tampon *tp)
{
- assert(tp != NULL);
- assert(tp->tp != NULL);
- assert(tp->pc != NULL);
+ INVARIANTS_TP(tp);
assert(tp->pc - tp->tp > 1);
assert(*tp->tp == ':');
- do {
- --tp->pc;
- } while (*tp->pc != ':');
+ while (*--tp->pc != ':')
+ ;
if (tp->pc - tp->tp != 0 && *(tp->pc-1) == '\\') {
--tp->pc;
assert(tp->pc - tp->tp > 0);