1
0

hstring.h 2.3 KB

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