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

222 lines
4.8 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include <string>
  15. #include <vector>
  16. #include <sstream>
  17. #include <fnmatch.h>
  18. using std::string;
  19. using std::vector;
  20. using std::istringstream;
  21. namespace str
  22. {
  23. void
  24. split(const char *str_,
  25. const char delimiter_,
  26. vector<string> *result_)
  27. {
  28. string part;
  29. istringstream ss(str_);
  30. while(std::getline(ss,part,delimiter_))
  31. result_->push_back(part);
  32. }
  33. void
  34. split(const string &str_,
  35. const char delimiter_,
  36. vector<string> *result_)
  37. {
  38. return str::split(str_.c_str(),delimiter_,result_);
  39. }
  40. void
  41. rsplit1(const string &str_,
  42. const char delimiter_,
  43. vector<string> *result_)
  44. {
  45. std::size_t off;
  46. off = str_.rfind('=');
  47. if(off == std::string::npos)
  48. {
  49. result_->push_back(str_);
  50. }
  51. else
  52. {
  53. result_->push_back(str_.substr(0,off));
  54. result_->push_back(str_.substr(off+1));
  55. }
  56. }
  57. void
  58. splitkv(const string &str_,
  59. const char delimiter_,
  60. string *key_,
  61. string *val_)
  62. {
  63. istringstream iss;
  64. std::string key;
  65. std::string val;
  66. iss.str(str_);
  67. std::getline(iss,key,delimiter_);
  68. std::getline(iss,val,'\0');
  69. *key_ = key;
  70. *val_ = val;
  71. }
  72. string
  73. join(const vector<string> &vec_,
  74. const size_t substridx_,
  75. const char sep_)
  76. {
  77. if(vec_.empty())
  78. return string();
  79. string rv = vec_[0].substr(substridx_);
  80. for(size_t i = 1; i < vec_.size(); i++)
  81. rv += sep_ + vec_[i].substr(substridx_);
  82. return rv;
  83. }
  84. string
  85. join(const vector<string> &vec_,
  86. const char sep_)
  87. {
  88. return str::join(vec_,0,sep_);
  89. }
  90. size_t
  91. longest_common_prefix_index(const vector<string> &vec_)
  92. {
  93. if(vec_.empty())
  94. return string::npos;
  95. for(size_t n = 0; n < vec_[0].size(); n++)
  96. {
  97. char chr = vec_[0][n];
  98. for(size_t i = 1; i < vec_.size(); i++)
  99. {
  100. if(n >= vec_[i].size())
  101. return n;
  102. if(vec_[i][n] != chr)
  103. return n;
  104. }
  105. }
  106. return string::npos;
  107. }
  108. string
  109. longest_common_prefix(const vector<string> &vec_)
  110. {
  111. size_t idx;
  112. idx = longest_common_prefix_index(vec_);
  113. if(idx != string::npos)
  114. return vec_[0].substr(0,idx);
  115. return string();
  116. }
  117. string
  118. remove_common_prefix_and_join(const vector<string> &vec_,
  119. const char sep_)
  120. {
  121. size_t idx;
  122. idx = str::longest_common_prefix_index(vec_);
  123. if(idx == string::npos)
  124. idx = 0;
  125. return str::join(vec_,idx,sep_);
  126. }
  127. void
  128. erase_fnmatches(const vector<string> &patterns_,
  129. vector<string> &strs_)
  130. {
  131. vector<string>::iterator si;
  132. vector<string>::const_iterator pi;
  133. si = strs_.begin();
  134. while(si != strs_.end())
  135. {
  136. int match = FNM_NOMATCH;
  137. for(pi = patterns_.begin();
  138. pi != patterns_.end() && match != 0;
  139. ++pi)
  140. {
  141. match = fnmatch(pi->c_str(),si->c_str(),0);
  142. }
  143. if(match == 0)
  144. si = strs_.erase(si);
  145. else
  146. ++si;
  147. }
  148. }
  149. bool
  150. isprefix(const string &s0_,
  151. const string &s1_)
  152. {
  153. return ((s0_.size() >= s1_.size()) &&
  154. (s0_.compare(0,s1_.size(),s1_) == 0));
  155. }
  156. bool
  157. startswith(const string &str_,
  158. const string &prefix_)
  159. {
  160. return ((str_.size() >= prefix_.size()) &&
  161. (str_.compare(0,prefix_.size(),prefix_) == 0));
  162. }
  163. bool
  164. endswith(const string &str_,
  165. const string &suffix_)
  166. {
  167. if(suffix_.size() > str_.size())
  168. return false;
  169. return std::equal(suffix_.rbegin(),
  170. suffix_.rend(),
  171. str_.rbegin());
  172. }
  173. std::string
  174. trim(const std::string &str_)
  175. {
  176. std::string rv;
  177. rv = str_;
  178. while(!rv.empty() && (rv[0] == ' '))
  179. rv.erase(0);
  180. while(!rv.empty() && (rv[rv.size()-1] == ' '))
  181. rv.erase(rv.size()-1);
  182. return rv;
  183. }
  184. }