From 42685106ca98574831de17c36f64189a6db272ed Mon Sep 17 00:00:00 2001 From: Daniel Kroening Date: Fri, 3 Jul 2026 06:01:54 -0700 Subject: [PATCH] use std::string_view for has_prefix, has_infix, has_suffix The has_prefix, has_infix, has_suffix functions do not copy the given strings, and hence, using std::string_view can be used to relieve the caller from constructing the std::string object. --- src/util/infix.h | 7 ++----- src/util/prefix.h | 6 +++--- src/util/suffix.h | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/util/infix.h b/src/util/infix.h index 0ff47c646c1..6122c0e35bb 100644 --- a/src/util/infix.h +++ b/src/util/infix.h @@ -12,12 +12,9 @@ Author: Chris Smowton, chris.smowton@diffblue.com #ifndef CPROVER_UTIL_INFIX_H #define CPROVER_UTIL_INFIX_H -#include +#include -inline bool has_infix( - const std::string &s, - const std::string &infix, - size_t offset) +inline bool has_infix(std::string_view s, std::string_view infix, size_t offset) { return s.compare(offset, infix.size(), infix)==0; } diff --git a/src/util/prefix.h b/src/util/prefix.h index b94c1cd93d2..3d49f1949c1 100644 --- a/src/util/prefix.h +++ b/src/util/prefix.h @@ -10,11 +10,11 @@ Author: Daniel Kroening, kroening@kroening.com #ifndef CPROVER_UTIL_PREFIX_H #define CPROVER_UTIL_PREFIX_H -#include +#include -// C++20 will have std::string::starts_with +// C++20 will have std::string_view::starts_with -inline bool has_prefix(const std::string &s, const std::string &prefix) +inline bool has_prefix(std::string_view s, std::string_view prefix) { return s.compare(0, prefix.size(), prefix)==0; } diff --git a/src/util/suffix.h b/src/util/suffix.h index 4ec273d89f9..10fa8f30cdf 100644 --- a/src/util/suffix.h +++ b/src/util/suffix.h @@ -12,9 +12,9 @@ Author: Daniel Kroening, kroening@kroening.com #include -// C++20 will have std::string::ends_with +// C++20 will have std::string_view::ends_with -inline bool has_suffix(const std::string &s, const std::string &suffix) +inline bool has_suffix(std::string_view s, std::string_view suffix) { if(suffix.size()>s.size()) return false;