Add string_view version of cricket::StringToProto

And deprecate old version.

Bug: webrtc:13579
Change-Id: I3eda669fdaa814c0e3c75a78242279bf9e526b1c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/262241
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36999}
This commit is contained in:
Niels Möller
2022-05-20 15:44:37 +02:00
committed by WebRTC LUCI CQ
parent f7f0b2108f
commit 4662f53285
4 changed files with 28 additions and 12 deletions

View File

@ -80,13 +80,21 @@ const char* ProtoToString(ProtocolType proto) {
return PROTO_NAMES[proto]; return PROTO_NAMES[proto];
} }
bool StringToProto(const char* value, ProtocolType* proto) { absl::optional<ProtocolType> StringToProto(absl::string_view proto_name) {
for (size_t i = 0; i <= PROTO_LAST; ++i) { for (size_t i = 0; i <= PROTO_LAST; ++i) {
if (absl::EqualsIgnoreCase(PROTO_NAMES[i], value)) { if (absl::EqualsIgnoreCase(PROTO_NAMES[i], proto_name)) {
*proto = static_cast<ProtocolType>(i); return static_cast<ProtocolType>(i);
return true;
} }
} }
return absl::nullopt;
}
bool StringToProto(const char* value, ProtocolType* proto) {
if (absl::optional<ProtocolType> type = StringToProto(value);
type.has_value()) {
*proto = *type;
return true;
}
return false; return false;
} }

View File

@ -18,6 +18,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "api/candidate.h" #include "api/candidate.h"
#include "api/field_trials_view.h" #include "api/field_trials_view.h"
@ -125,7 +126,10 @@ class CandidateStats {
typedef std::vector<CandidateStats> CandidateStatsList; typedef std::vector<CandidateStats> CandidateStatsList;
const char* ProtoToString(ProtocolType proto); const char* ProtoToString(ProtocolType proto);
bool StringToProto(const char* value, ProtocolType* proto); absl::optional<ProtocolType> StringToProto(absl::string_view proto_name);
// TODO(bugs.webrtc.org/13579): Delete once downstream usage is updated.
[[deprecated]] bool StringToProto(const char* value, ProtocolType* proto);
struct ProtocolAddress { struct ProtocolAddress {
rtc::SocketAddress address; rtc::SocketAddress address;

View File

@ -183,12 +183,15 @@ static RTCErrorType ParseIceServerUrl(
RTC_LOG(LS_WARNING) << "Transport parameter missing value."; RTC_LOG(LS_WARNING) << "Transport parameter missing value.";
return RTCErrorType::SYNTAX_ERROR; return RTCErrorType::SYNTAX_ERROR;
} }
if (!cricket::StringToProto(tokens[1].c_str(), &turn_transport_type) ||
(turn_transport_type != cricket::PROTO_UDP && absl::optional<cricket::ProtocolType> proto =
turn_transport_type != cricket::PROTO_TCP)) { cricket::StringToProto(tokens[1]);
if (!proto ||
(*proto != cricket::PROTO_UDP && *proto != cricket::PROTO_TCP)) {
RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp."; RTC_LOG(LS_WARNING) << "Transport parameter should always be udp or tcp.";
return RTCErrorType::SYNTAX_ERROR; return RTCErrorType::SYNTAX_ERROR;
} }
turn_transport_type = *proto;
} }
std::string hoststring; std::string hoststring;

View File

@ -1112,12 +1112,13 @@ bool ParseCandidate(absl::string_view message,
} }
SocketAddress address(connection_address, port); SocketAddress address(connection_address, port);
cricket::ProtocolType protocol; absl::optional<cricket::ProtocolType> protocol =
if (!StringToProto(transport.c_str(), &protocol)) { cricket::StringToProto(transport);
if (!protocol) {
return ParseFailed(first_line, "Unsupported transport type.", error); return ParseFailed(first_line, "Unsupported transport type.", error);
} }
bool tcp_protocol = false; bool tcp_protocol = false;
switch (protocol) { switch (*protocol) {
// Supported protocols. // Supported protocols.
case cricket::PROTO_UDP: case cricket::PROTO_UDP:
break; break;
@ -1225,7 +1226,7 @@ bool ParseCandidate(absl::string_view message,
} }
} }
*candidate = Candidate(component_id, cricket::ProtoToString(protocol), *candidate = Candidate(component_id, cricket::ProtoToString(*protocol),
address, priority, username, password, candidate_type, address, priority, username, password, candidate_type,
generation, foundation, network_id, network_cost); generation, foundation, network_id, network_cost);
candidate->set_related_address(related_address); candidate->set_related_address(related_address);