hstring.h 1.5 KB

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