From f1a1dfb26e8572dd22abca99700622ceea9e6ef1 Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Thu, 27 Aug 2015 18:06:20 +0200 Subject: [PATCH] Update use of String::Trim() fixes #9704 --- lib/base/application.cpp | 3 +-- lib/cli/nodeutility.cpp | 9 +++----- lib/cli/nodewizardcommand.cpp | 28 ++++++++++++------------- lib/icinga/legacytimeperiod.cpp | 9 +++----- lib/livestatus/livestatuslogutility.cpp | 7 ++----- lib/livestatus/livestatusquery.cpp | 4 +--- lib/methods/pluginchecktask.cpp | 4 ++-- lib/remote/configmoduleutility.cpp | 3 +-- lib/remote/httprequest.cpp | 8 +++---- test/base-string.cpp | 12 ++++------- 10 files changed, 34 insertions(+), 53 deletions(-) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index 1363410b0..ec62ad083 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -502,9 +502,8 @@ static String UnameHelper(char type) pclose(fp); String result = msgbuf.str(); - result.Trim(); - return result; + return result.Trim(); } int ReleaseHelper(std::string &result) diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index 1cf3ce087..b526bf717 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -271,23 +271,20 @@ int NodeUtility::GenerateNodeIcingaConfig(const std::vector& endpoi Dictionary::Ptr my_master_endpoint = new Dictionary(); if (tokens.size() > 1) { - String host = tokens[1]; - host.Trim(); + String host = tokens[1].Trim(); if (!host.IsEmpty()) my_master_endpoint->Set("host", host); } if (tokens.size() > 2) { - String port = tokens[2]; - port.Trim(); + String port = tokens[2].Trim(); if (!port.IsEmpty()) my_master_endpoint->Set("port", port); } - String cn = tokens[0]; - cn.Trim(); + String cn = tokens[0].Trim(); my_master_endpoint->Set("__name", cn); my_master_endpoint->Set("__type", "Endpoint"); diff --git a/lib/cli/nodewizardcommand.cpp b/lib/cli/nodewizardcommand.cpp index 917d88576..1a523b0cb 100644 --- a/lib/cli/nodewizardcommand.cpp +++ b/lib/cli/nodewizardcommand.cpp @@ -126,7 +126,7 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm, cons answer = Utility::GetFQDN(); String cn = answer; - cn.Trim(); + cn = cn.Trim(); std::cout << ConsoleColorTag(Console_Bold) << "Please specifiy the local zone name" << ConsoleColorTag(Console_Normal) << " [" << cn << "]: "; @@ -137,7 +137,7 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm, cons answer = cn; String local_zone = answer; - local_zone.Trim(); + local_zone = local_zone.Trim(); std::vector endpoints; @@ -159,7 +159,7 @@ wizard_endpoint_loop_start: } endpoint_buffer = answer; - endpoint_buffer.Trim(); + endpoint_buffer = endpoint_buffer.Trim(); std::cout << "Do you want to establish a connection to the master " << ConsoleColorTag(Console_Bold) << "from this node?" << ConsoleColorTag(Console_Normal) << " [Y/n]: "; @@ -183,7 +183,8 @@ wizard_endpoint_loop_start: } String tmp = answer; - tmp.Trim(); + tmp = tmp.Trim(); + endpoint_buffer += "," + tmp; master_endpoint_name = tmp; //store the endpoint name for later @@ -197,8 +198,7 @@ wizard_endpoint_loop_start: else tmp = "5665"; - tmp.Trim(); - endpoint_buffer += "," + tmp; + endpoint_buffer += "," + tmp.Trim(); } endpoints.push_back(endpoint_buffer); @@ -228,7 +228,7 @@ wizard_master_host: goto wizard_master_host; String master_host = answer; - master_host.Trim(); + master_host = master_host.Trim(); std::cout << ConsoleColorTag(Console_Bold) << "Port" << ConsoleColorTag(Console_Normal) << " [5665]: "; @@ -239,7 +239,7 @@ wizard_master_host: answer = "5665"; String master_port = answer; - master_port.Trim(); + master_port = master_port.Trim(); /* workaround for fetching the master cert */ String pki_path = PkiUtility::GetPkiPath(); @@ -306,7 +306,7 @@ wizard_ticket: goto wizard_ticket; String ticket = answer; - ticket.Trim(); + ticket = ticket.Trim(); Log(LogInformation, "cli") << "Processing self-signed certificate request. Ticket '" << ticket << "'.\n"; @@ -339,7 +339,7 @@ wizard_ticket: boost::algorithm::to_lower(answer); String bind_host = answer; - bind_host.Trim(); + bind_host = bind_host.Trim(); std::cout << "Bind Port []: "; @@ -347,7 +347,7 @@ wizard_ticket: boost::algorithm::to_lower(answer); String bind_port = answer; - bind_port.Trim(); + bind_port = bind_port.Trim(); std::cout << ConsoleColorTag(Console_Bold) << "Accept config from master?" << ConsoleColorTag(Console_Normal) << " [y/N]: "; std::getline(std::cin, answer); @@ -449,7 +449,7 @@ wizard_ticket: answer = Utility::GetFQDN(); String cn = answer; - cn.Trim(); + cn = cn.Trim(); /* check whether the user wants to generate a new certificate or not */ String existing_path = PkiUtility::GetPkiPath() + "/" + cn + ".crt"; @@ -481,7 +481,7 @@ wizard_ticket: boost::algorithm::to_lower(answer); String bind_host = answer; - bind_host.Trim(); + bind_host = bind_host.Trim(); std::cout << ConsoleColorTag(Console_Bold) << "Bind Port" << ConsoleColorTag(Console_Normal) << " []: "; @@ -489,7 +489,7 @@ wizard_ticket: boost::algorithm::to_lower(answer); String bind_port = answer; - bind_port.Trim(); + bind_port = bind_port.Trim(); /* api feature is always enabled, check above */ String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf"; diff --git a/lib/icinga/legacytimeperiod.cpp b/lib/icinga/legacytimeperiod.cpp index 5c7d64b5f..aadcd7033 100644 --- a/lib/icinga/legacytimeperiod.cpp +++ b/lib/icinga/legacytimeperiod.cpp @@ -269,8 +269,7 @@ void LegacyTimePeriod::ParseTimeRange(const String& timerange, tm *begin, tm *en size_t pos = def.FindFirstOf('/'); if (pos != String::NPos) { - String strStride = def.SubStr(pos + 1); - strStride.Trim(); + String strStride = def.SubStr(pos + 1).Trim(); *stride = Convert::ToLong(strStride); /* Remove the stride parameter from the definition. */ @@ -283,11 +282,9 @@ void LegacyTimePeriod::ParseTimeRange(const String& timerange, tm *begin, tm *en pos = def.Find("- "); if (pos != String::NPos) { - String first = def.SubStr(0, pos); - first.Trim(); + String first = def.SubStr(0, pos).Trim(); - String second = def.SubStr(pos + 1); - second.Trim(); + String second = def.SubStr(pos + 1).Trim(); ParseTimeSpec(first, begin, NULL, reference); diff --git a/lib/livestatus/livestatuslogutility.cpp b/lib/livestatus/livestatuslogutility.cpp index de70a2be7..f41a4404c 100644 --- a/lib/livestatus/livestatuslogutility.cpp +++ b/lib/livestatus/livestatuslogutility.cpp @@ -136,11 +136,8 @@ Dictionary::Ptr LivestatusLogUtility::GetAttributes(const String& text) size_t colon = text.FindFirstOf(':'); size_t colon_offset = colon - 13; - String type = String(text.SubStr(13, colon_offset)); - String options = String(text.SubStr(colon + 1)); - - type.Trim(); - options.Trim(); + String type = String(text.SubStr(13, colon_offset)).Trim(); + String options = String(text.SubStr(colon + 1)).Trim(); bag->Set("type", type); bag->Set("options", options); diff --git a/lib/livestatus/livestatusquery.cpp b/lib/livestatus/livestatusquery.cpp index 863e18f93..804261f1d 100644 --- a/lib/livestatus/livestatusquery.cpp +++ b/lib/livestatus/livestatusquery.cpp @@ -154,9 +154,7 @@ LivestatusQuery::LivestatusQuery(const std::vector& lines, const String& //OutputFormat:json or OutputFormat: json if (line.GetLength() > col_index + 1) - params = line.SubStr(col_index + 1); - - params.Trim(); + params = line.SubStr(col_index + 1).Trim(); if (header == "ResponseHeader") m_ResponseHeader = params; diff --git a/lib/methods/pluginchecktask.cpp b/lib/methods/pluginchecktask.cpp index ed39ed6c9..b63ff9ca3 100644 --- a/lib/methods/pluginchecktask.cpp +++ b/lib/methods/pluginchecktask.cpp @@ -67,8 +67,8 @@ void PluginCheckTask::ProcessFinishedHandler(const Checkable::Ptr& checkable, co << pr.ExitStatus << ", output: " << pr.Output; } - String output = pr.Output; - output.Trim(); + String output = pr.Output.Trim(); + std::pair co = PluginUtility::ParseCheckOutput(output); cr->SetCommand(commandLine); cr->SetOutput(co.first); diff --git a/lib/remote/configmoduleutility.cpp b/lib/remote/configmoduleutility.cpp index 15b157128..964519ba0 100644 --- a/lib/remote/configmoduleutility.cpp +++ b/lib/remote/configmoduleutility.cpp @@ -243,14 +243,13 @@ String ConfigModuleUtility::GetActiveStage(const String& moduleName) String stage; std::getline(fp, stage.GetData()); - stage.Trim(); fp.close(); if (fp.fail()) return ""; - return stage; + return stage.Trim(); } diff --git a/lib/remote/httprequest.cpp b/lib/remote/httprequest.cpp index 49bee4af5..951169e9d 100644 --- a/lib/remote/httprequest.cpp +++ b/lib/remote/httprequest.cpp @@ -82,11 +82,9 @@ bool HttpRequest::Parse(const Stream::Ptr& stream, StreamReadContext& src, bool String::SizeType pos = line.FindFirstOf(":"); if (pos == String::NPos) BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid HTTP request")); - String key = line.SubStr(0, pos); - boost::algorithm::to_lower(key); - key.Trim(); - String value = line.SubStr(pos + 1); - value.Trim(); + String key = line.SubStr(0, pos).ToLower().Trim(); + + String value = line.SubStr(pos + 1).Trim(); Headers->Set(key, value); if (key == "x-http-method-override") diff --git a/test/base-string.cpp b/test/base-string.cpp index ea70326c5..2c87c21f8 100644 --- a/test/base-string.cpp +++ b/test/base-string.cpp @@ -63,20 +63,16 @@ BOOST_AUTO_TEST_CASE(append) BOOST_AUTO_TEST_CASE(trim) { String s1 = "hello"; - s1.Trim(); - BOOST_CHECK(s1 == "hello"); + BOOST_CHECK(s1.Trim() == "hello"); String s2 = " hello"; - s2.Trim(); - BOOST_CHECK(s2 == "hello"); + BOOST_CHECK(s2.Trim() == "hello"); String s3 = "hello "; - s3.Trim(); - BOOST_CHECK(s3 == "hello"); + BOOST_CHECK(s3.Trim() == "hello"); String s4 = " hello "; - s4.Trim(); - BOOST_CHECK(s4 == "hello"); + BOOST_CHECK(s4.Trim() == "hello"); } BOOST_AUTO_TEST_CASE(contains)