std::string的一些常用操作封装

ifndef STRING_UTIL_H
define STRING_UTIL_H
include
include

namespace pita {
namespace factory_test_core {

// static class std::string的一些常用操作封装
class StringUtil
{
public:
/**
* @description: url编码
* @param value:待编码字符串
* @return 函数返回:编码结果
/
static std::string url_encode(const std::string& value);
/
*
* @description: url解码
* @param value:待解码字符串
* @return 函数返回:解码结果
/
static std::string url_decode(const std::string& value);
/
*
* @description: 字符串value是否以match开头
* @param value:原始字符串 match:匹配字符串
* @return true 是;false 否
/
static bool starts_with(const std::string& value, const std::string& match);
/
*
* @description: 字符串value是否以match结尾
* @param value:原始字符串 match:匹配字符串
* @return true 是;false 否
/
static bool ends_With(const std::string& value, const std::string& match);
/
*
* @description: 字符串value是否包含match
* @param value:原始字符串 match:匹配字符串
* @return true 是;false 否
/
static bool contains(const std::string& value, const std::string& match);
/
*
* @description: 字符串转大写
* @param value:待转换字符串
* @return 转换后的全大写字符串
/
std::string str_toupper(std::string value);
/
*
* @description: 字符串转小写
* @param value:待转换的字符串
* @return 转换后的全小写字符串
/
std::string str_tolower(std::string value);
/
*
* @description: 字符串src头和尾剔除chars
* @param src:原始字符串 chars:剔除字符串
* @return 字符串src头和尾剔除chars的结果
/
static std::string& strip(std::string& src, const std::string& chars = " ");
/
*
* @description: 字符串分割
* @param src:待分割字符串 tokens:分割结果 delimiters:分隔符
* @return void
*/
static void split(const std::string& src, std::vectorstd::string& tokens,
const std::string& delimiters = " ");
};

} // namespace pita
} // namespace factory_test_core

endif

#include "pita/factory_test_core/utils/StringUtil.h"

include <ctype.h>

namespace pita {
namespace factory_test_core {

// 字符"0"-"9","A"-"Z","a"-"z","-","_",".","~"都不会被编码;
// 将空格转换为加号 (+) ;
// 将非文本内容转换成"%xy"的形式,xy是两位16进制的数值;
std::string StringUtil::url_encode(const std::string& value)
{
static auto hex_chars = "0123456789ABCDEF";

std::string result;
// Minimum size of result
result.reserve(value.size());

for (auto& chr : value)
{
    // 可用 isalnum((unsigned char)chr) 替代 检查所传的字符是否是字母和数字
    if ((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') ||
        (chr >= 'a' && chr <= 'z') || chr == '-' || chr == '.' ||
        chr == '_' || chr == '~')
    {
        result += chr;
    }
    else if (chr == ' ')
    {
        result += '+';
    }
    else
    {
        result += '%';
        result += hex_chars[static_cast<unsigned char>(chr) >> 4];
        result += hex_chars[static_cast<unsigned char>(chr) & 15];
    }
}

return result;

}

std::string StringUtil::url_decode(const std::string& value)
{
std::string result;
// Minimum size of result
result.reserve(value.size() / 3 + (value.size() % 3));

for (std::size_t i = 0; i < value.size(); ++i)
{
    auto& chr = value[i];
    if (chr == '%' && i + 2 < value.size())
    {
        auto hex = value.substr(i + 1, 2);
        // 把参数 str 所指向的字符串根据给定的 base 转换为一个长整数
        // 类型为 long int 型
        auto decoded_chr =
            static_cast<char>(std::strtol(hex.c_str(), nullptr, 16));
        result += decoded_chr;
        i += 2;
    }
    else if (chr == '+')
    {
        result += ' ';
    }
    else
    {
        result += chr;
    }
}

return result;

}

bool StringUtil::starts_with(const std::string& value, const std::string& match)
{
return ((match.size() <= value.size()) &&
std::equal(match.begin(), match.end(), value.begin()));
}

bool StringUtil::ends_With(const std::string& value, const std::string& match)
{
return ((match.size() <= value.size()) &&
value.compare(value.size() - match.size(), match.size(), match) ==
0);
}

bool StringUtil::contains(const std::string& value, const std::string& match)
{
return (std::string::npos != value.find(match));
}

std::string StringUtil::str_toupper(std::string value)
{
std::transform(
value.begin(), value.end(), value.begin(),
[](unsigned char ch) -> unsigned char { return std::toupper(ch); });
return value;
}

std::string StringUtil::str_tolower(std::string value)
{
std::transform(
value.begin(), value.end(), value.begin(),
[](unsigned char ch) -> unsigned char { return std::tolower(ch); });
return value;
}

std::string& StringUtil::strip(std::string& src, const std::string& chars)
{
src.erase(0, src.find_first_not_of(chars.c_str()));
src.erase(src.find_last_not_of(chars.c_str()) + 1);
return src;
}

void StringUtil::split(const std::string& src, std::vectorstd::string& tokens,
const std::string& delimiters)
{
std::string::size_type lastPos = src.find_first_not_of(delimiters, 0);
std::string::size_type pos = src.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
tokens.emplace_back(src.substr(lastPos, pos - lastPos));
lastPos = src.find_first_not_of(delimiters, pos);
pos = src.find_first_of(delimiters, lastPos);
}
}

} // namespace pita
} // namespace factory_test_core

备注:仅供参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值