Skip to content

Commit

Permalink
Restructure string_utils into a library. (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkrodrigues authored Dec 31, 2023
1 parent 4124824 commit abda9a3
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 98 deletions.
6 changes: 4 additions & 2 deletions components/core/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ FixNamespaceComments: true
IncludeBlocks: "Regroup"
IncludeCategories:
# NOTE: A header is grouped by first matching regex
# Third-party headers. Update when adding new third-party libraries.
- Regex: "^<(archive|boost|catch2|date|fmt|json|log_surgeon|mariadb|spdlog|sqlite3|yaml-cpp|zstd)"
# Library headers. Update when adding new libraries.
# NOTE: clang-format retains leading white-space on a line in violation of the YAML spec.
- Regex: "^<(archive|boost|catch2|date|fmt|json|log_surgeon|mariadb|spdlog|sqlite3|string_utils\
|yaml-cpp|zstd)"
Priority: 3
# C system headers
- Regex: "^<.+\\.h>"
Expand Down
15 changes: 6 additions & 9 deletions components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ set(SOURCE_FILES_clp
src/streaming_compression/zstd/Constants.hpp
src/streaming_compression/zstd/Decompressor.cpp
src/streaming_compression/zstd/Decompressor.hpp
src/string_utils.cpp
src/string_utils.hpp
src/StringReader.cpp
src/StringReader.hpp
src/TimestampPattern.cpp
Expand Down Expand Up @@ -338,6 +336,7 @@ target_link_libraries(clp
LibArchive::LibArchive
MariaDBClient::MariaDBClient
${STD_FS_LIBS}
string_utils::string_utils
yaml-cpp::yaml-cpp
ZStd::ZStd
)
Expand Down Expand Up @@ -449,8 +448,6 @@ set(SOURCE_FILES_clg
src/streaming_compression/zstd/Constants.hpp
src/streaming_compression/zstd/Decompressor.cpp
src/streaming_compression/zstd/Decompressor.hpp
src/string_utils.cpp
src/string_utils.hpp
src/StringReader.cpp
src/StringReader.hpp
src/TimestampPattern.cpp
Expand Down Expand Up @@ -486,6 +483,7 @@ target_link_libraries(clg
spdlog::spdlog
${sqlite_LIBRARY_DEPENDENCIES}
${STD_FS_LIBS}
string_utils::string_utils
yaml-cpp::yaml-cpp
ZStd::ZStd
)
Expand Down Expand Up @@ -583,8 +581,6 @@ set(SOURCE_FILES_clo
src/streaming_compression/zstd/Constants.hpp
src/streaming_compression/zstd/Decompressor.cpp
src/streaming_compression/zstd/Decompressor.hpp
src/string_utils.cpp
src/string_utils.hpp
src/StringReader.cpp
src/StringReader.hpp
src/Thread.cpp
Expand Down Expand Up @@ -621,6 +617,7 @@ target_link_libraries(clo
spdlog::spdlog
${sqlite_LIBRARY_DEPENDENCIES}
${STD_FS_LIBS}
string_utils::string_utils
ZStd::ZStd
)
target_compile_features(clo
Expand Down Expand Up @@ -774,9 +771,6 @@ set(SOURCE_FILES_unitTest
src/streaming_compression/zstd/Constants.hpp
src/streaming_compression/zstd/Decompressor.cpp
src/streaming_compression/zstd/Decompressor.hpp
src/string_utils.cpp
src/string_utils.hpp
src/string_utils.inc
src/StringReader.cpp
src/StringReader.hpp
src/TimestampPattern.cpp
Expand Down Expand Up @@ -828,6 +822,7 @@ target_link_libraries(unitTest
spdlog::spdlog
${sqlite_LIBRARY_DEPENDENCIES}
${STD_FS_LIBS}
string_utils::string_utils
yaml-cpp::yaml-cpp
ZStd::ZStd
)
Expand All @@ -836,3 +831,5 @@ target_compile_features(unitTest
)

include(cmake/utils.cmake)

add_subdirectory(src)
3 changes: 1 addition & 2 deletions components/core/cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ set(SOURCE_FILES_make-dictionaries-readable
${CMAKE_CURRENT_SOURCE_DIR}/src/streaming_compression/passthrough/Decompressor.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/streaming_compression/zstd/Decompressor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/streaming_compression/zstd/Decompressor.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/string_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/string_utils.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Utils.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/utils/make_dictionaries_readable/CommandLineArguments.cpp
Expand All @@ -46,6 +44,7 @@ target_link_libraries(make-dictionaries-readable
Boost::filesystem Boost::iostreams Boost::program_options
log_surgeon::log_surgeon
spdlog::spdlog
string_utils::string_utils
ZStd::ZStd
)
target_compile_features(make-dictionaries-readable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef STRING_UTILS_HPP
#define STRING_UTILS_HPP

#include <charconv>
#include <string>

namespace string_utils {
/**
* Checks if the given character is an alphabet
* @param c
Expand Down Expand Up @@ -122,6 +124,16 @@ bool wildcard_match_unsafe_case_sensitive(std::string_view tame, std::string_vie
template <typename integer_t>
bool convert_string_to_int(std::string_view raw, integer_t& converted);

#include "string_utils.inc"
template <typename integer_t>
bool convert_string_to_int(std::string_view raw, integer_t& converted) {
auto raw_end = raw.cend();
auto result = std::from_chars(raw.cbegin(), raw_end, converted);
if (raw_end != result.ptr) {
return false;
} else {
return result.ec == std::errc();
}
}
} // namespace string_utils

#endif // STRING_UTILS_HPP
12 changes: 12 additions & 0 deletions components/core/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(
STRING_UTILS_HEADER_LIST
"${PROJECT_SOURCE_DIR}/include/string_utils/string_utils.hpp"
)
add_library(
string_utils
string_utils/string_utils.cpp
${STRING_UTILS_HEADER_LIST}
)
add_library(string_utils::string_utils ALIAS string_utils)
target_include_directories(string_utils PUBLIC "${PROJECT_SOURCE_DIR}/include")
target_compile_features(string_utils PRIVATE cxx_std_17)
9 changes: 7 additions & 2 deletions components/core/src/DictionaryReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <vector>

#include <boost/algorithm/string.hpp>
#include <string_utils/string_utils.hpp>

#include "dictionary_utils.hpp"
#include "DictionaryEntry.hpp"
#include "FileReader.hpp"
#include "streaming_compression/passthrough/Decompressor.hpp"
#include "streaming_compression/zstd/Decompressor.hpp"
#include "string_utils.hpp"
#include "Utils.hpp"

/**
Expand Down Expand Up @@ -256,7 +256,12 @@ void DictionaryReader<DictionaryIdType, EntryType>::get_entries_matching_wildcar
std::unordered_set<EntryType const*>& entries
) const {
for (auto const& entry : m_entries) {
if (wildcard_match_unsafe(entry.get_value(), wildcard_string, false == ignore_case)) {
if (string_utils::wildcard_match_unsafe(
entry.get_value(),
wildcard_string,
false == ignore_case
))
{
entries.insert(&entry);
}
}
Expand Down
5 changes: 3 additions & 2 deletions components/core/src/EncodedVariableInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include <cassert>
#include <cmath>

#include <string_utils/string_utils.hpp>

#include "Defs.h"
#include "ffi/ir_stream/decoding_methods.hpp"
#include "ir/LogEvent.hpp"
#include "ir/types.hpp"
#include "spdlog_with_specializations.hpp"
#include "string_utils.hpp"
#include "type_utils.hpp"

using ffi::cEightByteEncodedFloatDigitsBitMask;
Expand Down Expand Up @@ -55,7 +56,7 @@ bool EncodedVariableInterpreter::convert_string_to_representable_integer_var(
}

int64_t result;
if (false == convert_string_to_int(value, result)) {
if (false == string_utils::convert_string_to_int(value, result)) {
// Conversion failed
return false;
} else {
Expand Down
7 changes: 6 additions & 1 deletion components/core/src/Grep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>

#include <log_surgeon/Constants.hpp>
#include <string_utils/string_utils.hpp>

#include "EncodedVariableInterpreter.hpp"
#include "ir/parsing.hpp"
Expand All @@ -17,6 +18,10 @@ using std::vector;
using streaming_archive::reader::Archive;
using streaming_archive::reader::File;
using streaming_archive::reader::Message;
using string_utils::clean_up_wildcard_search_string;
using string_utils::is_alphabet;
using string_utils::is_wildcard;
using string_utils::wildcard_match_unsafe;

// Local types
enum class SubQueryMatchabilityResult {
Expand Down Expand Up @@ -693,7 +698,7 @@ bool Grep::get_bounds_of_next_potential_var(
}
}

if (is_decimal_digit(c)) {
if (string_utils::is_decimal_digit(c)) {
contains_decimal_digit = true;
} else if (is_alphabet(c)) {
contains_alphabet = true;
Expand Down
2 changes: 1 addition & 1 deletion components/core/src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include <boost/lexical_cast.hpp>
#include <log_surgeon/SchemaParser.hpp>
#include <spdlog/spdlog.h>
#include <string_utils/string_utils.hpp>

#include "spdlog_with_specializations.hpp"
#include "string_utils.hpp"

using std::list;
using std::string;
Expand Down
21 changes: 15 additions & 6 deletions components/core/src/ffi/encoding_methods.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#include <algorithm>

#include <string_utils/string_utils.hpp>

#include "../ir/parsing.hpp"
#include "../ir/types.hpp"
#include "../string_utils.hpp"
#include "../type_utils.hpp"

namespace ffi {
Expand Down Expand Up @@ -326,7 +327,7 @@ bool encode_integer_string(std::string_view str, encoded_variable_t& encoded_var
}
encoded_variable_t result;
if (false == convert_string_to_int(str, result)) {
if (false == string_utils::convert_string_to_int(str, result)) {
// Conversion failed
return false;
} else {
Expand Down Expand Up @@ -519,7 +520,7 @@ bool wildcard_query_matches_any_encoded_var(
if constexpr (ir::VariablePlaceholder::Float == var_placeholder) {
auto decoded_var = decode_float_var(encoded_vars[encoded_vars_ix]);
if (wildcard_match_unsafe(decoded_var, wildcard_query)) {
if (string_utils::wildcard_match_unsafe(decoded_var, wildcard_query)) {
return true;
}
}
Expand All @@ -537,7 +538,7 @@ bool wildcard_query_matches_any_encoded_var(
if constexpr (ir::VariablePlaceholder::Integer == var_placeholder) {
auto decoded_var = decode_integer_var(encoded_vars[encoded_vars_ix]);
if (wildcard_match_unsafe(decoded_var, wildcard_query)) {
if (string_utils::wildcard_match_unsafe(decoded_var, wildcard_query)) {
return true;
}
}
Expand Down Expand Up @@ -591,7 +592,11 @@ bool wildcard_match_encoded_vars(
if (wildcard_var_placeholders[wildcard_var_ix] == c) {
auto decoded_var = decode_float_var(encoded_vars[var_ix]);
if (wildcard_match_unsafe(decoded_var, wildcard_var_queries[wildcard_var_ix])) {
if (string_utils::wildcard_match_unsafe(
decoded_var,
wildcard_var_queries[wildcard_var_ix]
))
{
++wildcard_var_ix;
if (wildcard_var_ix == wildcard_var_queries_len) {
break;
Expand All @@ -612,7 +617,11 @@ bool wildcard_match_encoded_vars(
if (wildcard_var_placeholders[wildcard_var_ix] == c) {
auto decoded_var = decode_integer_var(encoded_vars[var_ix]);
if (wildcard_match_unsafe(decoded_var, wildcard_var_queries[wildcard_var_ix])) {
if (string_utils::wildcard_match_unsafe(
decoded_var,
wildcard_var_queries[wildcard_var_ix]
))
{
++wildcard_var_ix;
if (wildcard_var_ix == wildcard_var_queries_len) {
break;
Expand Down
4 changes: 3 additions & 1 deletion components/core/src/ffi/search/CompositeWildcardToken.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "CompositeWildcardToken.hpp"

#include <string_utils/string_utils.hpp>

#include "../../ir/parsing.hpp"
#include "../../ir/types.hpp"

Expand Down Expand Up @@ -28,7 +30,7 @@ CompositeWildcardToken<encoded_variable_t>::CompositeWildcardToken(
is_escaped = false;
} else if ('\\' == c) {
is_escaped = true;
} else if (is_wildcard(c)) {
} else if (string_utils::is_wildcard(c)) {
m_wildcards.emplace_back(c, i, begin_pos == i || end_pos - 1 == i);
}
}
Expand Down
7 changes: 4 additions & 3 deletions components/core/src/ffi/search/WildcardToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

#include <string_view>

#include <string_utils/string_utils.hpp>

#include "../../ir/types.hpp"
#include "../../string_utils.hpp"
#include "../../type_utils.hpp"
#include "../encoding_methods.hpp"
#include "QueryWildcard.hpp"
Expand Down Expand Up @@ -122,9 +123,9 @@ static bool could_be_static_text(string_view query, size_t begin_pos, size_t end
is_escaped = false;
} else if ('\\' == c) {
is_escaped = true;
} else if (is_decimal_digit(c)) {
} else if (string_utils::is_decimal_digit(c)) {
return false;
} else if (is_alphabet(c)) {
} else if (string_utils::is_alphabet(c)) {
contains_alphabet = true;
}
}
Expand Down
7 changes: 5 additions & 2 deletions components/core/src/ffi/search/query_methods.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "query_methods.hpp"

#include <string_utils/string_utils.hpp>

#include "../../ir/parsing.hpp"
#include "../../ir/types.hpp"
#include "CompositeWildcardToken.hpp"
Expand All @@ -13,6 +15,7 @@ using std::string;
using std::string_view;
using std::variant;
using std::vector;
using string_utils::is_wildcard;

namespace ffi::search {
static auto TokenGetBeginPos = [](auto const& token) { return token.get_begin_pos(); };
Expand Down Expand Up @@ -251,9 +254,9 @@ static void find_delimiter(
}
}

if (is_decimal_digit(c)) {
if (string_utils::is_decimal_digit(c)) {
contains_decimal_digit = true;
} else if (is_alphabet(c)) {
} else if (string_utils::is_alphabet(c)) {
contains_alphabet = true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion components/core/src/ir/LogEventDeserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <vector>

#include <json/single_include/nlohmann/json.hpp>
#include <string_utils/string_utils.hpp>

#include "../ffi/ir_stream/decoding_methods.hpp"
#include "types.hpp"
Expand Down Expand Up @@ -56,7 +57,7 @@ auto LogEventDeserializer<encoded_variable_t>::create(ReaderInterface& reader)
}
auto ref_timestamp_str = ref_timestamp_iter->get_ref<nlohmann::json::string_t&>();
epoch_time_ms_t ref_timestamp{};
if (false == convert_string_to_int(ref_timestamp_str, ref_timestamp)) {
if (false == string_utils::convert_string_to_int(ref_timestamp_str, ref_timestamp)) {
return std::errc::protocol_error;
}

Expand Down
Loading

0 comments on commit abda9a3

Please sign in to comment.