1
0

hstring.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. typedef std::vector<std::string> StringList;
  15. // std::map<std::string, std::string, StringCaseLess>
  16. class StringCaseLess : public std::less<std::string> {
  17. public:
  18. bool operator()(const std::string& lhs, const std::string& rhs) const {
  19. return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
  20. }
  21. };
  22. // NOTE: low-version NDK not provide std::to_string
  23. template<typename T>
  24. HV_INLINE std::string to_string(const T& t) {
  25. std::ostringstream oss;
  26. oss << t;
  27. return oss.str();
  28. }
  29. template<typename T>
  30. HV_INLINE T from_string(const std::string& str) {
  31. T t;
  32. std::istringstream iss(str);
  33. iss >> t;
  34. return t;
  35. }
  36. template<typename T>
  37. HV_INLINE void print(const T& t) {
  38. std::cout << t;
  39. }
  40. template<typename T>
  41. HV_INLINE void println(const T& t) {
  42. std::cout << t << std::endl;
  43. }
  44. HV_EXPORT std::string& toupper(std::string& str);
  45. HV_EXPORT std::string& tolower(std::string& str);
  46. HV_EXPORT std::string& reverse(std::string& str);
  47. HV_EXPORT bool startswith(const std::string& str, const std::string& start);
  48. HV_EXPORT bool endswith(const std::string& str, const std::string& end);
  49. HV_EXPORT bool contains(const std::string& str, const std::string& sub);
  50. HV_EXPORT std::string asprintf(const char* fmt, ...);
  51. // x,y,z
  52. HV_EXPORT StringList split(const std::string& str, char delim = ',');
  53. // k1=v1&k2=v2
  54. HV_EXPORT hv::KeyValue splitKV(const std::string& str, char kv_kv = '&', char k_v = '=');
  55. HV_EXPORT std::string trim(const std::string& str, const char* chars = SPACE_CHARS);
  56. HV_EXPORT std::string ltrim(const std::string& str, const char* chars = SPACE_CHARS);
  57. HV_EXPORT std::string rtrim(const std::string& str, const char* chars = SPACE_CHARS);
  58. HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAIR_CHARS);
  59. HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
  60. HV_EXPORT std::string replaceAll(const std::string& str, const std::string& find, const std::string& rep);
  61. } // end namespace hv
  62. #endif // HV_STRING_H_