Change signatures of SDP ParseFailed* functions

Change types of const std::string& arguments.

Use absl::string_view for the reference to input, to prepare for
parsing with less copies. Use std::string (passed by value) for the
description, to support ownership transfer without copying.

Bug: None
Change-Id: I4358b42bb824e4eb7a5ac9b64d44db1b9b022bab
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/223667
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34721}
This commit is contained in:
Niels Möller 2021-08-11 10:07:45 +02:00 committed by WebRTC LUCI CQ
parent 9367cff59b
commit a820cc226e

View file

@ -396,12 +396,12 @@ static void RemoveInvalidRidsFromSimulcast(
// The line starting at `line_start` of `message` is the failing line. // The line starting at `line_start` of `message` is the failing line.
// The reason for the failure should be provided in the `description`. // The reason for the failure should be provided in the `description`.
// An example of a description could be "unknown character". // An example of a description could be "unknown character".
static bool ParseFailed(const std::string& message, static bool ParseFailed(absl::string_view message,
size_t line_start, size_t line_start,
const std::string& description, std::string description,
SdpParseError* error) { SdpParseError* error) {
// Get the first line of `message` from `line_start`. // Get the first line of `message` from `line_start`.
std::string first_line; absl::string_view first_line;
size_t line_end = message.find(kNewLine, line_start); size_t line_end = message.find(kNewLine, line_start);
if (line_end != std::string::npos) { if (line_end != std::string::npos) {
if (line_end > 0 && (message.at(line_end - 1) == kReturnChar)) { if (line_end > 0 && (message.at(line_end - 1) == kReturnChar)) {
@ -412,72 +412,72 @@ static bool ParseFailed(const std::string& message,
first_line = message.substr(line_start); first_line = message.substr(line_start);
} }
if (error) {
error->line = first_line;
error->description = description;
}
RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line RTC_LOG(LS_ERROR) << "Failed to parse: \"" << first_line
<< "\". Reason: " << description; << "\". Reason: " << description;
if (error) {
error->line = std::string(first_line);
error->description = std::move(description);
}
return false; return false;
} }
// `line` is the failing line. The reason for the failure should be // `line` is the failing line. The reason for the failure should be
// provided in the `description`. // provided in the `description`.
static bool ParseFailed(const std::string& line, static bool ParseFailed(absl::string_view line,
const std::string& description, std::string description,
SdpParseError* error) { SdpParseError* error) {
return ParseFailed(line, 0, description, error); return ParseFailed(line, 0, std::move(description), error);
} }
// Parses failure where the failing SDP line isn't know or there are multiple // Parses failure where the failing SDP line isn't know or there are multiple
// failing lines. // failing lines.
static bool ParseFailed(const std::string& description, SdpParseError* error) { static bool ParseFailed(std::string description, SdpParseError* error) {
return ParseFailed("", description, error); return ParseFailed("", std::move(description), error);
} }
// `line` is the failing line. The failure is due to the fact that `line` // `line` is the failing line. The failure is due to the fact that `line`
// doesn't have `expected_fields` fields. // doesn't have `expected_fields` fields.
static bool ParseFailedExpectFieldNum(const std::string& line, static bool ParseFailedExpectFieldNum(absl::string_view line,
int expected_fields, int expected_fields,
SdpParseError* error) { SdpParseError* error) {
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Expects " << expected_fields << " fields."; description << "Expects " << expected_fields << " fields.";
return ParseFailed(line, description.str(), error); return ParseFailed(line, description.Release(), error);
} }
// `line` is the failing line. The failure is due to the fact that `line` has // `line` is the failing line. The failure is due to the fact that `line` has
// less than `expected_min_fields` fields. // less than `expected_min_fields` fields.
static bool ParseFailedExpectMinFieldNum(const std::string& line, static bool ParseFailedExpectMinFieldNum(absl::string_view line,
int expected_min_fields, int expected_min_fields,
SdpParseError* error) { SdpParseError* error) {
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Expects at least " << expected_min_fields << " fields."; description << "Expects at least " << expected_min_fields << " fields.";
return ParseFailed(line, description.str(), error); return ParseFailed(line, description.Release(), error);
} }
// `line` is the failing line. The failure is due to the fact that it failed to // `line` is the failing line. The failure is due to the fact that it failed to
// get the value of `attribute`. // get the value of `attribute`.
static bool ParseFailedGetValue(const std::string& line, static bool ParseFailedGetValue(absl::string_view line,
const std::string& attribute, const std::string& attribute,
SdpParseError* error) { SdpParseError* error) {
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Failed to get the value of attribute: " << attribute; description << "Failed to get the value of attribute: " << attribute;
return ParseFailed(line, description.str(), error); return ParseFailed(line, description.Release(), error);
} }
// The line starting at `line_start` of `message` is the failing line. The // The line starting at `line_start` of `message` is the failing line. The
// failure is due to the line type (e.g. the "m" part of the "m-line") // failure is due to the line type (e.g. the "m" part of the "m-line")
// not matching what is expected. The expected line type should be // not matching what is expected. The expected line type should be
// provided as `line_type`. // provided as `line_type`.
static bool ParseFailedExpectLine(const std::string& message, static bool ParseFailedExpectLine(absl::string_view message,
size_t line_start, size_t line_start,
const char line_type, const char line_type,
const std::string& line_value, absl::string_view line_value,
SdpParseError* error) { SdpParseError* error) {
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Expect line: " << std::string(1, line_type) << "=" description << "Expect line: " << std::string(1, line_type) << "="
<< line_value; << line_value;
return ParseFailed(message, line_start, description.str(), error); return ParseFailed(message, line_start, description.Release(), error);
} }
static bool AddLine(const std::string& line, std::string* message) { static bool AddLine(const std::string& line, std::string* message) {
@ -638,7 +638,7 @@ static bool GetSingleTokenValue(const std::string& message,
if (strspn(value->c_str(), kLegalTokenCharacters) != value->size()) { if (strspn(value->c_str(), kLegalTokenCharacters) != value->size()) {
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Illegal character found in the value of " << attribute; description << "Illegal character found in the value of " << attribute;
return ParseFailed(message, description.str(), error); return ParseFailed(message, description.Release(), error);
} }
return true; return true;
} }
@ -657,7 +657,7 @@ static bool GetValueFromString(const std::string& line,
if (!rtc::FromString(s, t)) { if (!rtc::FromString(s, t)) {
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Invalid value: " << s << "."; description << "Invalid value: " << s << ".";
return ParseFailed(line, description.str(), error); return ParseFailed(line, description.Release(), error);
} }
return true; return true;
} }
@ -1080,7 +1080,7 @@ bool ParseCandidate(const std::string& message,
description << "Expect line: " << kAttributeCandidate description << "Expect line: " << kAttributeCandidate
<< ":" << ":"
"<candidate-str>"; "<candidate-str>";
return ParseFailed(first_line, 0, description.str(), error); return ParseFailed(first_line, 0, description.Release(), error);
} else { } else {
return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes, return ParseFailedExpectLine(first_line, 0, kLineTypeAttributes,
kAttributeCandidate, error); kAttributeCandidate, error);
@ -3426,7 +3426,7 @@ bool ParseSsrcAttribute(const std::string& line,
rtc::StringBuilder description; rtc::StringBuilder description;
description << "Failed to get the ssrc attribute value from " << field2 description << "Failed to get the ssrc attribute value from " << field2
<< ". Expected format <attribute>:<value>."; << ". Expected format <attribute>:<value>.";
return ParseFailed(line, description.str(), error); return ParseFailed(line, description.Release(), error);
} }
// Check if there's already an item for this `ssrc_id`. Create a new one if // Check if there's already an item for this `ssrc_id`. Create a new one if