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.

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