|
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- |
| 2 | +// |
| 3 | +// trimws.h: Rcpp R/C++ interface class library -- trimws |
| 4 | +// |
| 5 | +// Copyright (C) 2017 Nathan Russell |
| 6 | +// |
| 7 | +// This file is part of Rcpp. |
| 8 | +// |
| 9 | +// Rcpp is free software: you can redistribute it and/or modify it |
| 10 | +// under the terms of the GNU General Public License as published by |
| 11 | +// the Free Software Foundation, either version 2 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// Rcpp is distributed in the hope that it will be useful, but |
| 15 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License |
| 20 | +// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +#ifndef Rcpp__sugar__trimws_h |
| 23 | +#define Rcpp__sugar__trimws_h |
| 24 | + |
| 25 | +#include <string> |
| 26 | +#include <cstring> |
| 27 | + |
| 28 | +namespace Rcpp { |
| 29 | +namespace sugar { |
| 30 | +namespace detail { |
| 31 | + |
| 32 | + |
| 33 | +/* NB: std::isspace is not used because it also counts |
| 34 | + '\f' and '\v' as whitespace, whereas base::trimws only |
| 35 | + checks for ' ', '\t', '\r', and '\n' */ |
| 36 | +inline bool isws(const char c) { |
| 37 | + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; |
| 38 | +} |
| 39 | + |
| 40 | +inline const char* trim_left(const char* str) { |
| 41 | + static std::string buff; |
| 42 | + |
| 43 | + if (!str) { |
| 44 | + return ""; |
| 45 | + } |
| 46 | + |
| 47 | + buff.clear(); |
| 48 | + char c = *str; |
| 49 | + |
| 50 | + while (isws(c)) { |
| 51 | + ++str; |
| 52 | + c = *str; |
| 53 | + } |
| 54 | + |
| 55 | + buff.append(str); |
| 56 | + return buff.c_str(); |
| 57 | +} |
| 58 | + |
| 59 | +inline const char* trim_right(const char* str) { |
| 60 | + static std::string buff; |
| 61 | + |
| 62 | + if (!str) { |
| 63 | + return ""; |
| 64 | + } |
| 65 | + |
| 66 | + buff.clear(); |
| 67 | + std::size_t sz = std::strlen(str); |
| 68 | + |
| 69 | + const char* ptr = str + sz - 1; |
| 70 | + char c = *ptr; |
| 71 | + |
| 72 | + for (; ptr > str && isws(c); c = *ptr) { |
| 73 | + --sz; --ptr; |
| 74 | + } |
| 75 | + |
| 76 | + buff.append(str, sz - isws(*ptr)); |
| 77 | + return buff.c_str(); |
| 78 | +} |
| 79 | + |
| 80 | +inline const char* trim_both(const char* str) { |
| 81 | + static std::string buff; |
| 82 | + |
| 83 | + if (!str) { |
| 84 | + return ""; |
| 85 | + } |
| 86 | + |
| 87 | + buff.clear(); |
| 88 | + char c = *str; |
| 89 | + |
| 90 | + while (isws(c)) { |
| 91 | + ++str; |
| 92 | + c = *str; |
| 93 | + } |
| 94 | + |
| 95 | + std::size_t sz = std::strlen(str); |
| 96 | + const char* ptr = str + sz - 1; |
| 97 | + c = *ptr; |
| 98 | + |
| 99 | + for (; ptr >= str; c = *ptr, --sz, --ptr) { |
| 100 | + if (!isws(c)) { |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + buff.append(str, sz + 1); |
| 106 | + return buff.c_str(); |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +} // detail |
| 111 | +} // sugar |
| 112 | + |
| 113 | + |
| 114 | +inline Vector<STRSXP> trimws(const Vector<STRSXP>& x, const char* which = "both") { |
| 115 | + typedef const char* (*trim_function)(const char*); |
| 116 | + trim_function trim = NULL; |
| 117 | + |
| 118 | + if (*which == 'b') { |
| 119 | + trim = sugar::detail::trim_both; |
| 120 | + } else if (*which == 'l') { |
| 121 | + trim = sugar::detail::trim_left; |
| 122 | + } else if (*which == 'r') { |
| 123 | + trim = sugar::detail::trim_right; |
| 124 | + } else { |
| 125 | + stop("Invalid `which` argument '%s'!", which); |
| 126 | + return Vector<STRSXP>::create("Unreachable"); |
| 127 | + } |
| 128 | + |
| 129 | + R_xlen_t i = 0, sz = x.size(); |
| 130 | + Vector<STRSXP> res(sz); |
| 131 | + |
| 132 | + for (; i < sz; i++) { |
| 133 | + if (traits::is_na<STRSXP>(x[i])) { |
| 134 | + res[i] = x[i]; |
| 135 | + } else { |
| 136 | + res[i] = (*trim)(x[i]); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return res; |
| 141 | +} |
| 142 | + |
| 143 | +inline Matrix<STRSXP> trimws(const Matrix<STRSXP>& x, const char* which = "both") { |
| 144 | + typedef const char* (*trim_function)(const char*); |
| 145 | + trim_function trim = NULL; |
| 146 | + |
| 147 | + if (*which == 'b') { |
| 148 | + trim = sugar::detail::trim_both; |
| 149 | + } else if (*which == 'l') { |
| 150 | + trim = sugar::detail::trim_left; |
| 151 | + } else if (*which == 'r') { |
| 152 | + trim = sugar::detail::trim_right; |
| 153 | + } else { |
| 154 | + stop("Invalid `which` argument '%s'!", which); |
| 155 | + return Matrix<STRSXP>(); |
| 156 | + } |
| 157 | + |
| 158 | + R_xlen_t i = 0, sz = x.size(); |
| 159 | + Matrix<STRSXP> res(x.nrow(), x.ncol()); |
| 160 | + |
| 161 | + for (; i < sz; i++) { |
| 162 | + if (traits::is_na<STRSXP>(x[i])) { |
| 163 | + res[i] = x[i]; |
| 164 | + } else { |
| 165 | + res[i] = (*trim)(x[i]); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return res; |
| 170 | +} |
| 171 | + |
| 172 | +inline String trimws(const String& str, const char* which = "both") { |
| 173 | + if (*which == 'b') { |
| 174 | + if (traits::is_na<STRSXP>(str.get_sexp())) { |
| 175 | + return String(str.get_sexp()); |
| 176 | + } |
| 177 | + return sugar::detail::trim_both(str.get_cstring()); |
| 178 | + } |
| 179 | + |
| 180 | + if (*which == 'l') { |
| 181 | + if (traits::is_na<STRSXP>(str.get_sexp())) { |
| 182 | + return String(str.get_sexp()); |
| 183 | + } |
| 184 | + return sugar::detail::trim_left(str.get_cstring()); |
| 185 | + } |
| 186 | + |
| 187 | + if (*which == 'r') { |
| 188 | + if (traits::is_na<STRSXP>(str.get_sexp())) { |
| 189 | + return String(str.get_sexp()); |
| 190 | + } |
| 191 | + return sugar::detail::trim_right(str.get_cstring()); |
| 192 | + } |
| 193 | + |
| 194 | + stop("Invalid `which` argument '%s'!", which); |
| 195 | + return String("Unreachable"); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +} // Rcpp |
| 200 | + |
| 201 | +#endif // Rcpp__sugar__trimws_h |
0 commit comments