Skip to content

Commit 9a0e93f

Browse files
committed
*: Replace string_view& with string_view
References to `string_view` are nearly always unnecessary indirection. Additionally, this CL clarifies that pw_sys_io WriteLine arguments do not need to be null-terminated. Fix: b/334933335 Fix: b/279159436 Change-Id: I13e0f8ad65d070e8363888242bc1a9b962be64ec Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/204591 Reviewed-by: Wyatt Hepler <hepler@google.com>
1 parent 1e8d9d0 commit 9a0e93f

File tree

28 files changed

+56
-57
lines changed

28 files changed

+56
-57
lines changed

pw_assert_basic/basic_handler.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static const char* kCrashBanner[] = {
7070
" ",
7171
};
7272

73-
static void WriteLine(const std::string_view& s) {
73+
static void WriteLine(std::string_view s) {
7474
pw::sys_io::WriteLine(s)
7575
.IgnoreError(); // TODO: b/242598609 - Handle Status properly
7676
}

pw_json/builder_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static_assert([] {
4242
.Add("dynamic allocation", false);
4343

4444
pw::NestedJsonArray nested_array = object.AddNestedArray("features");
45-
for (const std::string_view& feature : features) {
45+
for (const std::string_view feature : features) {
4646
nested_array.Append(feature);
4747
}
4848
// DOCTSAG: [pw-json-builder-example-1]

pw_span/span_test.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ TEST(SpanTest, DeductionGuides_FromReference) {
208208

209209
TEST(SpanTest, DeductionGuides_FromConstReference) {
210210
std::string_view string = "yo!";
211-
const std::string_view& string_ref = string;
211+
const std::string_view string_ref = string;
212212

213213
auto the_span = span(string_ref);
214214
static_assert(the_span.extent == dynamic_extent);

pw_spi_linux/cli.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ struct Args {
107107
};
108108

109109
template <class T>
110-
std::optional<T> ParseNumber(const std::string_view& str) {
110+
std::optional<T> ParseNumber(std::string_view str) {
111111
T value{};
112112
const auto* str_end = str.data() + str.size();
113113
auto [ptr, ec] = std::from_chars(str.data(), str_end, value);

pw_string/api.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ String utility functions
5252

5353
pw::string::Assign()
5454
--------------------
55-
.. doxygenfunction:: pw::string::Assign(InlineString<> &string, const std::string_view &view)
55+
.. doxygenfunction:: pw::string::Assign(InlineString<> &string, std::string_view view)
5656

5757
pw::string::Append()
5858
--------------------
59-
.. doxygenfunction:: pw::string::Append(InlineString<>& string, const std::string_view& view)
59+
.. doxygenfunction:: pw::string::Append(InlineString<>& string, std::string_view view)
6060

6161
pw::string::ClampedCString()
6262
----------------------------
@@ -67,13 +67,13 @@ pw::string::Copy()
6767
------------------
6868
.. doxygenfunction:: pw::string::Copy(const char* source, char* dest, size_t num)
6969
.. doxygenfunction:: pw::string::Copy(const char* source, Span&& dest)
70-
.. doxygenfunction:: pw::string::Copy(const std::string_view& source, Span&& dest)
70+
.. doxygenfunction:: pw::string::Copy(std::string_view source, Span&& dest)
7171

7272
It also has variants that provide a destination of ``pw::Vector<char>``
7373
(see :ref:`module-pw_containers` for details) that do not store the null
7474
terminator in the vector.
7575

76-
.. cpp:function:: StatusWithSize Copy(const std::string_view& source, pw::Vector<char>& dest)
76+
.. cpp:function:: StatusWithSize Copy(std::string_view source, pw::Vector<char>& dest)
7777
.. cpp:function:: StatusWithSize Copy(const char* source, pw::Vector<char>& dest)
7878

7979
pw::string::Format()
@@ -95,4 +95,4 @@ pw::string::NullTerminatedLength()
9595

9696
pw::string::PrintableCopy()
9797
---------------------------
98-
.. doxygenfunction:: pw::string::PrintableCopy(const std::string_view& source, span<char> dest)
98+
.. doxygenfunction:: pw::string::PrintableCopy(std::string_view source, span<char> dest)

pw_string/public/pw_string/string_builder.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ class StringBuilder {
209209
StringBuilder& append(const char* str);
210210

211211
/// Appends a `std::string_view` to the end of the `StringBuilder`.
212-
StringBuilder& append(const std::string_view& str);
212+
StringBuilder& append(std::string_view str);
213213

214214
/// Appends a substring from the `std::string_view` to the `StringBuilder`.
215215
/// Copies up to count characters starting from `pos` to the end of the
216216
/// `StringBuilder`. If `pos > str.size()`, sets the status to `OUT_OF_RANGE`.
217-
StringBuilder& append(const std::string_view& str,
217+
StringBuilder& append(std::string_view str,
218218
size_t pos,
219219
size_t count = std::string_view::npos);
220220

pw_string/public/pw_string/type_to_string.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ StatusWithSize PointerToString(const void* pointer, span<char> buffer);
114114
//
115115
// Returns the number of characters written, excluding the null terminator. If
116116
// the string is truncated, the status is RESOURCE_EXHAUSTED.
117-
inline StatusWithSize CopyStringOrNull(const std::string_view& value,
117+
inline StatusWithSize CopyStringOrNull(std::string_view value,
118118
span<char> buffer) {
119119
return Copy(value, buffer);
120120
}
@@ -133,7 +133,7 @@ inline StatusWithSize CopyStringOrNull(const char* value, span<char> buffer) {
133133
// Returns the number of characters written, excluding the null terminator. If
134134
// the full string does not fit, only a null terminator is written and the
135135
// status is RESOURCE_EXHAUSTED.
136-
StatusWithSize CopyEntireStringOrNull(const std::string_view& value,
136+
StatusWithSize CopyEntireStringOrNull(std::string_view value,
137137
span<char> buffer);
138138

139139
// Same as the string_view form of CopyEntireString, except that if value is a

pw_string/public/pw_string/util.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ namespace pw {
3434
namespace string {
3535
namespace internal {
3636

37-
PW_CONSTEXPR_CPP20 inline StatusWithSize CopyToSpan(
38-
const std::string_view& source, span<char> dest) {
37+
PW_CONSTEXPR_CPP20 inline StatusWithSize CopyToSpan(std::string_view source,
38+
span<char> dest) {
3939
if (dest.empty()) {
4040
return StatusWithSize::ResourceExhausted();
4141
}
@@ -117,7 +117,7 @@ constexpr Result<size_t> NullTerminatedLength(const char* str, size_t max_len) {
117117
///
118118
/// @endrst
119119
template <typename Span>
120-
PW_CONSTEXPR_CPP20 inline StatusWithSize Copy(const std::string_view& source,
120+
PW_CONSTEXPR_CPP20 inline StatusWithSize Copy(std::string_view source,
121121
Span&& dest) {
122122
static_assert(
123123
!std::is_base_of_v<InlineString<>, std::decay_t<Span>>,
@@ -155,7 +155,7 @@ PW_CONSTEXPR_CPP20 inline StatusWithSize Copy(const char* source,
155155
/// RESOURCE_EXHAUSTED: The ``std::string_view`` was truncated to fit.
156156
///
157157
/// @endrst
158-
inline Status Assign(InlineString<>& string, const std::string_view& view) {
158+
inline Status Assign(InlineString<>& string, std::string_view view) {
159159
const size_t chars_copied =
160160
std::min(view.size(), static_cast<size_t>(string.capacity()));
161161
string.assign(view, 0, static_cast<string_impl::size_type>(chars_copied));
@@ -182,7 +182,7 @@ inline Status Assign(InlineString<>& string, const char* c_string) {
182182
/// RESOURCE_EXHAUSTED: The ``std::string_view`` was truncated to fit.
183183
///
184184
/// @endrst
185-
inline Status Append(InlineString<>& string, const std::string_view& view) {
185+
inline Status Append(InlineString<>& string, std::string_view view) {
186186
const size_t chars_copied = std::min(
187187
view.size(), static_cast<size_t>(string.capacity() - string.size()));
188188
string.append(view, 0, static_cast<string_impl::size_type>(chars_copied));
@@ -200,8 +200,8 @@ inline Status Append(InlineString<>& string, const char* c_string) {
200200
/// Copies the `source` string to the `dest` string with same behavior as
201201
/// `pw::string::Copy`, with the difference that any non-printable characters
202202
/// are changed to `.`.
203-
PW_CONSTEXPR_CPP20 inline StatusWithSize PrintableCopy(
204-
const std::string_view& source, span<char> dest) {
203+
PW_CONSTEXPR_CPP20 inline StatusWithSize PrintableCopy(std::string_view source,
204+
span<char> dest) {
205205
StatusWithSize copy_result = Copy(source, dest);
206206
for (size_t i = 0; i < copy_result.size(); i++) {
207207
dest[i] = std::isprint(dest[i]) ? dest[i] : '.';

pw_string/string_builder.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ StringBuilder& StringBuilder::append(const char* str) {
4747
return append(string::ClampedCString(str, buffer_.size() - size()));
4848
}
4949

50-
StringBuilder& StringBuilder::append(const std::string_view& str) {
50+
StringBuilder& StringBuilder::append(std::string_view str) {
5151
return append(str.data(), str.size());
5252
}
5353

54-
StringBuilder& StringBuilder::append(const std::string_view& str,
54+
StringBuilder& StringBuilder::append(std::string_view str,
5555
size_t pos,
5656
size_t count) {
5757
if (pos > str.size()) {

pw_string/type_to_string.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ StatusWithSize PointerToString(const void* pointer, span<char> buffer) {
7575
return IntToHexString(reinterpret_cast<uintptr_t>(pointer), buffer);
7676
}
7777

78-
StatusWithSize CopyEntireStringOrNull(const std::string_view& value,
78+
StatusWithSize CopyEntireStringOrNull(std::string_view value,
7979
span<char> buffer) {
8080
if (value.size() >= buffer.size()) {
8181
return internal::HandleExhaustedBuffer(buffer);

pw_sys_io/docs.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ API reference
4444
.. doxygenfunction:: pw::sys_io::ReadByte(std::byte* dest)
4545
.. doxygenfunction:: pw::sys_io::TryReadByte(std::byte* dest)
4646
.. doxygenfunction:: pw::sys_io::WriteByte(std::byte b)
47-
.. doxygenfunction:: pw::sys_io::WriteLine(const std::string_view& s)
47+
.. doxygenfunction:: pw::sys_io::WriteLine(std::string_view s)
4848
.. doxygenfunction:: pw::sys_io::ReadBytes(ByteSpan dest)
4949
.. doxygenfunction:: pw::sys_io::WriteBytes(ConstByteSpan src)
5050

pw_sys_io/public/pw_sys_io/sys_io.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ Status WriteByte(std::byte b);
101101

102102
/// Writes a string out the `pw_sys_io` backend.
103103
///
104-
/// This function takes a null-terminated string and writes it out the
105-
/// `pw_sys_io` backend, adding any platform-specific newline character(s)
106-
/// (these are accounted for in the returned `StatusWithSize`).
104+
/// This function takes a `string_view` and writes it out the `pw_sys_io`
105+
/// backend, adding any platform-specific newline character(s) (these are
106+
/// accounted for in the returned `StatusWithSize`).
107107
///
108108
/// @pre This function must be implemented by the `pw_sys_io` backend.
109109
///
@@ -120,7 +120,7 @@ Status WriteByte(std::byte b);
120120
/// part of the ``StatusWithSize``.
121121
///
122122
/// @endrst
123-
StatusWithSize WriteLine(const std::string_view& s);
123+
StatusWithSize WriteLine(std::string_view s);
124124

125125
/// Fills a byte span from the `pw_sys_io` backend using `ReadByte()`.
126126
///

pw_sys_io_ambiq_sdk/sys_io.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Status WriteByte(std::byte b) {
113113
}
114114

115115
// Writes a string using pw::sys_io, and add newline characters at the end.
116-
StatusWithSize WriteLine(const std::string_view& s) {
116+
StatusWithSize WriteLine(std::string_view s) {
117117
StatusWithSize result = WriteBytes(as_bytes(span(s)));
118118
if (!result.ok()) {
119119
return result;

pw_sys_io_arduino/sys_io_arduino.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Status WriteByte(std::byte b) {
6363
}
6464

6565
// Writes a string using pw::sys_io, and add newline characters at the end.
66-
StatusWithSize WriteLine(const std::string_view& s) {
66+
StatusWithSize WriteLine(std::string_view s) {
6767
size_t chars_written = 0;
6868
StatusWithSize result = WriteBytes(as_bytes(span(s)));
6969
if (!result.ok()) {

pw_sys_io_baremetal_lm3s6965evb/sys_io_baremetal.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Status WriteByte(std::byte b) {
119119
}
120120

121121
// Writes a string using pw::sys_io, and add newline characters at the end.
122-
StatusWithSize WriteLine(const std::string_view& s) {
122+
StatusWithSize WriteLine(std::string_view s) {
123123
size_t chars_written = 0;
124124
StatusWithSize result = WriteBytes(as_bytes(span(s)));
125125
if (!result.ok()) {

pw_sys_io_baremetal_stm32f429/sys_io_baremetal.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Status WriteByte(std::byte b) {
205205
}
206206

207207
// Writes a string using pw::sys_io, and add newline characters at the end.
208-
StatusWithSize WriteLine(const std::string_view& s) {
208+
StatusWithSize WriteLine(std::string_view s) {
209209
size_t chars_written = 0;
210210
StatusWithSize result = WriteBytes(as_bytes(span(s)));
211211
if (!result.ok()) {

pw_sys_io_emcraft_sf2/sys_io_emcraft_sf2.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Status WriteByte(std::byte b) {
8686
}
8787

8888
// Writes a string using pw::sys_io, and add newline characters at the end.
89-
StatusWithSize WriteLine(const std::string_view& s) {
89+
StatusWithSize WriteLine(std::string_view s) {
9090
size_t chars_written = 0;
9191
StatusWithSize result = WriteBytes(as_bytes(span(s)));
9292
if (!result.ok()) {

pw_sys_io_mcuxpresso/sys_io.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Status WriteByte(std::byte b) {
6262
}
6363

6464
// Writes a string using pw::sys_io, and add newline characters at the end.
65-
StatusWithSize WriteLine(const std::string_view& s) {
65+
StatusWithSize WriteLine(std::string_view s) {
6666
size_t chars_written = 0;
6767
StatusWithSize result = WriteBytes(as_bytes(span(s)));
6868
if (!result.ok()) {

pw_sys_io_rp2040/sys_io.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Status WriteByte(std::byte b) {
8888
}
8989

9090
// Writes a string using pw::sys_io, and add newline characters at the end.
91-
StatusWithSize WriteLine(const std::string_view& s) {
91+
StatusWithSize WriteLine(std::string_view s) {
9292
size_t chars_written = 0;
9393
StatusWithSize result = WriteBytes(as_bytes(span(s)));
9494
if (!result.ok()) {

pw_sys_io_stdio/sys_io.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Status WriteByte(std::byte b) {
4343
return OkStatus();
4444
}
4545

46-
StatusWithSize WriteLine(const std::string_view& s) {
46+
StatusWithSize WriteLine(std::string_view s) {
4747
size_t chars_written = 0;
4848
StatusWithSize size_result = WriteBytes(as_bytes(span(s)));
4949
if (!size_result.ok()) {

pw_sys_io_stm32cube/sys_io.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Status WriteByte(std::byte b) {
124124
}
125125

126126
// Writes a string using pw::sys_io, and add newline characters at the end.
127-
StatusWithSize WriteLine(const std::string_view& s) {
127+
StatusWithSize WriteLine(std::string_view s) {
128128
size_t chars_written = 0;
129129
StatusWithSize result = WriteBytes(as_bytes(span(s)));
130130
if (!result.ok()) {

pw_sys_io_zephyr/sys_io.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Status WriteByte(std::byte b) {
6969
: OkStatus();
7070
}
7171

72-
StatusWithSize WriteLine(const std::string_view& s) {
72+
StatusWithSize WriteLine(std::string_view s) {
7373
size_t chars_written = 0;
7474
StatusWithSize size_result = WriteBytes(as_bytes(span(s)));
7575
if (!size_result.ok()) {

pw_tokenizer/decode.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ std::array<char, 2> ReadLengthModifier(const char* str) {
6161
// Returns the error message that is used in place of a decoded arg when an
6262
// error occurs.
6363
std::string ErrorMessage(ArgStatus status,
64-
const std::string_view& spec,
65-
const std::string_view& value) {
64+
std::string_view spec,
65+
std::string_view value) {
6666
const char* message;
6767
if (status.HasError(ArgStatus::kSkipped)) {
6868
message = "SKIPPED";
@@ -93,9 +93,9 @@ std::string ErrorMessage(ArgStatus status,
9393
} // namespace
9494

9595
DecodedArg::DecodedArg(ArgStatus error,
96-
const std::string_view& spec,
96+
std::string_view spec,
9797
size_t raw_size_bytes,
98-
const std::string_view& value)
98+
std::string_view value)
9999
: value_(ErrorMessage(error, spec, value)),
100100
spec_(spec),
101101
raw_data_size_bytes_(raw_size_bytes),

pw_tokenizer/public/pw_tokenizer/internal/decode.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ class DecodedArg {
9191

9292
// Constructs a DecodedArg that encountered an error during decoding.
9393
DecodedArg(ArgStatus error,
94-
const std::string_view& spec,
94+
std::string_view spec,
9595
size_t raw_size_bytes = 0u,
96-
const std::string_view& value = {});
96+
std::string_view value = {});
9797

9898
// This argument's value as a string. If an error occurred while decoding this
9999
// argument, value() will be an error message.
@@ -129,7 +129,7 @@ class StringSegment {
129129
static StringSegment ParseFormatSpec(const char* format);
130130

131131
// Creates a StringSegment that represents a piece of plain text.
132-
StringSegment(const std::string_view& text) : StringSegment(text, kLiteral) {}
132+
StringSegment(std::string_view text) : StringSegment(text, kLiteral) {}
133133

134134
// Returns the DecodedArg with this StringSegment decoded according to the
135135
// provided arguments.
@@ -165,10 +165,10 @@ class StringSegment {
165165

166166
StringSegment() : type_(kLiteral) {}
167167

168-
StringSegment(const std::string_view& text, Type type)
168+
StringSegment(std::string_view text, Type type)
169169
: StringSegment(text, type, VarargSize<void*>()) {}
170170

171-
StringSegment(const std::string_view& text, Type type, ArgSize local_size)
171+
StringSegment(std::string_view text, Type type, ArgSize local_size)
172172
: text_(text), type_(type), local_size_(local_size) {}
173173

174174
DecodedArg DecodeString(const span<const uint8_t>& arguments) const;
@@ -232,7 +232,7 @@ class FormatString {
232232
// returns a string.
233233
DecodedFormatString Format(span<const uint8_t> arguments) const;
234234

235-
DecodedFormatString Format(const std::string_view& arguments) const {
235+
DecodedFormatString Format(std::string_view arguments) const {
236236
return Format(span(reinterpret_cast<const uint8_t*>(arguments.data()),
237237
arguments.size()));
238238
}

pw_transfer/atomic_file_transfer_handler.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pw::Status EnsureDirectoryExists(std::string_view filepath) {
3737

3838
// Copy file and remove on succes.
3939
// If the copy fails, the file `input_target` is not removed.
40-
pw::Status CopyFile(const std::string_view input_target,
41-
const std::string_view output_target) {
40+
pw::Status CopyFile(std::string_view input_target,
41+
std::string_view output_target) {
4242
auto err = std::error_code{};
4343
std::filesystem::copy(input_target,
4444
output_target,
@@ -60,8 +60,8 @@ pw::Status CopyFile(const std::string_view input_target,
6060

6161
// Uses the same approach as unix `mv` command. First try to rename. If we get
6262
// a cross-device link error, copies then deletes input_target.
63-
pw::Status RenameFile(const std::string_view input_target,
64-
const std::string_view output_target) {
63+
pw::Status RenameFile(std::string_view input_target,
64+
std::string_view output_target) {
6565
auto err = std::error_code{};
6666
std::filesystem::rename(input_target, output_target, err);
6767
if (err && err.value() == CROSS_DEVICE_LINK_ERROR) {

pw_unit_test/docs.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ To do more complex testing, such as on-device testing:
293293
// pw_unit_test:light requires an event handler to be configured.
294294
#include "pw_unit_test/simple_printing_event_handler.h"
295295

296-
void WriteString(const std::string_view& string, bool newline) {
296+
void WriteString(std::string_view string, bool newline) {
297297
printf("%s", string.data());
298298
if (newline) {
299299
printf("\n");

0 commit comments

Comments
 (0)