|
| 1 | +// Copyright CERN and copyright holders of ALICE O2. This software is |
| 2 | +// distributed under the terms of the GNU General Public License v3 (GPL |
| 3 | +// Version 3), copied verbatim in the file "COPYING". |
| 4 | +// |
| 5 | +// See http://alice-o2.web.cern.ch/license for full licensing information. |
| 6 | +// |
| 7 | +// In applying this license CERN does not waive the privileges and immunities |
| 8 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 9 | +// or submit itself to any jurisdiction. |
| 10 | + |
| 11 | +/// |
| 12 | +/// \file StringUtils.h |
| 13 | +/// \author Barthelemy von Haller |
| 14 | +/// |
| 15 | + |
| 16 | +#ifndef ALICEO2_STRINGUTILS_H |
| 17 | +#define ALICEO2_STRINGUTILS_H |
| 18 | + |
| 19 | +namespace o2 |
| 20 | +{ |
| 21 | +namespace utils |
| 22 | +{ |
| 23 | + |
| 24 | +// Code for trimming coming from https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring |
| 25 | + |
| 26 | +/** |
| 27 | + * Trim from start (in place) |
| 28 | + * @param s |
| 29 | + */ |
| 30 | +static inline void ltrim(std::string& s) |
| 31 | +{ |
| 32 | + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { |
| 33 | + return !std::isspace(ch); |
| 34 | + })); |
| 35 | +} |
| 36 | + |
| 37 | +/** Trim from end (in place) |
| 38 | + * |
| 39 | + * @param s |
| 40 | + */ |
| 41 | +static inline void rtrim(std::string& s) |
| 42 | +{ |
| 43 | + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { |
| 44 | + return !std::isspace(ch); |
| 45 | + }).base(), |
| 46 | + s.end()); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Trim from both ends (in place) |
| 51 | + * @param s |
| 52 | + */ |
| 53 | +static inline void trim(std::string& s) |
| 54 | +{ |
| 55 | + ltrim(s); |
| 56 | + rtrim(s); |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Trim from start (copying) |
| 61 | + * @param s |
| 62 | + * @return |
| 63 | + */ |
| 64 | +static inline std::string ltrim_copy(std::string s) |
| 65 | +{ |
| 66 | + ltrim(s); |
| 67 | + return s; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Trim from end (copying) |
| 72 | + * @param s |
| 73 | + * @return |
| 74 | + */ |
| 75 | +static inline std::string rtrim_copy(std::string s) |
| 76 | +{ |
| 77 | + rtrim(s); |
| 78 | + return s; |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace utils |
| 82 | +} // namespace o2 |
| 83 | + |
| 84 | +#endif //ALICEO2_STRINGUTILS_H |
0 commit comments