Skip to content

Use std::string_view in place of const std::string& #4300

Merged
garth-wells merged 17 commits into
mainfrom
garth/string-view
Jul 19, 2026
Merged

Use std::string_view in place of const std::string& #4300
garth-wells merged 17 commits into
mainfrom
garth/string-view

Conversation

@garth-wells

@garth-wells garth-wells commented Jul 18, 2026

Copy link
Copy Markdown
Member

Pass std::string_view in place of const std::string& to functions. This can avoid unnecessary (and rather hidden) creations of std::strings.

To avoid numerous std::string_view -> std::string constructions inside functions, make much greater use of std::format for string concatenation. This also tidies up and simplifies many cases of string handling.

Used Claude.

garth-wells and others added 2 commits July 14, 2026 15:49
…meters

Converts const std::string& parameters to std::string_view across
mesh::to_type, the XDMF/HDF5/VTK I/O layer, common::Table/TimeLogger/timing,
and the PETSc/SLEPc option-prefix and error-reporting helpers, avoiding
unnecessary string copies/allocations for callers passing literals or
substrings. Parameters that are stored, mutated in place, or are
map/vector element types are left as std::string. Updates the two
nanobind wrapper files that bind the changed functions directly to
include the string_view caster.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@garth-wells garth-wells added the housekeeping Tidying and style improvements label Jul 18, 2026
garth-wells and others added 13 commits July 18, 2026 09:05
Two real bugs left over from the const std::string& -> std::string_view
conversion:

- VTKFile.h: the constructor declaration's last parameter had been
  mangled to `std::string_view;`, dropping the parameter name and
  closing paren entirely (a hard compile error).
- xdmf_mesh.cpp: add_mesh's renamed parameter (name -> path_prefix)
  collided with a local variable of the same name computing the
  combined "/Mesh/<name>" path, causing a redefinition error; the stray
  "git" token in the initializer suggests this line was also corrupted
  in the same edit. Renamed the local to full_path_prefix.

Also reformats the rest of the branch's files with clang-format, which
had drifted out of compliance during the refactor.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This reverts commit f857f4d.
This reverts commit c9b113a.
Replace std::string/std::to_string + operator+ chains with std::format
across mesh, fem, io, graph, refinement, common, and la, matching the
existing usage elsewhere in the codebase. Also fixes a couple of latent
bugs surfaced along the way: missing spaces in two error messages
(Topology.cpp, dofmapbuilder.cpp) and math::det() reporting matrix data
values instead of the matrix shape in its error message.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
GCC 12/13 spuriously flags common::Timer::_start_time (a
std::optional<time_point>) as possibly uninitialized in
graph/partitioners.cpp, triggered by inlining changes in Timer's
destructor after TimeLogger::register_timing switched to
std::string_view. Add -Wno-maybe-uninitialized alongside the existing
GCC 12/13 workarounds for -Wno-array-bounds/-Wno-stringop-overflow.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@garth-wells garth-wells added the enhancement New feature or request label Jul 18, 2026
@garth-wells
garth-wells marked this pull request as ready for review July 18, 2026 16:46
@garth-wells
garth-wells added this pull request to the merge queue Jul 19, 2026
Merged via the queue into main with commit f5ad72e Jul 19, 2026
20 checks passed
@garth-wells
garth-wells deleted the garth/string-view branch July 19, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request housekeeping Tidying and style improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant