string pad/rpad/lpad
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#include "ml_string.h"
|
||||
|
||||
|
||||
|
||||
// Replace a substring with a replacement string in a source string
|
||||
void replace_substring(std::string &src, const std::string &substr, const std::string &replacement) {
|
||||
size_t i = 0;
|
||||
@@ -19,7 +18,7 @@ bool regexp_search(const std::string &where, const std::string ®ex_str) {
|
||||
std::regex regex(regex_str);
|
||||
std::smatch match;
|
||||
|
||||
if(std::regex_search(where, match, regex)) {
|
||||
if (std::regex_search(where, match, regex)) {
|
||||
// std::cout << "matches for '" << where << "'\n";
|
||||
// std::cout << "Prefix: '" << match.prefix() << "'\n";
|
||||
// for (size_t i = 0; i < match.size(); ++i)
|
||||
@@ -30,3 +29,18 @@ bool regexp_search(const std::string &where, const std::string ®ex_str) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string string_padd(const std::string &str, int pad_len, char fill_char, bool from_right) {
|
||||
int str_len = str.length();
|
||||
|
||||
if (str_len == pad_len)
|
||||
return str;
|
||||
else if (str_len > pad_len) {
|
||||
return (from_right ? str.substr(0, pad_len) : str.substr(str_len - pad_len, std::string::npos));
|
||||
}
|
||||
|
||||
if (from_right)
|
||||
return str + std::string(pad_len - str.size(), fill_char);
|
||||
else
|
||||
return std::string(pad_len - str.size(), fill_char) + str;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user