hstring.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef HV_STRING_H_
  2. #define HV_STRING_H_
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <sstream>
  7. #include "hbase.h"
  8. using std::string;
  9. typedef std::vector<string> StringList;
  10. // MultiMap
  11. namespace std {
  12. /*
  13. int main() {
  14. std::MultiMap<std::string, std::string> kvs;
  15. kvs["name"] = "hw";
  16. kvs["filename"] = "1.jpg";
  17. kvs["filename"] = "2.jpg";
  18. //kvs.insert(std::pair<std::string,std::string>("name", "hw"));
  19. //kvs.insert(std::pair<std::string,std::string>("filename", "1.jpg"));
  20. //kvs.insert(std::pair<std::string,std::string>("filename", "2.jpg"));
  21. for (auto& pair : kvs) {
  22. printf("%s:%s\n", pair.first.c_str(), pair.second.c_str());
  23. }
  24. auto iter = kvs.find("filename");
  25. if (iter != kvs.end()) {
  26. for (int i = 0; i < kvs.count("filename"); ++i, ++iter) {
  27. printf("%s:%s\n", iter->first.c_str(), iter->second.c_str());
  28. }
  29. }
  30. return 0;
  31. }
  32. */
  33. template<typename Key,typename Value>
  34. class MultiMap : public multimap<Key, Value> {
  35. public:
  36. Value& operator[](Key key) {
  37. auto iter = this->insert(std::pair<Key,Value>(key,Value()));
  38. return (*iter).second;
  39. }
  40. };
  41. }
  42. #ifdef USE_MULTIMAP
  43. #define HV_MAP std::MultiMap
  44. #else
  45. #define HV_MAP std::map
  46. #endif
  47. // KeyValue
  48. typedef HV_MAP<std::string, std::string> KeyValue;
  49. // std::map<std::string, std::string, StringCaseLess>
  50. class StringCaseLess : public std::less<std::string> {
  51. public:
  52. bool operator()(const std::string& lhs, const std::string& rhs) const {
  53. return stricmp(lhs.c_str(), rhs.c_str()) < 0;
  54. }
  55. };
  56. namespace hv {
  57. // NOTE: low-version NDK not provide std::to_string
  58. template<typename T>
  59. static inline std::string to_string(const T& num) {
  60. std::ostringstream os;
  61. os << num;
  62. return os.str();
  63. }
  64. }
  65. #define SPACE_CHARS " \t\r\n"
  66. #define PAIR_CHARS "{}[]()<>\"\"\'\'``"
  67. string asprintf(const char* fmt, ...);
  68. // x,y,z
  69. StringList split(const string& str, char delim = ',');
  70. // user=amdin&pswd=123456
  71. KeyValue splitKV(const string& str, char kv_kv = '&', char k_v = '=');
  72. string trim(const string& str, const char* chars = SPACE_CHARS);
  73. string trimL(const string& str, const char* chars = SPACE_CHARS);
  74. string trimR(const string& str, const char* chars = SPACE_CHARS);
  75. string trim_pairs(const string& str, const char* pairs = PAIR_CHARS);
  76. string replace(const string& str, const string& find, const string& rep);
  77. // str=/mnt/share/image/test.jpg
  78. // basename=test.jpg
  79. // dirname=/mnt/share/image
  80. // filename=test
  81. // suffixname=jpg
  82. string basename(const string& str);
  83. string dirname(const string& str);
  84. string filename(const string& str);
  85. string suffixname(const string& str);
  86. #endif // HV_STRING_H_