#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_SUPPORT_SOURCE 1 #include "Nuclex/Support/Config.h" #include "Nuclex/Support/Text/StringHelper.h" #include namespace { // ------------------------------------------------------------------------------------------- // /// Simple substring removal method using standard C++ primitives /// String from which substrings will be removed /// Substring of which all occurrences will be removed void removeAllOccurrenceNaive(std::string &master, const std::string &substringToRemove) { for(;;) { std::string::size_type index = master.find(substringToRemove); if(index == std::string::npos) { return; // All occurrences removed } master.erase(index, substringToRemove.length()); } } // ------------------------------------------------------------------------------------------- // /// Tests the removal of substrings via standard C++ primitives /// Master string with which the tests will be performed /// Substring to test the removal with /// /// A value dependent on the operation that can be used to prevent the optimizer /// from optimizing the entire method call away /// bool testNaiveRemoval(const std::string &master, const std::string &substringToRemove) { std::string masterCopy = master; removeAllOccurrenceNaive(masterCopy, substringToRemove); return masterCopy.empty(); } // ------------------------------------------------------------------------------------------- // /// Tests the removal of substrings via the custom StringHelper method /// Master string with which the tests will be performed /// Substring to test the removal with /// /// A value dependent on the operation that can be used to prevent the optimizer /// from optimizing the entire method call away /// bool testStringHelperRemoval(const std::string &master, const std::string &substringToRemove) { std::string masterCopy = master; Nuclex::Support::Text::StringHelper::EraseSubstrings(masterCopy, substringToRemove); return masterCopy.empty(); } // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Support { namespace Text { // ------------------------------------------------------------------------------------------- // BASELINE(SubstringRemoval, ViaCxxMethods, 1000, 0) { std::string master( u8"This is a longer string which may or may not have " u8"been spoken by a trained bovine.", 110 ); celero::DoNotOptimizeAway( testNaiveRemoval(master, u8" ") ); } // ------------------------------------------------------------------------------------------- // BENCHMARK(SubstringRemoval, ViaStringHelper, 1000, 0) { std::string master( u8"This is a longer string which may or may not have " u8"been spoken by a trained bovine.", 110 ); celero::DoNotOptimizeAway( testStringHelperRemoval(master, u8" ") ); } // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Text