CARTA Backend
The backend component of CARTA
Loading...
Searching...
No Matches
String.h File Reference
#include <string>
#include <vector>
Include dependency graph for String.h:
This graph shows which files directly or indirectly include this file:

Functions

std::string SafeStringEscape (const std::string &input)
 Escapes a string using percent-encoding (URL encoding).
 
std::string SafeStringUnescape (const std::string &input)
 Decodes a percent-encoded (URL-encoded) string.
 
void SplitString (std::string &input, char delim, std::vector< std::string > &parts)
 Splits a string into parts based on a specified delimiter.
 
bool HasSuffix (const std::string &haystack, const std::string &needle, bool case_sensitive=false)
 Checks if a string ends with a specified suffix.
 
bool ConstantTimeStringCompare (const std::string &a, const std::string &b)
 Performs a constant-time comparison of two strings.
 
bool StringToInt (const std::string &input, int &i)
 Converts a string to an integer.
 

Function Documentation

◆ ConstantTimeStringCompare()

bool ConstantTimeStringCompare ( const std::string &  a,
const std::string &  b 
)

Performs a constant-time comparison of two strings.

Parameters
[in]aThe first string to compare.
[in]bThe second string to compare.
Returns
true if both strings are equal, otherwise false.

This function compares two strings in a way that avoids timing attacks by ensuring that the execution time does not depend on the input values. It XORs each corresponding character and accumulates the differences, preventing early termination.

Note
If the strings have different lengths, the function immediately returns false.
Here is the caller graph for this function:

◆ HasSuffix()

bool HasSuffix ( const std::string &  haystack,
const std::string &  needle,
bool  case_sensitive 
)

Checks if a string ends with a specified suffix.

Parameters
[in]haystackThe string to be checked.
[in]needleThe suffix to look for.
[in]case_sensitiveIf true, performs a case-sensitive comparison; if false, ignores case differences.

This function determines whether the haystack string ends with the needle string. It supports both case-sensitive and case-insensitive comparisons.

Returns
true if haystack ends with needle, otherwise false.
Note
If needle is longer than haystack, the function immediately returns false.
Here is the caller graph for this function:

◆ SafeStringEscape()

std::string SafeStringEscape ( const std::string &  input)

Escapes a string using percent-encoding (URL encoding).

Parameters
[in]inputThe string to be escaped.
Returns
A percent-encoded version of the input string.

This function converts special characters in the input string into percent-encoded format, making it safe for use in URLs or other contexts where special characters need to be encoded. Alphanumeric characters and -, _, ., and ~ are left unchanged.

Here is the caller graph for this function:

◆ SafeStringUnescape()

std::string SafeStringUnescape ( const std::string &  input)

Decodes a percent-encoded (URL-encoded) string.

Parameters
[in]inputThe percent-encoded string to be decoded.
Returns
A decoded version of the input string with percent-encoded sequences replaced by their ASCII equivalents.

This function converts percent-encoded sequences (e.g., %20 for space) back into their ASCII character equivalents. It scans the input string and replaces any valid XX hexadecimal escape sequences with their corresponding characters.

Note
The function assumes that the input is a properly formatted percent-encoded string. If an invalid escape sequence is encountered, it is left unchanged.
Here is the caller graph for this function:

◆ SplitString()

void SplitString ( std::string &  input,
char  delim,
std::vector< std::string > &  parts 
)

Splits a string into parts based on a specified delimiter.

Parameters
[in]inputThe string to be split.
[in]delimThe character delimiter used to separate the input string.
[out]partsA vector to store the resulting substrings. It is cleared before new values are added.

This function tokenizes an input string by the given delimiter and stores the resulting substrings in a vector. It also removes trailing carriage return (\r) characters from each token.

Note
Empty tokens are ignored, so consecutive delimiters will not produce empty strings in parts.
Warning
The function modifies the parts vector by clearing its contents before adding new elements.
Here is the caller graph for this function:

◆ StringToInt()

bool StringToInt ( const std::string &  input,
int &  i 
)

Converts a string to an integer.

Parameters
[in]inputThe string to be converted to an integer.
[out]iReference to an integer where the result will be stored if conversion succeeds.
Returns
true if the conversion is successful, false otherwise.

Attempts to parse an integer from the given string. If successful, the parsed integer is stored in the output parameter, and the function returns true. If the conversion fails due to an invalid format or an out-of-range value, the function returns false.

Note
This function does not modify i if the conversion fails.
Here is the caller graph for this function: