2024-09-21 21:13:34 -06:00
|
|
|
#ifndef STRING_FUNCTIONS_H
|
|
|
|
#define STRING_FUNCTIONS_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the length of a string
|
|
|
|
* @param STR string to return the length of
|
|
|
|
* @return length of the input string
|
|
|
|
*/
|
|
|
|
unsigned long string_length(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the character of a string at a given index
|
|
|
|
* @param STR full string
|
|
|
|
* @param IDX index to access string at
|
|
|
|
* @return character from STR at index IDX
|
|
|
|
*/
|
|
|
|
char string_char_at(const std::string STR, const int IDX);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a concatenation of strings left and right
|
|
|
|
* @param LEFT string to be appended to
|
|
|
|
* @param RIGHT string to be appended
|
|
|
|
* @return concatenated string LEFTRIGHT
|
|
|
|
*/
|
|
|
|
std::string string_append(const std::string LEFT, const std::string RIGHT);
|
|
|
|
|
|
|
|
/**
|
2024-09-24 16:56:57 -06:00
|
|
|
* @brief Returns the result of inserting a string into another at a given
|
|
|
|
* position
|
2024-09-21 21:13:34 -06:00
|
|
|
* @param STR original string
|
|
|
|
* @param TO_INSERT string to be inserted into STR
|
|
|
|
* @param IDX location within STR to insert TO_INSERT
|
|
|
|
* @return a new string with TO_INSERT inserted at index IDX of STR
|
|
|
|
*/
|
2024-09-24 16:56:57 -06:00
|
|
|
std::string string_insert(const std::string STR, const std::string TO_INSERT,
|
|
|
|
const int IDX);
|
2024-09-21 21:13:34 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the first index of a character in a string
|
|
|
|
* @param STR string to search within (the haystack)
|
|
|
|
* @param C character to search for (the needle)
|
2024-09-24 16:56:57 -06:00
|
|
|
* @return if found, first position within STR that C is located. otherwise
|
|
|
|
* returns string::npos
|
2024-09-21 21:13:34 -06:00
|
|
|
*/
|
|
|
|
size_t string_find(const std::string STR, const char C);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns part of a string
|
|
|
|
* @param STR original full string
|
|
|
|
* @param IDX starting position to return a substring from
|
|
|
|
* @param LEN length of substring to return
|
|
|
|
* @return a substring of length LEN from STR
|
|
|
|
*/
|
2024-09-24 16:56:57 -06:00
|
|
|
std::string string_substring(const std::string STR, const int IDX,
|
|
|
|
const int LEN);
|
2024-09-21 21:13:34 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Replaces part of a string
|
|
|
|
* @param STR original full string (the haystack)
|
|
|
|
* @param TEXT_TO_REPLACE the existing text to replace (the needle)
|
|
|
|
* @param REPLACE_WITH the new text to insert
|
|
|
|
* @return modified string (if text found), otherwise the original string
|
|
|
|
*/
|
2024-09-24 16:56:57 -06:00
|
|
|
std::string string_replace(const std::string STR,
|
|
|
|
const std::string TEXT_TO_REPLACE,
|
|
|
|
const std::string REPLACE_WITH);
|
2024-09-21 21:13:34 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the first word, given a sentence
|
|
|
|
* @param STR original full string
|
|
|
|
* @return substring up to the first whitespace
|
|
|
|
*/
|
|
|
|
std::string string_first_word(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the string with the first word removed
|
|
|
|
* @param STR original full string
|
|
|
|
* @return substring after the first whitespace
|
|
|
|
*/
|
|
|
|
std::string string_remove_first_word(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the second word, given a sentence
|
|
|
|
* @param STR original full string
|
|
|
|
* @return substring after the first whitespace up to the second whitespace
|
|
|
|
*/
|
|
|
|
std::string string_second_word(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the third word, given a sentence
|
|
|
|
* @param STR original full string
|
|
|
|
* @return substring after the second whitespace up to the third whitespace
|
|
|
|
*/
|
|
|
|
std::string string_third_word(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the nth word, given a sentence
|
|
|
|
* @param STR original full string
|
|
|
|
* @param N word within STR to return (beginning at 1)
|
|
|
|
* @return corresponding word from STR
|
|
|
|
*/
|
|
|
|
std::string string_nth_word(const std::string STR, const int N);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Splits a string into a list of tokens deliminated by a given character
|
2024-09-24 16:56:57 -06:00
|
|
|
*
|
2024-09-21 21:13:34 -06:00
|
|
|
* @param STR string to tokenize
|
|
|
|
* @param DELIMINATOR character to split on
|
2024-09-24 16:56:57 -06:00
|
|
|
* @return vector<string> list of all tokens in the order present in the
|
|
|
|
* original string
|
2024-09-21 21:13:34 -06:00
|
|
|
*/
|
2024-09-24 16:56:57 -06:00
|
|
|
std::vector<std::string> string_tokenize(const std::string STR,
|
|
|
|
const char DELIMINATOR);
|
2024-09-21 21:13:34 -06:00
|
|
|
|
|
|
|
/**
|
2024-09-24 16:56:57 -06:00
|
|
|
* @brief Returns a string substituting target character with replacement
|
|
|
|
* character
|
2024-09-21 21:13:34 -06:00
|
|
|
* @param STR original full string (the haystack)
|
|
|
|
* @param TARGET character to replace (the needle)
|
|
|
|
* @param REPLACEMENT character to substitute with
|
|
|
|
* @return string with all instance of TARGET replaced with REPLACEMENT
|
|
|
|
*/
|
2024-09-24 16:56:57 -06:00
|
|
|
std::string string_substitute(const std::string STR, const char TARGET,
|
|
|
|
const char REPLACEMENT);
|
2024-09-21 21:13:34 -06:00
|
|
|
|
|
|
|
/**
|
2024-09-24 16:56:57 -06:00
|
|
|
* @brief Returns a string with all uppercase characters converted to lowercase
|
|
|
|
* characters
|
|
|
|
*
|
2024-09-21 21:13:34 -06:00
|
|
|
* @param STR original string
|
|
|
|
* @return string with lowercase characters
|
|
|
|
*/
|
|
|
|
std::string string_to_lower(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
2024-09-24 16:56:57 -06:00
|
|
|
* @brief Returns a string with all lowercase characters converted to uppercase
|
|
|
|
* characters
|
|
|
|
*
|
2024-09-21 21:13:34 -06:00
|
|
|
* @param STR original string
|
|
|
|
* @return string with uppercase characters
|
|
|
|
*/
|
|
|
|
std::string string_to_upper(const std::string STR);
|
|
|
|
|
|
|
|
/**
|
2024-09-24 16:56:57 -06:00
|
|
|
* @brief Compares two strings to determine their ordering or equality. Returns
|
|
|
|
* -1 if LHS < RHS. Returns 0 if LHS == RHS. Returns 1 if LHS > RHS
|
|
|
|
*
|
2024-09-21 21:13:34 -06:00
|
|
|
* @param LHS left hand string
|
|
|
|
* @param RHS right hand string
|
|
|
|
* @return int representing relationship of LHS to RHS
|
|
|
|
*/
|
|
|
|
int string_compare(const std::string LHS, const std::string RHS);
|
|
|
|
|
|
|
|
#endif
|