hstring.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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& toupper(std::string& str);
  35. HV_EXPORT std::string& tolower(std::string& str);
  36. HV_EXPORT std::string& reverse(std::string& str);
  37. HV_EXPORT bool startswith(const std::string& str, const std::string& start);
  38. HV_EXPORT bool endswith(const std::string& str, const std::string& end);
  39. HV_EXPORT bool contains(const std::string& str, const std::string& sub);
  40. HV_EXPORT std::string asprintf(const char* fmt, ...);
  41. // x,y,z
  42. HV_EXPORT StringList split(const std::string& str, char delim = ',');
  43. // k1=v1&k2=v2
  44. HV_EXPORT hv::KeyValue splitKV(const std::string& str, char kv_kv = '&', char k_v = '=');
  45. HV_EXPORT std::string trim(const std::string& str, const char* chars = SPACE_CHARS);
  46. HV_EXPORT std::string trimL(const std::string& str, const char* chars = SPACE_CHARS);
  47. HV_EXPORT std::string trimR(const std::string& str, const char* chars = SPACE_CHARS);
  48. HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAIR_CHARS);
  49. HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
  50. } // end namespace hv
  51. #endif // HV_STRING_H_