stringutil.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef _STRINGUTIL_H
  18. #define _STRINGUTIL_H
  19. #include <kopano/zcdefs.h>
  20. #include <cstdarg>
  21. #include <string>
  22. #include <vector>
  23. #include <algorithm>
  24. #include <cctype>
  25. #include <cwctype>
  26. #include <kopano/platform.h>
  27. #include <openssl/md5.h>
  28. namespace KC {
  29. /*
  30. * Comparison handler for case-insensitive keys in maps
  31. */
  32. struct strcasecmp_comparison {
  33. bool operator()(const std::string &left, const std::string &right) const
  34. {
  35. return left.size() < right.size() || (left.size() == right.size() && strcasecmp(left.c_str(), right.c_str()) < 0);
  36. }
  37. };
  38. struct wcscasecmp_comparison {
  39. bool operator()(const std::wstring &left, const std::wstring &right) const
  40. {
  41. return left.size() < right.size() || (left.size() == right.size() && wcscasecmp(left.c_str(), right.c_str()) < 0);
  42. }
  43. };
  44. static inline std::string strToUpper(std::string f) {
  45. transform(f.begin(), f.end(), f.begin(), ::toupper);
  46. return f;
  47. }
  48. static inline std::string strToLower(std::string f) {
  49. transform(f.begin(), f.end(), f.begin(), ::tolower);
  50. return f;
  51. }
  52. static inline std::wstring strToUpper(std::wstring f)
  53. {
  54. std::transform(f.begin(), f.end(), f.begin(), ::towupper);
  55. return f;
  56. }
  57. // Use casting if passing hard coded values.
  58. extern _kc_export std::string stringify(unsigned int x, bool usehex = false, bool _signed = false);
  59. extern _kc_export std::string stringify_int64(int64_t, bool usehex = false);
  60. extern _kc_export std::string stringify_float(float);
  61. extern _kc_export std::string stringify_double(double, int prec = 18, bool locale = false);
  62. extern _kc_export std::wstring wstringify(unsigned int x, bool usehex = false, bool _signed = false);
  63. #ifdef UNICODE
  64. #define tstringify wstringify
  65. #else
  66. #define tstringify stringify
  67. #endif
  68. inline unsigned int atoui(const char *szString) { return strtoul(szString, NULL, 10); }
  69. extern _kc_export unsigned int xtoi(const char *);
  70. extern _kc_export int memsubstr(const void *haystack, size_t hsize, const void *needle, size_t nsize);
  71. std::string striconv(const std::string &strinput, const char *lpszFromCharset, const char *lpszToCharset);
  72. extern _kc_export std::string str_storage(uint64_t bytes, bool unlimited = true);
  73. extern _kc_export std::string GetServerNameFromPath(const char *);
  74. extern _kc_export std::string GetServerPortFromPath(const char *);
  75. static inline bool parseBool(const char *s)
  76. {
  77. return s == nullptr || (strcmp(s, "0") != 0 &&
  78. strcmp(s, "false") != 0 && strcmp(s, "no") != 0);
  79. }
  80. extern _kc_export std::string shell_escape(const std::string &);
  81. extern _kc_export std::string shell_escape(const std::wstring &);
  82. extern _kc_export std::vector<std::wstring> tokenize(const std::wstring &, const wchar_t sep, bool filter_empty = false);
  83. extern _kc_export std::vector<std::string> tokenize(const std::string &, const char sep, bool filter_empty = false);
  84. extern _kc_export std::string trim(const std::string &input, const std::string &trim = " ");
  85. extern _kc_export unsigned char x2b(char);
  86. extern _kc_export std::string hex2bin(const std::string &);
  87. extern _kc_export std::string hex2bin(const std::wstring &);
  88. extern _kc_export std::string bin2hex(const std::string &);
  89. std::wstring bin2hexw(const std::string &input);
  90. extern _kc_export std::string bin2hex(unsigned int len, const unsigned char *input);
  91. std::wstring bin2hexw(unsigned int inLength, const unsigned char *input);
  92. #ifdef UNICODE
  93. #define bin2hext bin2hexw
  94. #else
  95. #define bin2hext bin2hex
  96. #endif
  97. extern _kc_export std::string urlEncode(const std::string &);
  98. extern _kc_export std::string urlEncode(const std::wstring &, const char *charset);
  99. extern _kc_export std::string urlEncode(const wchar_t *input, const char *charset);
  100. extern _kc_export std::string urlDecode(const std::string &);
  101. extern _kc_export void BufferLFtoCRLF(size_t size, const char *input, char *output, size_t *outsize);
  102. extern _kc_export void StringCRLFtoLF(const std::wstring &in, std::wstring *out);
  103. extern _kc_export void StringLFtoCRLF(std::string &inout);
  104. extern _kc_export void StringTabtoSpaces(const std::wstring &in, std::wstring *out);
  105. template<typename T>
  106. std::vector<T> tokenize(const T &str, const T &delimiters)
  107. {
  108. std::vector<T> tokens;
  109. // skip delimiters at beginning.
  110. typename T::size_type lastPos = str.find_first_not_of(delimiters, 0);
  111. // find first "non-delimiter".
  112. typename T::size_type pos = str.find_first_of(delimiters, lastPos);
  113. while (std::string::npos != pos || std::string::npos != lastPos)
  114. {
  115. // found a token, add it to the std::vector.
  116. tokens.push_back(str.substr(lastPos, pos - lastPos));
  117. // skip delimiters. Note the "not_of"
  118. lastPos = str.find_first_not_of(delimiters, pos);
  119. // find next "non-delimiter"
  120. pos = str.find_first_of(delimiters, lastPos);
  121. }
  122. return tokens;
  123. }
  124. template<typename T>
  125. std::vector<T> tokenize(const T &str, const typename T::value_type *delimiters)
  126. {
  127. return tokenize(str, (T)delimiters);
  128. }
  129. template<typename T>
  130. std::vector<std::basic_string<T> > tokenize(const T* str, const T* delimiters)
  131. {
  132. return tokenize(std::basic_string<T>(str), std::basic_string<T>(delimiters));
  133. }
  134. template<typename _InputIterator, typename _Tp>
  135. _Tp join(_InputIterator __first, _InputIterator __last, _Tp __sep)
  136. {
  137. _Tp s;
  138. for (; __first != __last; ++__first) {
  139. if(!s.empty())
  140. s += __sep;
  141. s += *__first;
  142. }
  143. return s;
  144. }
  145. extern _kc_export std::string format(const char *fmt, ...) __LIKE_PRINTF(1, 2);
  146. extern _kc_export char *kc_strlcpy(char *dst, const char *src, size_t n);
  147. extern _kc_export bool kc_starts_with(const std::string &, const std::string &);
  148. extern _kc_export bool kc_istarts_with(const std::string &, const std::string &);
  149. extern _kc_export bool kc_ends_with(const std::string &, const std::string &);
  150. template<typename T> std::string kc_join(const T &v, const char *sep)
  151. {
  152. /* This is faster than std::copy(,,ostream_iterator(stringstream)); on glibc */
  153. std::string s;
  154. size_t z = 0;
  155. for (const auto i : v)
  156. z += i.size() + 1;
  157. s.reserve(z);
  158. for (const auto i : v) {
  159. s += i;
  160. s += sep;
  161. }
  162. z = strlen(sep);
  163. if (s.size() > z)
  164. s.erase(s.size() - z, z);
  165. return s;
  166. }
  167. extern _kc_export std::string base64_encode(const unsigned char *, unsigned int);
  168. extern _kc_export std::string base64_decode(const std::string &);
  169. extern _kc_export std::string zcp_md5_final_hex(MD5_CTX *);
  170. extern _kc_export std::string string_strip_nuls(const std::string &);
  171. extern _kc_export std::wstring string_strip_nuls(const std::wstring &);
  172. } /* namespace */
  173. #endif