Skip to content

Commit 69e395a

Browse files
Replace strlen with LENGTH
1 parent 6819eb5 commit 69e395a

File tree

1 file changed

+33
-17
lines changed
  • inst/include/Rcpp/sugar/functions/strings

1 file changed

+33
-17
lines changed

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,12 @@ inline const char* trim_left(const char* str) {
4949
return str;
5050
}
5151

52-
inline const char* trim_right(const char* str, std::string* buff) {
53-
// static std::string buff;
54-
52+
inline const char* trim_right(const char* str, R_len_t sz, std::string* buff) {
5553
if (!str) {
5654
return "";
5755
}
5856

5957
buff->clear();
60-
std::size_t sz = std::strlen(str);
61-
6258
const char* ptr = str + sz - 1;
6359

6460
for (; ptr > str && isws(*ptr); --sz, --ptr);
@@ -67,20 +63,16 @@ inline const char* trim_right(const char* str, std::string* buff) {
6763
return buff->c_str();
6864
}
6965

70-
inline const char* trim_both(const char* str, std::string* buff) {
71-
// static std::string buff;
72-
66+
inline const char* trim_both(const char* str, R_len_t sz, std::string* buff) {
7367
if (!str) {
7468
return "";
7569
}
7670

7771
buff->clear();
7872

7973
while (isws(*str)) {
80-
++str;
74+
++str; --sz;
8175
}
82-
83-
std::size_t sz = std::strlen(str);
8476
const char* ptr = str + sz - 1;
8577

8678
for (; ptr > str && isws(*ptr); --sz, --ptr);
@@ -104,7 +96,11 @@ inline Vector<STRSXP> trimws(const Vector<STRSXP>& x, const char* which = "both"
10496
if (traits::is_na<STRSXP>(x[i])) {
10597
res[i] = x[i];
10698
} else {
107-
res[i] = sugar::detail::trim_both(x[i], &buffer);
99+
res[i] = sugar::detail::trim_both(
100+
x[i],
101+
LENGTH(x[i]),
102+
&buffer
103+
);
108104
}
109105
}
110106
} else if (*which == 'l') {
@@ -120,7 +116,11 @@ inline Vector<STRSXP> trimws(const Vector<STRSXP>& x, const char* which = "both"
120116
if (traits::is_na<STRSXP>(x[i])) {
121117
res[i] = x[i];
122118
} else {
123-
res[i] = sugar::detail::trim_right(x[i], &buffer);
119+
res[i] = sugar::detail::trim_right(
120+
x[i],
121+
LENGTH(x[i]),
122+
&buffer
123+
);
124124
}
125125
}
126126
} else {
@@ -141,7 +141,11 @@ inline Matrix<STRSXP> trimws(const Matrix<STRSXP>& x, const char* which = "both"
141141
if (traits::is_na<STRSXP>(x[i])) {
142142
res[i] = x[i];
143143
} else {
144-
res[i] = sugar::detail::trim_both(x[i], &buffer);
144+
res[i] = sugar::detail::trim_both(
145+
x[i],
146+
LENGTH(x[i]),
147+
&buffer
148+
);
145149
}
146150
}
147151
} else if (*which == 'l') {
@@ -157,7 +161,11 @@ inline Matrix<STRSXP> trimws(const Matrix<STRSXP>& x, const char* which = "both"
157161
if (traits::is_na<STRSXP>(x[i])) {
158162
res[i] = x[i];
159163
} else {
160-
res[i] = sugar::detail::trim_right(x[i], &buffer);
164+
res[i] = sugar::detail::trim_right(
165+
x[i],
166+
LENGTH(x[i]),
167+
&buffer
168+
);
161169
}
162170
}
163171
} else {
@@ -175,7 +183,11 @@ inline String trimws(const String& str, const char* which = "both") {
175183
if (traits::is_na<STRSXP>(str.get_sexp())) {
176184
return String(str.get_sexp());
177185
}
178-
return sugar::detail::trim_both(str.get_cstring(), &buffer);
186+
return sugar::detail::trim_both(
187+
str.get_cstring(),
188+
LENGTH(str.get_sexp()),
189+
&buffer
190+
);
179191
}
180192

181193
if (*which == 'l') {
@@ -189,7 +201,11 @@ inline String trimws(const String& str, const char* which = "both") {
189201
if (traits::is_na<STRSXP>(str.get_sexp())) {
190202
return String(str.get_sexp());
191203
}
192-
return sugar::detail::trim_right(str.get_cstring(), &buffer);
204+
return sugar::detail::trim_right(
205+
str.get_cstring(),
206+
LENGTH(str.get_sexp()),
207+
&buffer
208+
);
193209
}
194210

195211
stop("Invalid `which` argument '%s'!", which);

0 commit comments

Comments
 (0)