9
3

string_util.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2013 Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _STRINGUTIL_H_
  17. #define _STRINGUTIL_H_
  18. #include <sstream>
  19. #include <string.h>
  20. #include <vector>
  21. #include <cstdarg>
  22. #include <tuple>
  23. #ifndef _WIN32
  24. // this doesn't appear to affect linux-based systems..need feedback for _WIN64
  25. #include <fmt/format.h>
  26. #endif
  27. #ifdef _WINDOWS
  28. #include <ctype.h>
  29. #include <functional>
  30. #include <algorithm>
  31. #endif
  32. #include "types.h"
  33. //std::string based
  34. const std::string str_tolower(std::string s);
  35. const std::string str_toupper(std::string s);
  36. const std::string ucfirst(std::string s);
  37. std::vector<std::string> split(std::string str_to_split, char delimiter);
  38. const std::string StringFormat(const char* format, ...);
  39. const std::string vStringFormat(const char* format, va_list args);
  40. std::string implode(std::string glue, std::vector<std::string> src);
  41. /**
  42. * @param str
  43. * @param chars
  44. * @return
  45. */
  46. inline std::string &ltrim(std::string &str, const std::string &chars = "\t\n\v\f\r ")
  47. {
  48. str.erase(0, str.find_first_not_of(chars));
  49. return str;
  50. }
  51. /**
  52. * @param str
  53. * @param chars
  54. * @return
  55. */
  56. inline std::string &rtrim(std::string &str, const std::string &chars = "\t\n\v\f\r ")
  57. {
  58. str.erase(str.find_last_not_of(chars) + 1);
  59. return str;
  60. }
  61. /**
  62. * @param str
  63. * @param chars
  64. * @return
  65. */
  66. inline std::string &trim(std::string &str, const std::string &chars = "\t\n\v\f\r ")
  67. {
  68. return ltrim(rtrim(str, chars), chars);
  69. }
  70. template <typename T>
  71. std::string implode(const std::string &glue, const std::pair<char, char> &encapsulation, const std::vector<T> &src)
  72. {
  73. if (src.empty()) {
  74. return {};
  75. }
  76. std::ostringstream oss;
  77. for (const T &src_iter : src) {
  78. oss << encapsulation.first << src_iter << encapsulation.second << glue;
  79. }
  80. std::string output(oss.str());
  81. output.resize(output.size() - glue.size());
  82. return output;
  83. }
  84. // _WIN32 builds require that #include<fmt/format.h> be included in whatever code file the invocation is made from (no header files)
  85. template <typename T1, typename T2>
  86. std::vector<std::string> join_pair(const std::string &glue, const std::pair<char, char> &encapsulation, const std::vector<std::pair<T1, T2>> &src)
  87. {
  88. if (src.empty()) {
  89. return {};
  90. }
  91. std::vector<std::string> output;
  92. for (const std::pair<T1, T2> &src_iter : src) {
  93. output.push_back(
  94. fmt::format(
  95. "{}{}{}{}{}{}{}",
  96. encapsulation.first,
  97. src_iter.first,
  98. encapsulation.second,
  99. glue,
  100. encapsulation.first,
  101. src_iter.second,
  102. encapsulation.second
  103. )
  104. );
  105. }
  106. return output;
  107. }
  108. // _WIN32 builds require that #include<fmt/format.h> be included in whatever code file the invocation is made from (no header files)
  109. template <typename T1, typename T2, typename T3, typename T4>
  110. std::vector<std::string> join_tuple(const std::string &glue, const std::pair<char, char> &encapsulation, const std::vector<std::tuple<T1, T2, T3, T4>> &src)
  111. {
  112. if (src.empty()) {
  113. return {};
  114. }
  115. std::vector<std::string> output;
  116. for (const std::tuple<T1, T2, T3, T4> &src_iter : src) {
  117. output.push_back(
  118. fmt::format(
  119. "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}",
  120. encapsulation.first,
  121. std::get<0>(src_iter),
  122. encapsulation.second,
  123. glue,
  124. encapsulation.first,
  125. std::get<1>(src_iter),
  126. encapsulation.second,
  127. glue,
  128. encapsulation.first,
  129. std::get<2>(src_iter),
  130. encapsulation.second,
  131. glue,
  132. encapsulation.first,
  133. std::get<3>(src_iter),
  134. encapsulation.second
  135. )
  136. );
  137. }
  138. return output;
  139. }
  140. std::vector<std::string> SplitString(const std::string &s, char delim);
  141. std::string EscapeString(const char *src, size_t sz);
  142. std::string EscapeString(const std::string &s);
  143. bool StringIsNumber(const std::string &s);
  144. void ToLowerString(std::string &s);
  145. void ToUpperString(std::string &s);
  146. std::string JoinString(const std::vector<std::string>& ar, const std::string &delim);
  147. void find_replace(std::string& string_subject, const std::string& search_string, const std::string& replace_string);
  148. void ParseAccountString(const std::string &s, std::string &account, std::string &loginserver);
  149. //const char based
  150. bool atobool(const char* iBool);
  151. bool isAlphaNumeric(const char *text);
  152. bool strn0cpyt(char* dest, const char* source, uint32 size);
  153. char *CleanMobName(const char *in, char *out);
  154. char *RemoveApostrophes(const char *s);
  155. char* strn0cpy(char* dest, const char* source, uint32 size);
  156. const char *ConvertArray(int input, char *returnchar);
  157. const char *ConvertArrayF(float input, char *returnchar);
  158. const char *MakeLowerString(const char *source);
  159. int MakeAnyLenString(char** ret, const char* format, ...);
  160. uint32 AppendAnyLenString(char** ret, uint32* bufsize, uint32* strlen, const char* format, ...);
  161. uint32 hextoi(const char* num);
  162. uint64 hextoi64(const char* num);
  163. void MakeLowerString(const char *source, char *target);
  164. void RemoveApostrophes(std::string &s);
  165. #endif