1
0

hstring.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef HV_STRING_H_
  2. #define HV_STRING_H_
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. #include <sstream>
  7. #include "hexport.h"
  8. #include "hplatform.h"
  9. #include "hmap.h"
  10. #define SPACE_CHARS " \t\r\n"
  11. #define PAIR_CHARS "{}[]()<>\"\"\'\'``"
  12. namespace hv {
  13. HV_EXPORT extern std::string empty_string;
  14. HV_EXPORT extern std::map<std::string, std::string> empty_map;
  15. typedef std::vector<std::string> StringList;
  16. // std::map<std::string, std::string, StringCaseLess>
  17. class StringCaseLess : public std::less<std::string> {
  18. public:
  19. bool operator()(const std::string& lhs, const std::string& rhs) const {
  20. return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
  21. }
  22. };
  23. // NOTE: low-version NDK not provide std::to_string
  24. template<typename T>
  25. HV_INLINE std::string to_string(const T& t) {
  26. std::ostringstream oss;
  27. oss << t;
  28. return oss.str();
  29. }
  30. template<typename T>
  31. HV_INLINE T from_string(const std::string& str) {
  32. T t;
  33. std::istringstream iss(str);
  34. iss >> t;
  35. return t;
  36. }
  37. template<typename T>
  38. HV_INLINE void print(const T& t) {
  39. std::cout << t;
  40. }
  41. template<typename T>
  42. HV_INLINE void println(const T& t) {
  43. std::cout << t << std::endl;
  44. }
  45. HV_EXPORT std::string& toupper(std::string& str);
  46. HV_EXPORT std::string& tolower(std::string& str);
  47. HV_EXPORT std::string& reverse(std::string& str);
  48. HV_EXPORT bool startswith(const std::string& str, const std::string& start);
  49. HV_EXPORT bool endswith(const std::string& str, const std::string& end);
  50. HV_EXPORT bool contains(const std::string& str, const std::string& sub);
  51. HV_EXPORT std::string asprintf(const char* fmt, ...);
  52. // x,y,z
  53. HV_EXPORT StringList split(const std::string& str, char delim = ',');
  54. // k1=v1&k2=v2
  55. HV_EXPORT hv::KeyValue splitKV(const std::string& str, char kv_kv = '&', char k_v = '=');
  56. HV_EXPORT std::string trim(const std::string& str, const char* chars = SPACE_CHARS);
  57. HV_EXPORT std::string ltrim(const std::string& str, const char* chars = SPACE_CHARS);
  58. HV_EXPORT std::string rtrim(const std::string& str, const char* chars = SPACE_CHARS);
  59. HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAIR_CHARS);
  60. HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
  61. HV_EXPORT std::string replaceAll(const std::string& str, const std::string& find, const std::string& rep);
  62. struct HV_EXPORT NetAddr {
  63. std::string ip;
  64. int port;
  65. NetAddr() : port(0) {}
  66. NetAddr(const std::string& _ip, int _port) : ip(_ip), port(_port) {}
  67. NetAddr(const std::string& ipport) { from_string(ipport); }
  68. void from_string(const std::string& ipport);
  69. std::string to_string();
  70. };
  71. } // end namespace hv
  72. #endif // HV_STRING_H_