feat: finish string helper except Split()

- add string lower upper function in string helper.
- add join function in string helper.
- add split function in string helper but implementation is not perfect and should be imporved in future.
This commit is contained in:
2024-05-21 10:24:05 +08:00
parent 6ebb457bd1
commit 9c943705de
3 changed files with 186 additions and 4 deletions

View File

@ -1,6 +1,8 @@
#pragma once
#include <string>
#include <cstdarg>
#include <functional>
#include <vector>
namespace YYCC::StringHelper {
@ -10,6 +12,53 @@ namespace YYCC::StringHelper {
std::string Printf(const char* format, ...);
std::string VPrintf(const char* format, va_list argptr);
//std::string Join(const char* decilmer);
/**
* @brief The data provider of general Join function.
* This function pointer return non-null string pointer to represent a element of joined series.
* otherwise return nullptr to terminate the joining process.
*/
using JoinDataProvider = std::function<const char*()>;
/**
* @brief General Join function.
* @details This function use function pointer as a general data provider interface,
* so this function suit for all types container, the user only need write a little bit adaption code.
* @param fct_data[in] The function pointer to data provider.
* @param decilmer[in] The decilmer.
* @return A std::string instance which containing the join result.
*/
std::string Join(JoinDataProvider fct_data, const char* decilmer);
/**
* @brief Specialized Join function for common used container.
* @param data
* @param decilmer
* @param reversed
* @return
*/
std::string Join(const std::vector<std::string>& data, const char* decilmer, bool reversed = false);
/**
* @brief Transform string to lower.
* @param strl
* @return
*/
std::string Lower(const char* strl);
void Lower(std::string& strl);
/**
* @brief Transform string to upper.
* @param strl
* @return
*/
std::string Upper(const char* strl);
void Upper(std::string& strl);
/**
* @brief General Split function.
* @param _strl
* @param _decilmer
* @return
* @remarks This function may be low performance because it just a homebrew Split functon.
* It can works in most toy cases but not suit for high performance scenario.
* Also, this function will produce a copy of original string because it is not zero copy.
*/
std::vector<std::string> Split(const char* _strl, const char* _decilmer);
}