utils: cleanup StartsWith

This commit is contained in:
Peter Keresztes Schmidt 2021-04-03 23:51:12 +02:00
parent 3dbf13e00b
commit 39a896f5b6
4 changed files with 16 additions and 17 deletions

View File

@ -487,28 +487,28 @@ void RtspThread::Run() {
for ( size_t i = 0; i < parts.size(); i++ ) { for ( size_t i = 0; i < parts.size(); i++ ) {
if ( parts[i] == "unicast" || parts[i] == "multicast" ) if ( parts[i] == "unicast" || parts[i] == "multicast" )
distribution = parts[i]; distribution = parts[i];
else if ( startsWith( parts[i], "server_port=" ) ) { else if (StartsWith(parts[i], "server_port=") ) {
method = "RTP/UNICAST"; method = "RTP/UNICAST";
StringVector subparts = split( parts[i], "=" ); StringVector subparts = split( parts[i], "=" );
StringVector ports = split( subparts[1], "-" ); StringVector ports = split( subparts[1], "-" );
remotePorts[0] = strtol( ports[0].c_str(), nullptr, 10 ); remotePorts[0] = strtol( ports[0].c_str(), nullptr, 10 );
remotePorts[1] = strtol( ports[1].c_str(), nullptr, 10 ); remotePorts[1] = strtol( ports[1].c_str(), nullptr, 10 );
} else if ( startsWith( parts[i], "interleaved=" ) ) { } else if (StartsWith(parts[i], "interleaved=") ) {
method = "RTP/RTSP"; method = "RTP/RTSP";
StringVector subparts = split( parts[i], "=" ); StringVector subparts = split( parts[i], "=" );
StringVector channels = split( subparts[1], "-" ); StringVector channels = split( subparts[1], "-" );
remoteChannels[0] = strtol( channels[0].c_str(), nullptr, 10 ); remoteChannels[0] = strtol( channels[0].c_str(), nullptr, 10 );
remoteChannels[1] = strtol( channels[1].c_str(), nullptr, 10 ); remoteChannels[1] = strtol( channels[1].c_str(), nullptr, 10 );
} else if ( startsWith( parts[i], "port=" ) ) { } else if (StartsWith(parts[i], "port=") ) {
method = "RTP/MULTICAST"; method = "RTP/MULTICAST";
StringVector subparts = split( parts[i], "=" ); StringVector subparts = split( parts[i], "=" );
StringVector ports = split( subparts[1], "-" ); StringVector ports = split( subparts[1], "-" );
localPorts[0] = strtol( ports[0].c_str(), nullptr, 10 ); localPorts[0] = strtol( ports[0].c_str(), nullptr, 10 );
localPorts[1] = strtol( ports[1].c_str(), nullptr, 10 ); localPorts[1] = strtol( ports[1].c_str(), nullptr, 10 );
} else if ( startsWith( parts[i], "destination=" ) ) { } else if (StartsWith(parts[i], "destination=") ) {
StringVector subparts = split( parts[i], "=" ); StringVector subparts = split( parts[i], "=" );
localHost = subparts[1]; localHost = subparts[1];
} else if ( startsWith( parts[i], "ssrc=" ) ) { } else if (StartsWith(parts[i], "ssrc=") ) {
StringVector subparts = split( parts[i], "=" ); StringVector subparts = split( parts[i], "=" );
ssrc = strtoll( subparts[1].c_str(), nullptr, 16 ); ssrc = strtoll( subparts[1].c_str(), nullptr, 16 );
} }
@ -558,10 +558,10 @@ void RtspThread::Run() {
// Parse the sequence and rtptime values // Parse the sequence and rtptime values
parts = split( streams[i].c_str(), ";" ); parts = split( streams[i].c_str(), ";" );
for ( size_t j = 0; j < parts.size(); j++ ) { for ( size_t j = 0; j < parts.size(); j++ ) {
if ( startsWith( parts[j], "seq=" ) ) { if (StartsWith(parts[j], "seq=") ) {
StringVector subparts = split( parts[j], "=" ); StringVector subparts = split( parts[j], "=" );
seq = strtol( subparts[1].c_str(), nullptr, 10 ); seq = strtol( subparts[1].c_str(), nullptr, 10 );
} else if ( startsWith( parts[j], "rtptime=" ) ) { } else if (StartsWith(parts[j], "rtptime=") ) {
StringVector subparts = split( parts[j], "=" ); StringVector subparts = split( parts[j], "=" );
rtpTime = strtol( subparts[1].c_str(), nullptr, 10 ); rtpTime = strtol( subparts[1].c_str(), nullptr, 10 );
} }

View File

@ -61,10 +61,6 @@ std::string ReplaceAll(std::string str, const std::string &old_value, const std:
return str; return str;
} }
bool startsWith(const std::string &haystack, const std::string &needle) {
return ( haystack.substr(0, needle.length()) == needle );
}
std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems; std::vector<std::string> elems;
std::stringstream ss(s); std::stringstream ss(s);

View File

@ -35,6 +35,10 @@ std::string Trim(const std::string &str, const std::string &char_set);
inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); } inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); }
std::string ReplaceAll(std::string str, const std::string& old_value, const std::string& new_value); std::string ReplaceAll(std::string str, const std::string& old_value, const std::string& new_value);
inline bool StartsWith(const std::string &haystack, const std::string &needle) {
return (haystack.substr(0, needle.length()) == needle);
}
template<typename... Args> template<typename... Args>
std::string stringtf(const std::string &format, Args... args) { std::string stringtf(const std::string &format, Args... args) {
int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0' int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
@ -46,7 +50,6 @@ std::string stringtf(const std::string &format, Args... args) {
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
} }
bool startsWith( const std::string &haystack, const std::string &needle );
StringVector split( const std::string &string, const std::string &chars, int limit=0 ); StringVector split( const std::string &string, const std::string &chars, int limit=0 );
const std::string join( const StringVector &, const char * ); const std::string join( const StringVector &, const char * );

View File

@ -65,12 +65,12 @@ TEST_CASE("ReplaceAll") {
REQUIRE(ReplaceAll("aTOKENaTOKEN", "TOKEN", "VAL") == "aVALaVAL"); REQUIRE(ReplaceAll("aTOKENaTOKEN", "TOKEN", "VAL") == "aVALaVAL");
} }
TEST_CASE("startsWith") { TEST_CASE("StartsWith") {
REQUIRE(startsWith("", "") == true); REQUIRE(StartsWith("", "") == true);
REQUIRE(startsWith("test", "test") == true); REQUIRE(StartsWith("test", "test") == true);
REQUIRE(startsWith("test=abc", "test") == true); REQUIRE(StartsWith("test=abc", "test") == true);
REQUIRE(startsWith(" test=abc", "test") == false); REQUIRE(StartsWith(" test=abc", "test") == false);
} }
TEST_CASE("split (char delimiter)") { TEST_CASE("split (char delimiter)") {