诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

230 lines
6.2 KiB

  1. // ***********************************************************************
  2. // Filename : StringUtil.h
  3. // Author : LIZHENG
  4. // Created : 2014-08-16
  5. // Description : 字符串辅助函数
  6. //
  7. // Copyright (c) lizhenghn@gmail.com. All rights reserved.
  8. // ***********************************************************************
  9. #ifndef ZL_STRING_UTIL_H
  10. #define ZL_STRING_UTIL_H
  11. #include <string.h>
  12. #include <string>
  13. #include <sstream>
  14. #include <algorithm>
  15. #include <functional>
  16. namespace zl{
  17. namespace base{
  18. /// 格式化字符串
  19. size_t stringFormatAppend(std::string *dst, const char* format, ...);
  20. size_t stringFormat(std::string *dst, const char* format, ...);
  21. std::string stringFormat(const char *format, ...);
  22. /// 任意类型转为字符串
  23. template <typename T>
  24. inline std::string toStr(const T& t)
  25. {
  26. std::ostringstream oss;
  27. oss << t;
  28. return oss.str();
  29. }
  30. /// 字符串转为某一类型
  31. template <typename T>
  32. T strTo(const std::string& str)
  33. {
  34. T t;
  35. std::istringstream iss(str);
  36. iss >> t;
  37. return t;
  38. }
  39. /// 字符串忽略大小写比较, 可用作容器(比如map、set)的比较子
  40. struct string_cmp_nocase : public std::binary_function<std::string, std::string, bool>
  41. {
  42. public:
  43. bool operator()(const std::string& lhs, const std::string& rhs) const
  44. {
  45. std::string::const_iterator p = lhs.begin();
  46. std::string::const_iterator p2 = rhs.begin();
  47. while (p != lhs.end() && p2 != rhs.end())
  48. {
  49. if (toupper(*p) != toupper(*p2))
  50. return (toupper(*p) < toupper(*p2) ? 1 : 0);
  51. ++p;
  52. ++p2;
  53. }
  54. return (lhs.size() == rhs.size()) ? 0 : (lhs.size() < rhs.size()) ? 1 : 0;
  55. }
  56. };
  57. /// 将字符串转为小写并返回
  58. inline std::string toLower(const std::string& str)
  59. {
  60. std::string t = str;
  61. std::transform(t.begin(), t.end(), t.begin(), ::tolower);
  62. return t;
  63. }
  64. /// 将字符串转为小写并返回
  65. inline std::string toUpper(const std::string& str)
  66. {
  67. std::string t = str;
  68. std::transform(t.begin(), t.end(), t.begin(), ::toupper);
  69. return t;
  70. }
  71. /// 判断字符串是否以某一子串为开始
  72. inline bool startsWith(const std::string& str, const std::string& substr)
  73. {
  74. return str.find(substr) == 0;
  75. }
  76. /// 判断字符串是否以某一子串为结尾
  77. inline bool endsWith(const std::string& str, const std::string& substr)
  78. {
  79. return str.rfind(substr) == (str.length() - substr.length());
  80. }
  81. /// 比较两个字符串是否相等
  82. inline bool equals(const std::string& lhs, const std::string& rhs)
  83. {
  84. return (lhs) == (rhs);
  85. }
  86. /// 去掉字符串中左边属于字符串delim中任一字符的所有字符(默认去除空格)
  87. inline std::string& trimLeft(std::string& str, const char* delim = " ")
  88. {
  89. str.erase(0, str.find_first_not_of(delim));
  90. return str;
  91. }
  92. /// 去掉字符串中右边属于字符串delim中任一字符的所有字符(默认去除空格)
  93. inline std::string& trimRight(std::string& str, const char* delim = " ")
  94. {
  95. str.erase(str.find_last_not_of(delim) + 1);
  96. return str;
  97. }
  98. /// 去掉字符串中两端属于字符串delim中任一字符的所有字符(默认去除空格)
  99. inline std::string& trim(std::string& str, const char* delim = " ")
  100. {
  101. trimLeft(str, delim);
  102. trimRight(str, delim);
  103. return str;
  104. }
  105. /// 去掉字符串中的所有特定单一字符
  106. inline std::string erase(std::string& str, char c = ' ')
  107. {
  108. str.erase(std::remove_if(str.begin(), str.end(), std::bind2nd(std::equal_to<char>(), c)), str.end());
  109. return str;
  110. }
  111. /// 字符串替换 去掉字符串中的某特定字符串delim并以新字符串s代替
  112. inline std::string replaceAll(std::string& str, const char* delim, const char* s = "")
  113. {
  114. size_t len = strlen(delim);
  115. size_t pos = str.find(delim);
  116. while (pos != std::string::npos)
  117. {
  118. str.replace(pos, len, s);
  119. pos = str.find(delim, pos);
  120. }
  121. return str;
  122. }
  123. /// 字符串分隔,insertEmpty : 如果有连续的delim,是否插入空串
  124. //inline void split(const std::string& str, std::vector<std::string>& result,
  125. // const std::string& delim = " ", bool insertEmpty = false)
  126. //{
  127. // if(str.empty() || delim.empty())
  128. // return;
  129. //
  130. // std::string::const_iterator substart = str.begin(), subend;
  131. // while(true)
  132. // {
  133. // subend = std::search(substart, str.end(), delim.begin(), delim.end());
  134. // std::string temp(substart, subend);
  135. //
  136. // if(!temp.empty())
  137. // {
  138. // result.push_back(temp);
  139. // }
  140. // else if(insertEmpty)
  141. // {
  142. // result.push_back("");
  143. // }
  144. //
  145. // if(subend == str.end())
  146. // break;
  147. // substart = subend + delim.size();
  148. // }
  149. //}
  150. /// 字符串合并
  151. template< typename SequenceSequenceT, typename Range1T>
  152. inline typename SequenceSequenceT::value_type join(const SequenceSequenceT& Input, const Range1T& Separator)
  153. {
  154. typedef typename SequenceSequenceT::value_type ResultT;
  155. typedef typename SequenceSequenceT::const_iterator InputIteratorT;
  156. InputIteratorT itBegin = Input.begin();
  157. InputIteratorT itEnd = Input.end();
  158. ResultT Result;
  159. if (itBegin != itEnd)
  160. {
  161. Result += *itBegin;
  162. ++itBegin;
  163. }
  164. for (; itBegin != itEnd; ++itBegin)
  165. {
  166. Result += Separator; // Add separator
  167. Result += *itBegin; // Add element
  168. }
  169. return Result;
  170. }
  171. template<typename SequenceSequenceT, typename Range1T, typename PredicateT>
  172. inline typename SequenceSequenceT::value_type
  173. join_if(const SequenceSequenceT& Input, const Range1T& Separator, PredicateT Pred)
  174. {
  175. typedef typename SequenceSequenceT::value_type ResultT;
  176. typedef typename SequenceSequenceT::const_iterator InputIteratorT;
  177. InputIteratorT itBegin = Input.begin();
  178. InputIteratorT itEnd = Input.end();
  179. ResultT Result;
  180. while (itBegin != itEnd && !Pred(*itBegin)) ++itBegin;
  181. if (itBegin != itEnd)
  182. {
  183. Result += *itBegin;
  184. ++itBegin;
  185. }
  186. for (; itBegin != itEnd; ++itBegin)
  187. {
  188. if (Pred(*itBegin))
  189. {
  190. Result += Separator; // Add separator
  191. Result += *itBegin; // Add element
  192. }
  193. }
  194. return Result;
  195. }
  196. } // namespace base
  197. } // namespace zl
  198. #endif // ZL_STRING_UTIL_H