Skip to content

Commit 5a8cc34

Browse files
authored
Merge pull request #3415 from stan-dev/string-utils
Additional string utilities for cmdstan's Boost replacement
2 parents 2f304ee + d14187c commit 5a8cc34

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

src/stan/io/string_utils.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,46 @@ inline void remove_first(std::string& input,
102102
}
103103
}
104104

105+
/**
106+
* Replaces the first occurrence of a substring in a string in place.
107+
*
108+
* @param input string to modify
109+
* @param substring substring to replace
110+
* @param replacement replacement string
111+
*/
112+
inline void replace_first(std::string& input, const std::string_view& substring,
113+
const std::string_view& replacement) {
114+
const std::size_t position = input.find(substring);
115+
if (position != std::string::npos) {
116+
input.replace(position, substring.size(), replacement);
117+
}
118+
}
119+
120+
/**
121+
* Tests whether a string contains a substring.
122+
*
123+
* @param input string to test
124+
* @param substring substring to look for
125+
* @return true if input contains substring
126+
*/
127+
inline bool contains(const std::string_view& input,
128+
const std::string_view& substring) {
129+
return input.find(substring) != std::string_view::npos;
130+
}
131+
132+
/**
133+
* Tests whether a string starts with a prefix.
134+
*
135+
* @param input string to test
136+
* @param prefix prefix to look for
137+
* @return true if input starts with prefix
138+
*/
139+
inline bool starts_with(const std::string_view& input,
140+
const std::string_view& prefix) {
141+
return input.size() >= prefix.size()
142+
&& input.substr(0, prefix.size()) == prefix;
143+
}
144+
105145
} // namespace io
106146
} // namespace stan
107147

0 commit comments

Comments
 (0)