Gentoo Archives: gentoo-commits

From: Mike Frysinger <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/, tests/rmspace/, tests/
Date: Sat, 28 Nov 2015 02:44:42
Message-Id: 1448646374.af57eb3627af70441e3d135a10d68987698aa506.vapier@gentoo
1 commit: af57eb3627af70441e3d135a10d68987698aa506
2 Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
3 AuthorDate: Fri Nov 27 17:46:14 2015 +0000
4 Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
5 CommitDate: Fri Nov 27 17:46:14 2015 +0000
6 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=af57eb36
7
8 rmspace: rewrite/optimize a bit
9
10 This makes the code a bit easier to read and avoids calling strlen
11 all the time as we can calculate it ourselves with basic math.
12
13 libq/rmspace.c | 15 +++++++++------
14 tests/Makefile | 2 +-
15 tests/rmspace/.gitignore | 1 +
16 tests/rmspace/Makefile | 18 ++++++++++++++++++
17 tests/rmspace/dotest | 22 ++++++++++++++++++++++
18 tests/rmspace/test.c | 32 ++++++++++++++++++++++++++++++++
19 6 files changed, 83 insertions(+), 7 deletions(-)
20
21 diff --git a/libq/rmspace.c b/libq/rmspace.c
22 index 1d5e45d..d374d7f 100644
23 --- a/libq/rmspace.c
24 +++ b/libq/rmspace.c
25 @@ -2,15 +2,18 @@
26 /* removed leading/trailing extraneous white space */
27 static char *rmspace(char *s)
28 {
29 - register char *p;
30 + char *p;
31 + size_t len = strlen(s);
32 /* find the start of trailing space and set it to \0 */
33 - for (p = s + strlen(s) - 1; (p >= s && isspace(*p)); --p);
34 - if (p != s + strlen(s) - 1)
35 - *(p + 1) = 0;
36 + for (p = s + len - 1; (p >= s && isspace(*p)); --p)
37 + continue;
38 + p[1] = '\0';
39 + len = (p - s) + 1;
40 /* find the end of leading space and set p to it */
41 - for (p = s; (isspace(*p) && *p); ++p);
42 + for (p = s; (isspace(*p) && *p); ++p)
43 + continue;
44 /* move the memory backward to overwrite leading space */
45 if (p != s)
46 - memmove(s, p, strlen(p)+1);
47 + memmove(s, p, len - (p - s) + 1);
48 return s;
49 }
50
51 diff --git a/tests/Makefile b/tests/Makefile
52 index 2fdea84..bbe0fe8 100644
53 --- a/tests/Makefile
54 +++ b/tests/Makefile
55 @@ -1,5 +1,5 @@
56 TESTS = \
57 - reinitialize atom_compare atom_explode mkdir \
58 + reinitialize atom_compare atom_explode mkdir rmspace \
59 qatom qcheck qdepends qfile qlist qlop qmerge qtbz2 quse qxpak \
60 install profile source
61
62
63 diff --git a/tests/rmspace/.gitignore b/tests/rmspace/.gitignore
64 new file mode 100644
65 index 0000000..28ce6a8
66 --- /dev/null
67 +++ b/tests/rmspace/.gitignore
68 @@ -0,0 +1 @@
69 +m
70
71 diff --git a/tests/rmspace/Makefile b/tests/rmspace/Makefile
72 new file mode 100644
73 index 0000000..ad14de3
74 --- /dev/null
75 +++ b/tests/rmspace/Makefile
76 @@ -0,0 +1,18 @@
77 +thisdir = mkdir
78 +include ../subdir.mk
79 +
80 +all: $(b)/m
81 +
82 +$(b)/m: $(s)/test.c
83 + mkdir -p $(b)
84 + $(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@
85 +
86 +test check: dotest
87 +
88 +dotest: $(b)/m
89 + $(Q)$(s)/dotest
90 +
91 +clean:
92 + rm -f $(b)/m
93 +
94 +.PHONY: all basic dotest test check clean
95
96 diff --git a/tests/rmspace/dotest b/tests/rmspace/dotest
97 new file mode 100755
98 index 0000000..1a3e71c
99 --- /dev/null
100 +++ b/tests/rmspace/dotest
101 @@ -0,0 +1,22 @@
102 +#!/bin/bash
103 +
104 +. ../init.sh || exit 1
105 +
106 +set -e
107 +
108 +m=${ab}/m
109 +
110 +tests=(
111 + ""
112 + "a"
113 + " a"
114 + " a"
115 + " a"
116 + "a "
117 + "a "
118 + " a "
119 +)
120 +${m} "${tests[@]}"
121 +: $(( tpassed += ${#tests[@]} ))
122 +
123 +end
124
125 diff --git a/tests/rmspace/test.c b/tests/rmspace/test.c
126 new file mode 100644
127 index 0000000..8143f46
128 --- /dev/null
129 +++ b/tests/rmspace/test.c
130 @@ -0,0 +1,32 @@
131 +/*
132 + * Copyright 2005-2014 Gentoo Foundation
133 + * Distributed under the terms of the GNU General Public License v2
134 + *
135 + * Copyright 2005-2008 Ned Ludd - <solar@g.o>
136 + * Copyright 2005-2014 Mike Frysinger - <vapier@g.o>
137 + */
138 +
139 +#include "tests/tests.h"
140 +
141 +#include "libq/rmspace.c"
142 +
143 +int main(int argc, char *argv[])
144 +{
145 + int i;
146 + char *s;
147 + size_t len;
148 +
149 + if (argc <= 1)
150 + return 1;
151 +
152 + for (i = 1; i < argc; ++i) {
153 + s = rmspace(argv[i]);
154 + len = strlen(s);
155 + if (isspace(s[0]) || isspace(s[len - 1])) {
156 + fprintf(stderr, "FAIL {%s}\n", s);
157 + return 1;
158 + }
159 + }
160 +
161 + return 0;
162 +}