hstring.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef HV_STRING_H_
  2. #define HV_STRING_H_
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. #include "hexport.h"
  7. #include "hplatform.h"
  8. #include "hmap.h"
  9. #define SPACE_CHARS " \t\r\n"
  10. #define PAIR_CHARS "{}[]()<>\"\"\'\'``"
  11. namespace hv {
  12. typedef std::vector<std::string> StringList;
  13. // std::map<std::string, std::string, StringCaseLess>
  14. class StringCaseLess : public std::less<std::string> {
  15. public:
  16. bool operator()(const std::string& lhs, const std::string& rhs) const {
  17. return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
  18. }
  19. };
  20. // NOTE: low-version NDK not provide std::to_string
  21. template<typename T>
  22. HV_INLINE std::string to_string(const T& t) {
  23. std::ostringstream oss;
  24. oss << t;
  25. return oss.str();
  26. }
  27. template<typename T>
  28. HV_INLINE T from_string(const std::string& str) {
  29. T t;
  30. std::istringstream iss(str);
  31. iss >> t;
  32. return t;
  33. }
  34. HV_EXPORT std::string asprintf(const char* fmt, ...);
  35. // x,y,z
  36. HV_EXPORT StringList split(const std::string& str, char delim = ',');
  37. // user=amdin&pswd=123456
  38. HV_EXPORT hv::KeyValue splitKV(const std::string& str, char kv_kv = '&', char k_v = '=');
  39. HV_EXPORT std::string trim(const std::string& str, const char* chars = SPACE_CHARS);
  40. HV_EXPORT std::string trimL(const std::string& str, const char* chars = SPACE_CHARS);
  41. HV_EXPORT std::string trimR(const std::string& str, const char* chars = SPACE_CHARS);
  42. HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAIR_CHARS);
  43. HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
  44. } // end namespace hv
  45. #endif // HV_STRING_H_