Commit: aaf1b2e2ed763d19c6a6aa3d580994034fd45d39
Parent: 05cee0ef80317dbe162adc00956fbb35c941607f
Author: Randy Palamar
Date: Thu, 25 May 2023 14:28:41 -0600
util: trim(): return start of trimmed string
this removes the need for a memmove() call
Diffstat:
3 files changed, 10 insertions(+), 13 deletions(-)
diff --git a/jdict.c b/jdict.c
@@ -321,10 +321,9 @@ repl(struct Dict *dicts, size_t ndicts)
fflush(stdout);
if (fgets(buf, LEN(buf), stdin) == NULL)
break;
- trim(buf);
for (i = 0; i < ndicts; i++) {
puts(dicts[i].name);
- find_and_print(buf, ents[i], nents[i]);
+ find_and_print(trim(buf), ents[i], nents[i]);
}
}
puts(repl_quit);
diff --git a/util.c b/util.c
@@ -19,22 +19,20 @@ die(const char *fmt, ...)
exit(1);
}
-/* trim whitespace from start and end of str */
-void
+/*
+ * trim whitespace from start and end of str
+ * returns start of trimmed str
+ */
+char *
trim(char *s)
{
char *p = s;
- size_t len;
-
- if (s == NULL)
- return;
-
- len = strlen(s);
+ size_t len = strlen(s);
for (; isspace(p[len-1]); p[--len] = 0);
- for (; *p && isspace(*p); p++, len--);
+ for (; *p && isspace(*p); p++);
- memmove(s, p, len + 1);
+ return p;
}
/* replace embedded escaped newlines with actual newlines */
diff --git a/util.h b/util.h
@@ -3,5 +3,5 @@
void die(const char *, ...);
char *fix_newlines(char *);
-void trim(char *);
+char *trim(char *);
void *xreallocarray(void *, size_t, size_t);