Skip to content

refactor(utils): improve splitString using std::string_view input#8544

Open
ZAINABKANGSIWALA wants to merge 1 commit intocppcheck-opensource:mainfrom
ZAINABKANGSIWALA:improve-splitstring-stringview
Open

refactor(utils): improve splitString using std::string_view input#8544
ZAINABKANGSIWALA wants to merge 1 commit intocppcheck-opensource:mainfrom
ZAINABKANGSIWALA:improve-splitstring-stringview

Conversation

@ZAINABKANGSIWALA
Copy link
Copy Markdown

Refactor the parameter type of splitString() in utils.cpp from 'const std::string&' to 'std::string_view' to avoid unnecessary string construction when called with string literals or string-like inputs.

Changes:

  • utils.h: Declaration updated to use 'std::string_view'
  • utils.cpp: Parameter changed from 'const std::string&' to 'std::string_view'
  • utils.cpp: Updated 'std::string::size_type' to 'std::string_view::size_type'
  • utils.cpp: Replaced 'push_back' with 'emplace_back'
  • utils.cpp: Initialized 'pos2' with 'std::string_view::npos' to avoid uninitialized variable warning

Note:

  • clangimport.cpp contains a separate static splitString() with a different signature; it is unrelated and unchanged.
  • std::string_view is used only for input; ownership remains with the caller.

Call sites reviewed:

  • errorlogger.cpp: passes std::string (implicit conversion)
  • suppressions.cpp: passes std::string (implicit conversion)
  • cppcheck.cpp: passes substr() result (implicit conversion)

No functional change in behavior.

@firewave
Copy link
Copy Markdown
Collaborator

firewave commented May 9, 2026

Cppcheck targets C++11 so this doesn't work. If we move up to a newer standard it is very likely it will be done to C++14 first.

Also this is quite a pointless change unless you would also return views since you would still create copies. And the description which is unnecessarily multitudes bigger than the change is just baffling...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants