You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.cpp 2.0 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "utils.h"
  2. vector<string> splitAllowSeperator(vector<char>::iterator from, vector<char>::iterator to, char sep)
  3. {
  4. vector<string> values;
  5. vector<char> buf;
  6. while(from < to)
  7. {
  8. if (*from == sep)
  9. {
  10. if (from+1 != to && *(from+1) == sep)
  11. {
  12. ++from;
  13. }
  14. else
  15. {
  16. char* value = new char[buf.size()+1];
  17. memcpy(value, buf.data(), buf.size());
  18. value[buf.size()]=0;
  19. values.push_back(value);
  20. delete[] value;
  21. buf.clear();
  22. ++from;
  23. continue;
  24. }
  25. }
  26. buf.push_back(*from);
  27. ++from;
  28. }
  29. return values;
  30. }
  31. void stringReplace(string& target,const string& pattern,const string& candidate)
  32. {
  33. auto pos=0;
  34. auto ps=pattern.size();
  35. auto cs=candidate.size();
  36. while((pos=target.find(pattern,pos)) != string::npos)
  37. {
  38. target.replace(pos,ps,candidate);
  39. pos+=cs;
  40. }
  41. }
  42. string getFileNameFromPath(const string &path)
  43. {
  44. auto sep = '/';
  45. auto result = path;
  46. if (result.at(result.length()-1) == sep)
  47. result = result.substr(0, result.length()-1);
  48. auto pos = result.find_last_of('/');
  49. if (pos == string::npos)
  50. return result;
  51. return result.substr(pos+1);
  52. }
  53. bool startsWith(const string &str, const string &patten)
  54. {
  55. if (str.length() < patten.length())
  56. return false;
  57. return std::equal(patten.begin(), patten.end(), str.begin());
  58. }
  59. bool endsWith(const string &str, const string &patten)
  60. {
  61. if (str.length() < patten.length())
  62. return false;
  63. return std::equal(patten.rbegin(), patten.rend(), str.rbegin());
  64. }
  65. string toString(const vector<char> &buf)
  66. {
  67. auto len = buf.size();
  68. char* value = new char[len+1];
  69. memcpy(value, buf.data(), len);
  70. value[len]=0;
  71. string result = value;
  72. delete[] value;
  73. return result;
  74. }

mac下的“飞秋”大多数只是飞鸽传书协议,而且未发现令人满意的开源项目,所以基于c++与qt实现了基础的飞秋协议。

Contributors (1)