commit:     af57eb3627af70441e3d135a10d68987698aa506
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Fri Nov 27 17:46:14 2015 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Fri Nov 27 17:46:14 2015 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=af57eb36

rmspace: rewrite/optimize a bit

This makes the code a bit easier to read and avoids calling strlen
all the time as we can calculate it ourselves with basic math.

 libq/rmspace.c           | 15 +++++++++------
 tests/Makefile           |  2 +-
 tests/rmspace/.gitignore |  1 +
 tests/rmspace/Makefile   | 18 ++++++++++++++++++
 tests/rmspace/dotest     | 22 ++++++++++++++++++++++
 tests/rmspace/test.c     | 32 ++++++++++++++++++++++++++++++++
 6 files changed, 83 insertions(+), 7 deletions(-)

diff --git a/libq/rmspace.c b/libq/rmspace.c
index 1d5e45d..d374d7f 100644
--- a/libq/rmspace.c
+++ b/libq/rmspace.c
@@ -2,15 +2,18 @@
 /* removed leading/trailing extraneous white space */
 static char *rmspace(char *s)
 {
-       register char *p;
+       char *p;
+       size_t len = strlen(s);
        /* find the start of trailing space and set it to \0 */
-       for (p = s + strlen(s) - 1; (p >= s && isspace(*p)); --p);
-       if (p != s + strlen(s) - 1)
-               *(p + 1) = 0;
+       for (p = s + len - 1; (p >= s && isspace(*p)); --p)
+               continue;
+       p[1] = '\0';
+       len = (p - s) + 1;
        /* find the end of leading space and set p to it */
-       for (p = s; (isspace(*p) && *p); ++p);
+       for (p = s; (isspace(*p) && *p); ++p)
+               continue;
        /* move the memory backward to overwrite leading space */
        if (p != s)
-               memmove(s, p, strlen(p)+1);
+               memmove(s, p, len - (p - s) + 1);
        return s;
 }

diff --git a/tests/Makefile b/tests/Makefile
index 2fdea84..bbe0fe8 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,5 +1,5 @@
 TESTS = \
-       reinitialize atom_compare atom_explode mkdir \
+       reinitialize atom_compare atom_explode mkdir rmspace \
        qatom qcheck qdepends qfile qlist qlop qmerge qtbz2 quse qxpak \
        install profile source
 

diff --git a/tests/rmspace/.gitignore b/tests/rmspace/.gitignore
new file mode 100644
index 0000000..28ce6a8
--- /dev/null
+++ b/tests/rmspace/.gitignore
@@ -0,0 +1 @@
+m

diff --git a/tests/rmspace/Makefile b/tests/rmspace/Makefile
new file mode 100644
index 0000000..ad14de3
--- /dev/null
+++ b/tests/rmspace/Makefile
@@ -0,0 +1,18 @@
+thisdir = mkdir
+include ../subdir.mk
+
+all: $(b)/m
+
+$(b)/m: $(s)/test.c
+       mkdir -p $(b)
+       $(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@
+
+test check: dotest
+
+dotest: $(b)/m
+       $(Q)$(s)/dotest
+
+clean:
+       rm -f $(b)/m
+
+.PHONY: all basic dotest test check clean

diff --git a/tests/rmspace/dotest b/tests/rmspace/dotest
new file mode 100755
index 0000000..1a3e71c
--- /dev/null
+++ b/tests/rmspace/dotest
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+. ../init.sh || exit 1
+
+set -e
+
+m=${ab}/m
+
+tests=(
+       ""
+       "a"
+       " a"
+       "       a"
+       "        a"
+       "a "
+       "a      "
+       "       a "
+)
+${m} "${tests[@]}"
+: $(( tpassed += ${#tests[@]} ))
+
+end

diff --git a/tests/rmspace/test.c b/tests/rmspace/test.c
new file mode 100644
index 0000000..8143f46
--- /dev/null
+++ b/tests/rmspace/test.c
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2005-2014 Gentoo Foundation
+ * Distributed under the terms of the GNU General Public License v2
+ *
+ * Copyright 2005-2008 Ned Ludd        - <[email protected]>
+ * Copyright 2005-2014 Mike Frysinger  - <[email protected]>
+ */
+
+#include "tests/tests.h"
+
+#include "libq/rmspace.c"
+
+int main(int argc, char *argv[])
+{
+       int i;
+       char *s;
+       size_t len;
+
+       if (argc <= 1)
+               return 1;
+
+       for (i = 1; i < argc; ++i) {
+               s = rmspace(argv[i]);
+               len = strlen(s);
+               if (isspace(s[0]) || isspace(s[len - 1])) {
+                       fprintf(stderr, "FAIL {%s}\n", s);
+                       return 1;
+               }
+       }
+
+       return 0;
+}

Reply via email to