From 517e34c03d76f47fc044cf74a8999d512ed05675 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 25 Aug 2026 19:32:59 +0800 Subject: [PATCH 1/3] Add string_utils for cmdstan --- src/stan/io/ends_with.hpp | 23 ------------ src/stan/io/string_utils.hpp | 54 +++++++++++++++++++++++++++++ src/test/unit/io/ends_with_test.cpp | 4 +-- 3 files changed, 56 insertions(+), 25 deletions(-) delete mode 100644 src/stan/io/ends_with.hpp diff --git a/src/stan/io/ends_with.hpp b/src/stan/io/ends_with.hpp deleted file mode 100644 index 9b279f7a24e..00000000000 --- a/src/stan/io/ends_with.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef STAN_IO_ENDS_WITH_HPP -#define STAN_IO_ENDS_WITH_HPP - -#include - -namespace stan { -namespace io { - -/** - * Return true if the specified string ends with the specified - * suffix. - * - * @param p suffix - * @param s string to test - * @return true if s has p as a suffix - */ -inline bool ends_with(const std::string& p, const std::string& s) { - return s.size() >= p.size() && s.substr(s.size() - p.size()) == p; -} - -} // namespace io -} // namespace stan -#endif diff --git a/src/stan/io/string_utils.hpp b/src/stan/io/string_utils.hpp index 1a03046dc0a..cbe65f7c1a0 100644 --- a/src/stan/io/string_utils.hpp +++ b/src/stan/io/string_utils.hpp @@ -102,6 +102,60 @@ 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 ends with a suffix. + * + * @param input string to test + * @param suffix suffix to look for + * @return true if input ends with suffix + */ +inline bool ends_with(const std::string_view& input, + const std::string_view& suffix) { + return input.size() >= suffix.size() + && input.substr(input.size() - suffix.size()) == suffix; +} + +/** + * 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 diff --git a/src/test/unit/io/ends_with_test.cpp b/src/test/unit/io/ends_with_test.cpp index e91c55cd7f5..4c102ca5649 100644 --- a/src/test/unit/io/ends_with_test.cpp +++ b/src/test/unit/io/ends_with_test.cpp @@ -1,9 +1,9 @@ -#include +#include #include #include void expect_ends(const std::string& p, const std::string& s, bool cond) { - EXPECT_EQ(cond, stan::io::ends_with(p, s)); + EXPECT_EQ(cond, stan::io::ends_with(s, p)); } TEST(Io, EndsWith) { From 14ce3360e9f5016f8ee6cd62ea76531934fcfa0c Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 25 Aug 2026 07:46:15 -0400 Subject: [PATCH 2/3] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/io/string_utils.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/stan/io/string_utils.hpp b/src/stan/io/string_utils.hpp index cbe65f7c1a0..01d8609138f 100644 --- a/src/stan/io/string_utils.hpp +++ b/src/stan/io/string_utils.hpp @@ -109,8 +109,7 @@ inline void remove_first(std::string& input, * @param substring substring to replace * @param replacement replacement string */ -inline void replace_first(std::string& input, - const std::string_view& substring, +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) { From d14187cb07e29d21b213507da3dd1686d4bf0465 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Wed, 26 Aug 2026 11:55:32 +0800 Subject: [PATCH 3/3] Revert ends_with changes --- src/stan/io/ends_with.hpp | 23 +++++++++++++++++++++++ src/stan/io/string_utils.hpp | 13 ------------- src/test/unit/io/ends_with_test.cpp | 4 ++-- 3 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 src/stan/io/ends_with.hpp diff --git a/src/stan/io/ends_with.hpp b/src/stan/io/ends_with.hpp new file mode 100644 index 00000000000..9b279f7a24e --- /dev/null +++ b/src/stan/io/ends_with.hpp @@ -0,0 +1,23 @@ +#ifndef STAN_IO_ENDS_WITH_HPP +#define STAN_IO_ENDS_WITH_HPP + +#include + +namespace stan { +namespace io { + +/** + * Return true if the specified string ends with the specified + * suffix. + * + * @param p suffix + * @param s string to test + * @return true if s has p as a suffix + */ +inline bool ends_with(const std::string& p, const std::string& s) { + return s.size() >= p.size() && s.substr(s.size() - p.size()) == p; +} + +} // namespace io +} // namespace stan +#endif diff --git a/src/stan/io/string_utils.hpp b/src/stan/io/string_utils.hpp index 01d8609138f..032dc9f31f6 100644 --- a/src/stan/io/string_utils.hpp +++ b/src/stan/io/string_utils.hpp @@ -129,19 +129,6 @@ inline bool contains(const std::string_view& input, return input.find(substring) != std::string_view::npos; } -/** - * Tests whether a string ends with a suffix. - * - * @param input string to test - * @param suffix suffix to look for - * @return true if input ends with suffix - */ -inline bool ends_with(const std::string_view& input, - const std::string_view& suffix) { - return input.size() >= suffix.size() - && input.substr(input.size() - suffix.size()) == suffix; -} - /** * Tests whether a string starts with a prefix. * diff --git a/src/test/unit/io/ends_with_test.cpp b/src/test/unit/io/ends_with_test.cpp index 4c102ca5649..e91c55cd7f5 100644 --- a/src/test/unit/io/ends_with_test.cpp +++ b/src/test/unit/io/ends_with_test.cpp @@ -1,9 +1,9 @@ -#include +#include #include #include void expect_ends(const std::string& p, const std::string& s, bool cond) { - EXPECT_EQ(cond, stan::io::ends_with(s, p)); + EXPECT_EQ(cond, stan::io::ends_with(p, s)); } TEST(Io, EndsWith) {