Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/stan/io/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,46 @@ inline void remove_first(std::string& input,
}
}

/**
* Replaces the first occurrence of a substring in a string in place.
*
* @param input string to modify
* @param substring substring to replace
* @param replacement replacement string
*/
inline void replace_first(std::string& input, const std::string_view& substring,
const std::string_view& replacement) {
const std::size_t position = input.find(substring);
if (position != std::string::npos) {
input.replace(position, substring.size(), replacement);
}
}

/**
* Tests whether a string contains a substring.
*
* @param input string to test
* @param substring substring to look for
* @return true if input contains substring
*/
inline bool contains(const std::string_view& input,
const std::string_view& substring) {
return input.find(substring) != std::string_view::npos;
}

/**
* Tests whether a string starts with a prefix.
*
* @param input string to test
* @param prefix prefix to look for
* @return true if input starts with prefix
*/
inline bool starts_with(const std::string_view& input,
const std::string_view& prefix) {
return input.size() >= prefix.size()
&& input.substr(0, prefix.size()) == prefix;
}

} // namespace io
} // namespace stan

Expand Down
Loading