Utility class for VPS-related string and data processing in ASAPCabinetFE. More...
#include <string_utils.h>
Public Member Functions | |
std::string | normalizeString (const std::string &input) const |
Normalizes a string for strict comparison. | |
std::string | normalizeStringLessAggressive (const std::string &input) const |
Normalizes a string with less aggressive rules. | |
std::string | normalizeVersion (const std::string &version) const |
Normalizes a version string for comparison. | |
bool | isVersionGreaterThan (const std::string &v1, const std::string &v2) const |
Compares two version strings. | |
std::string | extractYearFromDate (const std::string &dateString) const |
Extracts the year from a date string. | |
std::string | join (const nlohmann::json &array, const std::string &delimiter) const |
Joins a JSON array of strings into a single string. | |
std::string | safeGetString (const nlohmann::json &j, const std::string &key, const std::string &defaultValue) const |
Safely extracts a string value from a JSON object. | |
std::string | cleanString (const std::string &input) const |
size_t | levenshteinDistance (const std::string &s1, const std::string &s2) const |
std::string | toLower (const std::string &str) const |
std::string | extractCleanTitle (const std::string &input) const |
Static Public Member Functions | |
static std::string | safeGetMetadataString (const nlohmann::json &j, const std::string &key, const std::string &defaultValue="") |
Cleans a string by trimming whitespace and collapsing multiple spaces. | |
static std::string | cleanMetadataString (const std::string &input) |
static std::string | capitalizeWords (const std::string &input) |
Utility class for VPS-related string and data processing in ASAPCabinetFE.
This class provides static methods to normalize strings, compare versions, extract years from dates, and join JSON arrays into strings. These utilities are used for processing VPS metadata, ensuring consistency in matching and matchmaking tasks. The methods are stateless (const) and can be extended via configUI for user-defined normalization or formatting rules.
std::string StringUtils::extractYearFromDate | ( | const std::string & | dateString | ) | const |
Extracts the year from a date string.
Parses the date string to extract a four-digit year, supporting formats like "DD.MM.YYYY" (e.g., "01.01.2023") or standalone "YYYY" (e.g., "2023"). Returns an empty string if no year is found.
dateString | The date string to parse. |
bool StringUtils::isVersionGreaterThan | ( | const std::string & | v1, |
const std::string & | v2 | ||
) | const |
Compares two version strings.
Normalizes the version strings using normalizeVersion, then compares them numerically if possible (e.g., "1.2" > "1.1"). Falls back to lexicographical comparison if numeric conversion fails. Returns true if v1 is greater than v2.
v1 | The first version string to compare. |
v2 | The second version string to compare. |
std::string StringUtils::join | ( | const nlohmann::json & | array, |
const std::string & | delimiter | ||
) | const |
Joins a JSON array of strings into a single string.
Concatenates all string elements in the JSON array using the specified delimiter, skipping non-string elements with a debug log. Returns an empty string if the array is empty or contains no valid strings.
array | The JSON array containing strings to join. |
delimiter | The string to use as a separator between elements. |
std::string StringUtils::normalizeString | ( | const std::string & | input | ) | const |
Normalizes a string for strict comparison.
Converts the input string to lowercase and removes all non-alphanumeric characters, producing a simplified string for strict matching (e.g., in metadata comparison). This method can be customized via configUI for different normalization rules.
input | The input string to normalize. |
std::string StringUtils::normalizeStringLessAggressive | ( | const std::string & | input | ) | const |
Normalizes a string with less aggressive rules.
Converts the input string to lowercase, removes specific punctuation (e.g., underscores, dashes), preserves spaces and parentheses, collapses multiple spaces into one, and trims whitespace. This method is suitable for less strict matching while retaining some structure.
input | The input string to normalize. |
std::string StringUtils::normalizeVersion | ( | const std::string & | version | ) | const |
Normalizes a version string for comparison.
Trims whitespace, replaces commas with dots, and extracts the first part before a dash if it matches a numeric pattern (e.g., "1.2-beta" becomes "1.2"). This method ensures consistent version formatting for comparison.
version | The version string to normalize. |
|
static |
Cleans a string by trimming whitespace and collapsing multiple spaces.
Removes leading and trailing whitespace characters from the input string and replaces any sequence of multiple internal spaces with a single space.
input | The string to clean. |
std::string StringUtils::safeGetString | ( | const nlohmann::json & | j, |
const std::string & | key, | ||
const std::string & | defaultValue | ||
) | const |
Safely extracts a string value from a JSON object.
Retrieves the string value associated with the given key from a JSON object. If the key is not found or the value is not a string, it returns a default value. Handles null values by returning the default.
j | The JSON object to extract from. |
key | The key of the string value to extract. |
defaultValue | The default string value to return if extraction fails. |