diff --git a/DataFormats/Headers/include/Headers/DataHeader.h b/DataFormats/Headers/include/Headers/DataHeader.h index dbcdb8e0bba89..d3c705868b411 100644 --- a/DataFormats/Headers/include/Headers/DataHeader.h +++ b/DataFormats/Headers/include/Headers/DataHeader.h @@ -258,27 +258,18 @@ struct Descriptor { /// /// Note: no assignment operator operator=(const char*) as this potentially runs /// into trouble with this general pointer type. - void runtimeInit(const char* string, short length = -1) + void runtimeInit(std::string_view string) { - char* target = str; - char* targetEnd = target; - if (length >= 0 && length < (int)N) { - targetEnd += length; - } else { - targetEnd += N; - } - const char* source = string; - for (; source != nullptr && target < targetEnd && *source != 0; ++target, ++source) { - *target = *source; + // empty strings and string longet than the descriptor size are not allowed + if (string.empty() || (string[0] != 0 && string.size() > (int)N)) { + throw std::invalid_argument("argument must not be empty or longer than the descriptor size"); } - targetEnd = str + N; - for (; target < targetEnd; ++target) { - *target = 0; - } - // require the string to be not longer than the descriptor size - if (source != nullptr && (*source == 0 || (length >= 0 && length <= (int)N))) { - } else { - throw std::invalid_argument("argument must not be longer than descriptor size"); + // copy the content directly + std::memcpy(str, string.data(), string.size()); + // nullify the remainder + auto* ptr = str + string.size(); + for (; ptr < str + N; ++ptr) { + *ptr = 0; } } diff --git a/DataFormats/Headers/test/testDataHeader.cxx b/DataFormats/Headers/test/testDataHeader.cxx index 2403c1a6230be..ba8a248983c2d 100644 --- a/DataFormats/Headers/test/testDataHeader.cxx +++ b/DataFormats/Headers/test/testDataHeader.cxx @@ -97,7 +97,7 @@ BOOST_AUTO_TEST_CASE(Descriptor_test) // check with runtime string std::string runtimeString = "RUNTIMES"; TestDescriptorT runtimeDescriptor; - runtimeDescriptor.runtimeInit(runtimeString.c_str()); + runtimeDescriptor.runtimeInit(runtimeString); BOOST_CHECK(runtimeDescriptor == TestDescriptorT("RUNTIMES")); BOOST_CHECK(testDescriptor.as().length() == 8); @@ -130,7 +130,7 @@ BOOST_AUTO_TEST_CASE(DataDescription_test) std::string runtimeString = "DATA_DESCRIPTION"; DataDescription runtimeDesc; - runtimeDesc.runtimeInit(runtimeString.c_str()); + runtimeDesc.runtimeInit(runtimeString); BOOST_CHECK(runtimeDesc == DataDescription("DATA_DESCRIPTION")); BOOST_CHECK(desc.as().length() == 6); diff --git a/Detectors/DCS/testWorkflow/src/DCSDataReplaySpec.cxx b/Detectors/DCS/testWorkflow/src/DCSDataReplaySpec.cxx index 8dc003fc176f3..48ceb22ae0d73 100644 --- a/Detectors/DCS/testWorkflow/src/DCSDataReplaySpec.cxx +++ b/Detectors/DCS/testWorkflow/src/DCSDataReplaySpec.cxx @@ -153,7 +153,7 @@ o2::framework::DataProcessorSpec getDCSDataReplaySpec(std::vector* dpid2Det = mgr.getForTimeStamp>(detStr + "/Config/DCSDPconfig", ts); for (auto& el : *dpid2Det) { o2::header::DataDescription tmpd; - tmpd.runtimeInit(el.second.c_str(), el.second.size()); + tmpd.runtimeInit(el.second); dpid2DataDesc[el.first].push_back(tmpd); } } diff --git a/Detectors/GlobalTrackingWorkflow/readers/src/irframe-reader-workflow.cxx b/Detectors/GlobalTrackingWorkflow/readers/src/irframe-reader-workflow.cxx index 5b93b40b902fd..c36e2af608a75 100644 --- a/Detectors/GlobalTrackingWorkflow/readers/src/irframe-reader-workflow.cxx +++ b/Detectors/GlobalTrackingWorkflow/readers/src/irframe-reader-workflow.cxx @@ -39,7 +39,7 @@ WorkflowSpec defineDataProcessing(ConfigContext const& cc) WorkflowSpec specs; o2::conf::ConfigurableParam::updateFromString(cc.options().get("configKeyValues")); o2::header::DataOrigin origin; - origin.runtimeInit(cc.options().get("data-origin").c_str()); + origin.runtimeInit(cc.options().get("data-origin")); specs.emplace_back(o2::globaltracking::getIRFrameReaderSpec(origin, (uint32_t)cc.options().get("subspec"), cc.options().get("device-name"), cc.options().get("file-name"))); diff --git a/Detectors/ITSMFT/ITS/workflow/src/DCSGeneratorSpec.cxx b/Detectors/ITSMFT/ITS/workflow/src/DCSGeneratorSpec.cxx index 2ea0c833f3871..df28c5eef8169 100644 --- a/Detectors/ITSMFT/ITS/workflow/src/DCSGeneratorSpec.cxx +++ b/Detectors/ITSMFT/ITS/workflow/src/DCSGeneratorSpec.cxx @@ -131,7 +131,7 @@ o2::framework::DataProcessorSpec getITSDCSDataGeneratorSpec(const char* detName) o2::header::DataDescription dd; - dd.runtimeInit(desc.c_str(), desc.size()); + dd.runtimeInit(desc); return DataProcessorSpec{ "its-dcs-data-generator", diff --git a/Detectors/ITSMFT/common/workflow/src/STFDecoderSpec.cxx b/Detectors/ITSMFT/common/workflow/src/STFDecoderSpec.cxx index 3cc88752d290c..0ceb98f502733 100644 --- a/Detectors/ITSMFT/common/workflow/src/STFDecoderSpec.cxx +++ b/Detectors/ITSMFT/common/workflow/src/STFDecoderSpec.cxx @@ -70,8 +70,8 @@ void STFDecoder::init(InitContext& ic) auto v2 = o2::utils::Str::tokenize(v1[1], '?'); header::DataOrigin dataOrig; header::DataDescription dataDesc; - dataOrig.runtimeInit(v1[0].c_str()); - dataDesc.runtimeInit(v2[0].c_str()); + dataOrig.runtimeInit(v1[0]); + dataDesc.runtimeInit(v2[0]); Mapping map; for (int iLayer{0}; iLayer < mLayers; ++iLayer) { auto& dec = mDecoder.emplace_back(std::make_unique>()); diff --git a/Detectors/TOF/workflow/src/CompressedDecodingTask.cxx b/Detectors/TOF/workflow/src/CompressedDecodingTask.cxx index fc0e2b9b2c30d..9cb3b229a5766 100644 --- a/Detectors/TOF/workflow/src/CompressedDecodingTask.cxx +++ b/Detectors/TOF/workflow/src/CompressedDecodingTask.cxx @@ -458,7 +458,7 @@ DataProcessorSpec getCompressedDecodingSpec(const std::string& inputDesc, bool c // inputs.emplace_back(std::string("x:TOF/" + inputDesc).c_str(), 0, Lifetime::Optional); o2::header::DataDescription dataDesc; - dataDesc.runtimeInit(inputDesc.c_str()); + dataDesc.runtimeInit(inputDesc); inputs.emplace_back("x", ConcreteDataTypeMatcher{o2::header::gDataOriginTOF, dataDesc}, Lifetime::Timeframe); std::vector outputs; outputs.emplace_back(o2::header::gDataOriginTOF, "DIGITHEADER", 0, Lifetime::Timeframe); diff --git a/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeCMVSpec.h b/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeCMVSpec.h index f3373070ab7bb..992ce917948dd 100644 --- a/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeCMVSpec.h +++ b/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeCMVSpec.h @@ -230,7 +230,7 @@ class TPCDistributeCMVSpec : public o2::framework::Task { const std::string name = fmt::format("CMVAGG{}", lane); header::DataDescription description; - description.runtimeInit(name.substr(0, 16).c_str()); + description.runtimeInit(name); return description; } @@ -239,7 +239,7 @@ class TPCDistributeCMVSpec : public o2::framework::Task { const std::string name = fmt::format("CMVORB{}", lane); header::DataDescription description; - description.runtimeInit(name.substr(0, 16).c_str()); + description.runtimeInit(name); return description; } diff --git a/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeIDCSpec.h b/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeIDCSpec.h index 6e589cd6c4e8b..9b2abfaaa9070 100644 --- a/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeIDCSpec.h +++ b/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeIDCSpec.h @@ -216,7 +216,7 @@ class TPCDistributeIDCSpec : public o2::framework::Task { const std::string name = fmt::format("IDCAGG{}", lane).data(); header::DataDescription description; - description.runtimeInit(name.substr(0, 16).c_str()); + description.runtimeInit(name); return description; } diff --git a/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeSACSpec.h b/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeSACSpec.h index ee05a58b5dcbe..108f48d9be814 100644 --- a/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeSACSpec.h +++ b/Detectors/TPC/workflow/include/TPCWorkflow/TPCDistributeSACSpec.h @@ -86,7 +86,7 @@ class TPCDistributeSACSpec : public o2::framework::Task { const std::string name = fmt::format("SACVEC{}", lane).data(); header::DataDescription description; - description.runtimeInit(name.substr(0, 16).c_str()); + description.runtimeInit(name); return description; } diff --git a/Framework/Core/src/WorkflowSerializationHelpers.cxx b/Framework/Core/src/WorkflowSerializationHelpers.cxx index b824e8d0bb424..16eb6b067500a 100644 --- a/Framework/Core/src/WorkflowSerializationHelpers.cxx +++ b/Framework/Core/src/WorkflowSerializationHelpers.cxx @@ -737,11 +737,11 @@ struct WorkflowImporter : public rapidjson::BaseReaderHandler, } else if (in(State::IN_INPUT_BINDING)) { binding = s; } else if (in(State::IN_INPUT_ORIGIN)) { - origin.runtimeInit(s.c_str(), std::min(s.size(), 4UL)); + origin.runtimeInit(std::string_view{s.c_str(), std::min(s.size(), 4UL)}); std::string v(s.c_str(), std::min(s.size(), 4UL)); inputMatcherNodes.push_back(OriginValueMatcher{v}); } else if (in(State::IN_INPUT_DESCRIPTION)) { - description.runtimeInit(s.c_str(), std::min(s.size(), 16UL)); + description.runtimeInit(std::string_view{s.c_str(), std::min(s.size(), 16UL)}); std::string v(s.c_str(), std::min(s.size(), 16UL)); inputMatcherNodes.push_back(DescriptionValueMatcher{v}); } else if (in(State::IN_INPUT_STARTTIME)) { @@ -771,9 +771,9 @@ struct WorkflowImporter : public rapidjson::BaseReaderHandler, } else if (in(State::IN_OUTPUT_BINDING)) { binding = s; } else if (in(State::IN_OUTPUT_ORIGIN)) { - origin.runtimeInit(s.c_str(), std::min(s.size(), 4UL)); + origin.runtimeInit(std::string_view{s.c_str(), std::min(s.size(), 4UL)}); } else if (in(State::IN_OUTPUT_DESCRIPTION)) { - description.runtimeInit(s.c_str(), std::min(s.size(), 16UL)); + description.runtimeInit(std::string_view{s.c_str(), std::min(s.size(), 16UL)}); } else if (in(State::IN_OPTION_NAME)) { optionName = s; } else if (in(State::IN_OPTION_TYPE)) { diff --git a/Utilities/DataSampling/src/DataSamplingPolicy.cxx b/Utilities/DataSampling/src/DataSamplingPolicy.cxx index 2c63ba7993f98..435a21a16e62b 100644 --- a/Utilities/DataSampling/src/DataSamplingPolicy.cxx +++ b/Utilities/DataSampling/src/DataSamplingPolicy.cxx @@ -191,7 +191,7 @@ header::DataDescription DataSamplingPolicy::createPolicyDataDescription(std::str } header::DataDescription outputDescription; - outputDescription.runtimeInit((policyName + std::to_string(id)).c_str()); + outputDescription.runtimeInit((policyName + std::to_string(id))); return outputDescription; } diff --git a/Utilities/DataSampling/src/Dispatcher.cxx b/Utilities/DataSampling/src/Dispatcher.cxx index 22dd457a1211a..36e8e1408aced 100644 --- a/Utilities/DataSampling/src/Dispatcher.cxx +++ b/Utilities/DataSampling/src/Dispatcher.cxx @@ -75,7 +75,7 @@ void Dispatcher::init(InitContext& ctx) } auto& spec = ctx.services().get(); - mDeviceID.runtimeInit(spec.id.substr(0, DataSamplingHeader::deviceIDTypeSize).c_str()); + mDeviceID.runtimeInit(std::string_view{spec.id.c_str(), DataSamplingHeader::deviceIDTypeSize}); } header::Stack extractAdditionalHeaders(const char* inputHeaderStack) @@ -236,7 +236,7 @@ Inputs Dispatcher::getInputSpecs() // add timer input header::DataDescription timerDescription; - timerDescription.runtimeInit(("TIMER-" + mName).substr(0, 16).c_str()); + timerDescription.runtimeInit(std::string_view{("TIMER-" + mName).c_str(), 16}); declaredInputs.emplace_back(InputSpec{"timer-stats", "DS", timerDescription, 0, Lifetime::Timer}); return declaredInputs; diff --git a/Utilities/Mergers/include/Mergers/MergerBuilder.h b/Utilities/Mergers/include/Mergers/MergerBuilder.h index bcaaf08d7bf14..918c50d1e1db7 100644 --- a/Utilities/Mergers/include/Mergers/MergerBuilder.h +++ b/Utilities/Mergers/include/Mergers/MergerBuilder.h @@ -67,7 +67,7 @@ class MergerBuilder static inline header::DataDescription mergerDataDescription(std::string name) { header::DataDescription description; - description.runtimeInit(name.substr(0, 16).c_str()); + description.runtimeInit(std::string_view{name.c_str(), 16}); return description; }; static inline header::DataHeader::SubSpecificationType mergerSubSpec(size_t layer, size_t id)