@@ -49,32 +49,32 @@ inline const char* trim_left(const char* str) {
4949 return str;
5050}
5151
52- inline const char * trim_right (const char * str) {
53- static std::string buff;
52+ inline const char * trim_right (const char * str, std::string* buff ) {
53+ // static std::string buff;
5454
5555 if (!str) {
5656 return " " ;
5757 }
5858
59- buff. clear ();
59+ buff-> clear ();
6060 std::size_t sz = std::strlen (str);
6161
6262 const char * ptr = str + sz - 1 ;
6363
6464 for (; ptr > str && isws (*ptr); --sz, --ptr);
6565
66- buff. append (str, sz - isws (*ptr));
67- return buff. c_str ();
66+ buff-> append (str, sz - isws (*ptr));
67+ return buff-> c_str ();
6868}
6969
70- inline const char * trim_both (const char * str) {
71- static std::string buff;
70+ inline const char * trim_both (const char * str, std::string* buff ) {
71+ // static std::string buff;
7272
7373 if (!str) {
7474 return " " ;
7575 }
7676
77- buff. clear ();
77+ buff-> clear ();
7878
7979 while (isws (*str)) {
8080 ++str;
@@ -85,8 +85,8 @@ inline const char* trim_both(const char* str) {
8585
8686 for (; ptr > str && isws (*ptr); --sz, --ptr);
8787
88- buff. append (str, sz);
89- return buff. c_str ();
88+ buff-> append (str, sz);
89+ return buff-> c_str ();
9090}
9191
9292
@@ -97,13 +97,14 @@ inline const char* trim_both(const char* str) {
9797inline Vector<STRSXP> trimws (const Vector<STRSXP>& x, const char * which = " both" ) {
9898 R_xlen_t i = 0 , sz = x.size ();
9999 Vector<STRSXP> res = no_init (sz);
100+ std::string buffer;
100101
101102 if (*which == ' b' ) {
102103 for (; i < sz; i++) {
103104 if (traits::is_na<STRSXP>(x[i])) {
104105 res[i] = x[i];
105106 } else {
106- res[i] = sugar::detail::trim_both (x[i]);
107+ res[i] = sugar::detail::trim_both (x[i], &buffer );
107108 }
108109 }
109110 } else if (*which == ' l' ) {
@@ -119,7 +120,7 @@ inline Vector<STRSXP> trimws(const Vector<STRSXP>& x, const char* which = "both"
119120 if (traits::is_na<STRSXP>(x[i])) {
120121 res[i] = x[i];
121122 } else {
122- res[i] = sugar::detail::trim_right (x[i]);
123+ res[i] = sugar::detail::trim_right (x[i], &buffer );
123124 }
124125 }
125126 } else {
@@ -133,13 +134,14 @@ inline Vector<STRSXP> trimws(const Vector<STRSXP>& x, const char* which = "both"
133134inline Matrix<STRSXP> trimws (const Matrix<STRSXP>& x, const char * which = " both" ) {
134135 R_xlen_t i = 0 , nr = x.nrow (), nc = x.ncol (), sz = x.size ();
135136 Matrix<STRSXP> res = no_init (nr, nc);
137+ std::string buffer;
136138
137139 if (*which == ' b' ) {
138140 for (; i < sz; i++) {
139141 if (traits::is_na<STRSXP>(x[i])) {
140142 res[i] = x[i];
141143 } else {
142- res[i] = sugar::detail::trim_both (x[i]);
144+ res[i] = sugar::detail::trim_both (x[i], &buffer );
143145 }
144146 }
145147 } else if (*which == ' l' ) {
@@ -155,7 +157,7 @@ inline Matrix<STRSXP> trimws(const Matrix<STRSXP>& x, const char* which = "both"
155157 if (traits::is_na<STRSXP>(x[i])) {
156158 res[i] = x[i];
157159 } else {
158- res[i] = sugar::detail::trim_right (x[i]);
160+ res[i] = sugar::detail::trim_right (x[i], &buffer );
159161 }
160162 }
161163 } else {
@@ -167,11 +169,13 @@ inline Matrix<STRSXP> trimws(const Matrix<STRSXP>& x, const char* which = "both"
167169}
168170
169171inline String trimws (const String& str, const char * which = " both" ) {
172+ std::string buffer;
173+
170174 if (*which == ' b' ) {
171175 if (traits::is_na<STRSXP>(str.get_sexp ())) {
172176 return String (str.get_sexp ());
173177 }
174- return sugar::detail::trim_both (str.get_cstring ());
178+ return sugar::detail::trim_both (str.get_cstring (), &buffer );
175179 }
176180
177181 if (*which == ' l' ) {
@@ -185,7 +189,7 @@ inline String trimws(const String& str, const char* which = "both") {
185189 if (traits::is_na<STRSXP>(str.get_sexp ())) {
186190 return String (str.get_sexp ());
187191 }
188- return sugar::detail::trim_right (str.get_cstring ());
192+ return sugar::detail::trim_right (str.get_cstring (), &buffer );
189193 }
190194
191195 stop (" Invalid `which` argument '%s'!" , which);
0 commit comments