Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
string.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
10#include <algorithm>
11#include <cstring>
12
13namespace momentum {
14
15template <typename StringType>
16StringType trim(const StringType& text, const char* whiteChars) {
17 size_t end = text.length();
18 while (end > 0 && std::strchr(whiteChars, text[end - 1]) != nullptr) {
19 end--;
20 }
21 if (end == 0) {
22 return {};
23 }
24 size_t start = 0;
25 while (start < end && std::strchr(whiteChars, text[start]) != nullptr) {
26 start++;
27 }
28 if (start > 0 || end < text.length()) {
29 return text.substr(start, end - start);
30 }
31 return text;
32}
33
34template std::string trim(const std::string& text, const char* whiteChars);
35template std::string_view trim(const std::string_view& text, const char* whiteChars);
36
37std::string trim(const char* text, const char* whiteChars) {
38 return trim(std::string(text), whiteChars);
39}
40
41std::vector<std::string>
42tokenize(const std::string& inputString, const std::string& delimiters, const bool trim) {
43 if (inputString.empty()) {
44 return {};
45 }
46
47 std::vector<std::string> results;
48
49 size_t pos = 0;
50 size_t lastPos = 0;
51 while ((pos = inputString.find_first_of(delimiters, lastPos)) != std::string::npos) {
52 const std::string res = inputString.substr(lastPos, pos - lastPos);
53 // When `trim` is true, drop empty tokens (collapsing runs of consecutive delimiters);
54 // otherwise preserve them so callers can distinguish empty fields.
55 if (!res.empty() || !trim) {
56 results.push_back(res);
57 }
58 lastPos = pos + 1;
59 }
60 const std::string res = inputString.substr(lastPos);
61 if (!res.empty() || !trim) {
62 results.push_back(res);
63 }
64
65 return results;
66}
67
68std::vector<std::string_view>
69tokenize(std::string_view inputString, const std::string_view delimiters, const bool trim) {
70 if (inputString.empty()) {
71 return {};
72 }
73
74 std::vector<std::string_view> results;
75
76 size_t pos = 0;
77 size_t lastPos = 0;
78 while ((pos = inputString.find_first_of(delimiters, lastPos)) != std::string::npos) {
79 auto res = inputString.substr(lastPos, pos - lastPos);
80 // See `tokenize(std::string, ...)` above for the empty-token handling rationale.
81 if (!res.empty() || !trim) {
82 results.push_back(res);
83 }
84 lastPos = pos + 1;
85 }
86 auto res = inputString.substr(lastPos);
87 if (!res.empty() || !trim) {
88 results.push_back(res);
89 }
90
91 return results;
92}
93
94} // namespace momentum
StringType trim(const StringType &text, const char *whiteChars)
Returns a copy of the string with leading and trailing characters in whiteChars removed.
Definition string.cpp:16
std::vector< std::string > tokenize(const std::string &inputString, const std::string &delimiters, const bool trim)
Tokenizes a string using the specified single-byte delimiter characters.
Definition string.cpp:42