Skip to content

Commit cb57718

Browse files
Use no_init; remove function pointers
1 parent 5c0ef70 commit cb57718

File tree

1 file changed

+46
-32
lines changed
  • inst/include/Rcpp/sugar/functions/strings

1 file changed

+46
-32
lines changed

inst/include/Rcpp/sugar/functions/strings/trimws.h

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,60 +95,74 @@ inline const char* trim_both(const char* str) {
9595

9696

9797
inline Vector<STRSXP> trimws(const Vector<STRSXP>& x, const char* which = "both") {
98-
typedef const char* (*trim_function)(const char*);
99-
trim_function trim = NULL;
98+
R_xlen_t i = 0, sz = x.size();
99+
Vector<STRSXP> res = no_init(sz);
100100

101101
if (*which == 'b') {
102-
trim = sugar::detail::trim_both;
102+
for (; i < sz; i++) {
103+
if (traits::is_na<STRSXP>(x[i])) {
104+
res[i] = x[i];
105+
} else {
106+
res[i] = sugar::detail::trim_both(x[i]);
107+
}
108+
}
103109
} else if (*which == 'l') {
104-
trim = sugar::detail::trim_left;
110+
for (; i < sz; i++) {
111+
if (traits::is_na<STRSXP>(x[i])) {
112+
res[i] = x[i];
113+
} else {
114+
res[i] = sugar::detail::trim_left(x[i]);
115+
}
116+
}
105117
} else if (*which == 'r') {
106-
trim = sugar::detail::trim_right;
118+
for (; i < sz; i++) {
119+
if (traits::is_na<STRSXP>(x[i])) {
120+
res[i] = x[i];
121+
} else {
122+
res[i] = sugar::detail::trim_right(x[i]);
123+
}
124+
}
107125
} else {
108126
stop("Invalid `which` argument '%s'!", which);
109127
return Vector<STRSXP>::create("Unreachable");
110128
}
111129

112-
R_xlen_t i = 0, sz = x.size();
113-
Vector<STRSXP> res(sz);
114-
115-
for (; i < sz; i++) {
116-
if (traits::is_na<STRSXP>(x[i])) {
117-
res[i] = x[i];
118-
} else {
119-
res[i] = (*trim)(x[i]);
120-
}
121-
}
122-
123130
return res;
124131
}
125132

126133
inline Matrix<STRSXP> trimws(const Matrix<STRSXP>& x, const char* which = "both") {
127-
typedef const char* (*trim_function)(const char*);
128-
trim_function trim = NULL;
134+
R_xlen_t i = 0, nr = x.nrow(), nc = x.ncol(), sz = x.size();
135+
Matrix<STRSXP> res = no_init(nr, nc);
129136

130137
if (*which == 'b') {
131-
trim = sugar::detail::trim_both;
138+
for (; i < sz; i++) {
139+
if (traits::is_na<STRSXP>(x[i])) {
140+
res[i] = x[i];
141+
} else {
142+
res[i] = sugar::detail::trim_both(x[i]);
143+
}
144+
}
132145
} else if (*which == 'l') {
133-
trim = sugar::detail::trim_left;
146+
for (; i < sz; i++) {
147+
if (traits::is_na<STRSXP>(x[i])) {
148+
res[i] = x[i];
149+
} else {
150+
res[i] = sugar::detail::trim_left(x[i]);
151+
}
152+
}
134153
} else if (*which == 'r') {
135-
trim = sugar::detail::trim_right;
154+
for (; i < sz; i++) {
155+
if (traits::is_na<STRSXP>(x[i])) {
156+
res[i] = x[i];
157+
} else {
158+
res[i] = sugar::detail::trim_right(x[i]);
159+
}
160+
}
136161
} else {
137162
stop("Invalid `which` argument '%s'!", which);
138163
return Matrix<STRSXP>();
139164
}
140165

141-
R_xlen_t i = 0, sz = x.size();
142-
Matrix<STRSXP> res(x.nrow(), x.ncol());
143-
144-
for (; i < sz; i++) {
145-
if (traits::is_na<STRSXP>(x[i])) {
146-
res[i] = x[i];
147-
} else {
148-
res[i] = (*trim)(x[i]);
149-
}
150-
}
151-
152166
return res;
153167
}
154168

0 commit comments

Comments
 (0)