1
0
Files
YYCCommonplace/src/yycc/string/op.hpp

158 lines
6.6 KiB
C++
Raw Normal View History

2024-04-25 10:38:13 +08:00
#pragma once
#include <cstdarg>
#include <functional>
#include <vector>
2025-06-20 23:38:34 +08:00
#include "../string.hpp"
#define NS_YYCC_STRING ::yycc::string
2024-04-25 10:38:13 +08:00
2025-06-20 23:38:34 +08:00
namespace yycc::string::op {
2024-07-24 15:03:31 +08:00
/**
* @brief Perform a string formatting operation.
* @param[out] strl
* The string container receiving the result.
* There is no guarantee that the content is not modified when function failed.
* @param[in] format The format string.
* @param[in] ... Argument list of format string.
* @return True if success, otherwise false.
*/
2025-06-20 23:38:34 +08:00
bool printf(NS_YYCC_STRING::u8string& strl, const NS_YYCC_STRING::u8char* format, ...);
2024-07-24 15:03:31 +08:00
/**
* @brief Perform a string formatting operation.
* @param[out] strl
* The string container receiving the result.
* There is no guarantee that the content is not modified when function failed.
* @param[in] format The format string.
* @param[in] argptr Argument list of format string.
* @return True if success, otherwise false.
*/
2025-06-20 23:38:34 +08:00
bool vprintf(NS_YYCC_STRING::u8string& strl, const NS_YYCC_STRING::u8char* format, va_list argptr);
2024-07-24 15:03:31 +08:00
/**
* @brief Perform a string formatting operation.
* @param[in] format The format string.
* @param[in] ... Argument list of format string.
* @return The formatting result. Empty string if error happened.
*/
2025-06-20 23:38:34 +08:00
NS_YYCC_STRING::u8string printf(const NS_YYCC_STRING::u8char* format, ...);
2024-07-24 15:03:31 +08:00
/**
* @brief Perform a string formatting operation.
* @param[in] format The format string.
* @param[in] argptr Argument list of format string.
* @return The formatting result. Empty string if error happened.
*/
2025-06-20 23:38:34 +08:00
NS_YYCC_STRING::u8string vprintf(const NS_YYCC_STRING::u8char* format, va_list argptr);
2024-04-25 10:38:13 +08:00
2024-07-24 15:03:31 +08:00
/**
* @brief Modify given string with all occurrences of substring \e old replaced by \e new.
* @param[in,out] strl The string for replacing
* @param[in] _from_strl The \e old string.
* @param[in] _to_strl The \e new string.
*/
2025-06-20 23:38:34 +08:00
void replace(NS_YYCC_STRING::u8string& strl, const NS_YYCC_STRING::u8string_view& _from_strl, const NS_YYCC_STRING::u8string_view& _to_strl);
2024-07-24 15:03:31 +08:00
/**
* @brief Return a copy with all occurrences of substring \e old replaced by \e new.
* @param[in] _strl The string for replacing
* @param[in] _from_strl The \e old string.
* @param[in] _to_strl The \e new string.
* @return The result of replacement.
*/
2025-06-20 23:38:34 +08:00
NS_YYCC_STRING::u8string replace(const NS_YYCC_STRING::u8string_view& _strl, const NS_YYCC_STRING::u8string_view& _from_strl, const NS_YYCC_STRING::u8string_view& _to_strl);
/**
2024-07-24 15:03:31 +08:00
* @brief The data provider of general join function.
* @details
* For programmer using lambda to implement this function pointer:
* \li During calling, implementation should assign the reference of string view passed in argument
* to the string which need to be joined.
* \li Function return true to continue joining. otherwise return false to stop joining.
* The argument content assigned in the calling returning false is not included in join process.
*/
2025-06-20 23:38:34 +08:00
using JoinDataProvider = std::function<bool(NS_YYCC_STRING::u8string_view&)>;
/**
2024-07-24 15:03:31 +08:00
* @brief Universal join function.
* @details
* This function use function pointer as a general data provider interface,
* so this function suit for all types container.
* You can use this universal join function for any custom container by
* using C++ lambda syntax to create a code block adapted to this function pointer.
* @param[in] fct_data The function pointer in JoinDataProvider type prividing the data to be joined.
2025-06-22 17:14:49 +08:00
* @param[in] delimiter The delimiter used for joining.
2024-07-24 15:03:31 +08:00
* @return The result string of joining.
*/
2025-06-22 17:14:49 +08:00
NS_YYCC_STRING::u8string join(JoinDataProvider fct_data, const NS_YYCC_STRING::u8string_view& delimiter);
/**
* @brief Specialized join function for standard library container.
* @tparam InputIt
* Must meet the requirements of LegacyInputIterator.
2025-06-20 23:38:34 +08:00
* It also can be dereferenced and then implicitly converted to NS_YYCC_STRING::u8string_view.
* @param[in] first The beginning of the range of elements to join.
* @param[in] last The terminal of the range of elements to join (exclusive).
2025-06-22 17:14:49 +08:00
* @param[in] delimiter The delimiter used for joining.
2024-07-24 15:03:31 +08:00
* @return The result string of joining.
*/
template<class InputIt>
2025-06-22 17:14:49 +08:00
NS_YYCC_STRING::u8string join(InputIt first, InputIt last, const NS_YYCC_STRING::u8string_view& delimiter) {
2025-06-20 23:38:34 +08:00
return join([&first, &last](NS_YYCC_STRING::u8string_view& view) -> bool {
// if we reach tail, return false to stop join process
if (first == last) return false;
// otherwise fetch data, inc iterator and return.
view = *first;
++first;
return true;
2025-06-22 17:14:49 +08:00
}, delimiter);
}
2024-04-26 15:37:28 +08:00
2024-07-24 15:03:31 +08:00
/**
* @brief Convert given string to lowercase.
* @param[in,out] strl The string to be lowercase.
*/
2025-06-20 23:38:34 +08:00
void lower(NS_YYCC_STRING::u8string& strl);
2024-07-24 15:03:31 +08:00
/**
* @brief Return a copy of the string converted to lowercase.
* @param[in] strl The string to be lowercase.
* @return The copy of the string converted to lowercase.
2024-07-24 15:03:31 +08:00
*/
2025-06-22 19:53:49 +08:00
NS_YYCC_STRING::u8string to_lower(const NS_YYCC_STRING::u8string_view& strl);
2024-07-24 15:03:31 +08:00
/**
* @brief Convert given string to uppercase.
* @param[in,out] strl The string to be uppercase.
*/
2025-06-20 23:38:34 +08:00
void upper(NS_YYCC_STRING::u8string& strl);
/**
* @brief Return a copy of the string converted to uppercase.
* @param[in] strl The string to be uppercase.
* @return The copy of the string converted to uppercase.
*/
2025-06-22 19:53:49 +08:00
NS_YYCC_STRING::u8string to_upper(const NS_YYCC_STRING::u8string_view& strl);
2025-06-22 17:14:49 +08:00
/**
* @brief Split given string with specified delimiter as string view.
* @param[in] strl The string need to be splitting.
* @param[in] _delimiter The delimiter for splitting.
* @return
* The split result with string view format.
* This will not produce any copy of original string.
* \par
* If given string or delimiter are empty,
* the result container will only contain 1 entry which is equal to given string.
* @see Split(const NS_YYCC_STRING::u8string_view&, const NS_YYCC_STRING::u8char*)
*/
std::vector<NS_YYCC_STRING::u8string_view> split(const NS_YYCC_STRING::u8string_view& strl, const NS_YYCC_STRING::u8string_view& _delimiter);
/**
2025-06-22 17:14:49 +08:00
* @brief Split given string with specified delimiter.
2024-07-24 15:03:31 +08:00
* @param[in] strl The string need to be splitting.
2025-06-22 17:14:49 +08:00
* @param[in] _delimiter The delimiter for splitting.
2024-07-24 15:03:31 +08:00
* @return
* The split result.
* \par
2025-06-22 17:14:49 +08:00
* If given string or delimiter are empty,
2024-07-24 15:03:31 +08:00
* the result container will only contain 1 entry which is equal to given string.
*/
2025-06-22 17:14:49 +08:00
std::vector<NS_YYCC_STRING::u8string> split_owned(const NS_YYCC_STRING::u8string_view& strl, const NS_YYCC_STRING::u8string_view& _delimiter);
// undefined lazy_split(const NS_YYCC_STRING::u8string_view& strl, const NS_YYCC_STRING::u8string_view& _delimiter);
2024-04-25 10:38:13 +08:00
}
2025-06-20 23:38:34 +08:00
#undef NS_YYCC_STRING