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.

257 lines
5.3 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 <set>
  15. #include <sstream>
  16. #include <string>
  17. #include <vector>
  18. #include <fnmatch.h>
  19. using std::istringstream;
  20. using std::set;
  21. using std::string;
  22. using std::vector;
  23. namespace str
  24. {
  25. void
  26. split(const char *str_,
  27. const char delimiter_,
  28. vector<string> *result_)
  29. {
  30. string part;
  31. istringstream ss(str_);
  32. while(std::getline(ss,part,delimiter_))
  33. result_->push_back(part);
  34. }
  35. void
  36. split(const char *str_,
  37. const char delimiter_,
  38. set<string> *result_)
  39. {
  40. string part;
  41. istringstream ss(str_);
  42. while(std::getline(ss,part,delimiter_))
  43. result_->insert(part);
  44. }
  45. void
  46. split(const string &str_,
  47. const char delimiter_,
  48. vector<string> *result_)
  49. {
  50. return str::split(str_.c_str(),delimiter_,result_);
  51. }
  52. void
  53. split(const string &str_,
  54. const char delimiter_,
  55. set<string> *result_)
  56. {
  57. return str::split(str_.c_str(),delimiter_,result_);
  58. }
  59. void
  60. rsplit1(const string &str_,
  61. const char delimiter_,
  62. vector<string> *result_)
  63. {
  64. std::size_t off;
  65. off = str_.rfind('=');
  66. if(off == std::string::npos)
  67. {
  68. result_->push_back(str_);
  69. }
  70. else
  71. {
  72. result_->push_back(str_.substr(0,off));
  73. result_->push_back(str_.substr(off+1));
  74. }
  75. }
  76. void
  77. splitkv(const string &str_,
  78. const char delimiter_,
  79. string *key_,
  80. string *val_)
  81. {
  82. istringstream iss;
  83. std::string key;
  84. std::string val;
  85. iss.str(str_);
  86. std::getline(iss,key,delimiter_);
  87. std::getline(iss,val,'\0');
  88. *key_ = key;
  89. *val_ = val;
  90. }
  91. string
  92. join(const vector<string> &vec_,
  93. const size_t substridx_,
  94. const char sep_)
  95. {
  96. if(vec_.empty())
  97. return string();
  98. string rv = vec_[0].substr(substridx_);
  99. for(size_t i = 1; i < vec_.size(); i++)
  100. rv += sep_ + vec_[i].substr(substridx_);
  101. return rv;
  102. }
  103. string
  104. join(const vector<string> &vec_,
  105. const char sep_)
  106. {
  107. return str::join(vec_,0,sep_);
  108. }
  109. string
  110. join(const set<string> &set_,
  111. const char sep_)
  112. {
  113. string rv;
  114. for(auto const &s : set_)
  115. rv += s + sep_;
  116. rv.pop_back();
  117. return rv;
  118. }
  119. size_t
  120. longest_common_prefix_index(const vector<string> &vec_)
  121. {
  122. if(vec_.empty())
  123. return string::npos;
  124. for(size_t n = 0; n < vec_[0].size(); n++)
  125. {
  126. char chr = vec_[0][n];
  127. for(size_t i = 1; i < vec_.size(); i++)
  128. {
  129. if(n >= vec_[i].size())
  130. return n;
  131. if(vec_[i][n] != chr)
  132. return n;
  133. }
  134. }
  135. return string::npos;
  136. }
  137. string
  138. longest_common_prefix(const vector<string> &vec_)
  139. {
  140. size_t idx;
  141. idx = longest_common_prefix_index(vec_);
  142. if(idx != string::npos)
  143. return vec_[0].substr(0,idx);
  144. return string();
  145. }
  146. string
  147. remove_common_prefix_and_join(const vector<string> &vec_,
  148. const char sep_)
  149. {
  150. size_t idx;
  151. idx = str::longest_common_prefix_index(vec_);
  152. if(idx == string::npos)
  153. idx = 0;
  154. return str::join(vec_,idx,sep_);
  155. }
  156. void
  157. erase_fnmatches(const vector<string> &patterns_,
  158. vector<string> &strs_)
  159. {
  160. vector<string>::iterator si;
  161. vector<string>::const_iterator pi;
  162. si = strs_.begin();
  163. while(si != strs_.end())
  164. {
  165. int match = FNM_NOMATCH;
  166. for(pi = patterns_.begin();
  167. pi != patterns_.end() && match != 0;
  168. ++pi)
  169. {
  170. match = fnmatch(pi->c_str(),si->c_str(),0);
  171. }
  172. if(match == 0)
  173. si = strs_.erase(si);
  174. else
  175. ++si;
  176. }
  177. }
  178. bool
  179. isprefix(const string &s0_,
  180. const string &s1_)
  181. {
  182. return ((s0_.size() >= s1_.size()) &&
  183. (s0_.compare(0,s1_.size(),s1_) == 0));
  184. }
  185. bool
  186. startswith(const string &str_,
  187. const string &prefix_)
  188. {
  189. return ((str_.size() >= prefix_.size()) &&
  190. (str_.compare(0,prefix_.size(),prefix_) == 0));
  191. }
  192. bool
  193. endswith(const string &str_,
  194. const string &suffix_)
  195. {
  196. if(suffix_.size() > str_.size())
  197. return false;
  198. return std::equal(suffix_.rbegin(),
  199. suffix_.rend(),
  200. str_.rbegin());
  201. }
  202. std::string
  203. trim(const std::string &str_)
  204. {
  205. std::string rv;
  206. rv = str_;
  207. while(!rv.empty() && (rv[0] == ' '))
  208. rv.erase(0);
  209. while(!rv.empty() && (rv[rv.size()-1] == ' '))
  210. rv.erase(rv.size()-1);
  211. return rv;
  212. }
  213. }