paramed_string.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2011 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_PARAMED_STRING_H
  36. #define D_PARAMED_STRING_H
  37. #include "common.h"
  38. #include <string>
  39. #include <vector>
  40. #include <algorithm>
  41. #include "util.h"
  42. #include "DlAbortEx.h"
  43. #include "fmt.h"
  44. namespace aria2 {
  45. namespace paramed_string {
  46. template <typename InputIterator>
  47. InputIterator expandChoice(std::vector<std::string>& res, InputIterator first,
  48. InputIterator last)
  49. {
  50. ++first;
  51. InputIterator i = std::find(first, last, '}');
  52. if (i == last) {
  53. throw DL_ABORT_EX("Missing '}' in the parameterized string.");
  54. }
  55. std::vector<Scip> choices;
  56. util::splitIter(first, i, std::back_inserter(choices), ',', true, false);
  57. std::vector<std::string> res2;
  58. res2.reserve(res.size() * choices.size());
  59. for (std::vector<std::string>::const_iterator i = res.begin(),
  60. eoi = res.end();
  61. i != eoi; ++i) {
  62. for (std::vector<Scip>::const_iterator j = choices.begin(),
  63. eoj = choices.end();
  64. j != eoj; ++j) {
  65. res2.push_back(*i);
  66. res2.back().append((*j).first, (*j).second);
  67. }
  68. }
  69. res.swap(res2);
  70. return i + 1;
  71. }
  72. template <typename InputIterator>
  73. int32_t fromBase26(InputIterator first, InputIterator last, char zero)
  74. {
  75. int32_t res = 0;
  76. for (; first != last; ++first) {
  77. res *= 26;
  78. res += *first - zero;
  79. if (res > static_cast<int32_t>(UINT16_MAX)) {
  80. throw DL_ABORT_EX("Loop range overflow.");
  81. }
  82. }
  83. return res;
  84. }
  85. std::string toBase26(int32_t n, char zero, size_t width);
  86. template <typename InputIterator>
  87. InputIterator expandLoop(std::vector<std::string>& res, InputIterator first,
  88. InputIterator last)
  89. {
  90. ++first;
  91. InputIterator i = std::find(first, last, ']');
  92. if (i == last) {
  93. throw DL_ABORT_EX("Missing ']' in the parameterized string.");
  94. }
  95. InputIterator colon = std::find(first, i, ':');
  96. uint32_t step;
  97. if (colon == i) {
  98. step = 1;
  99. }
  100. else {
  101. if (!util::parseUIntNoThrow(step, std::string(colon + 1, i))) {
  102. throw DL_ABORT_EX("A step count must be a positive number.");
  103. }
  104. if (step > UINT16_MAX) {
  105. throw DL_ABORT_EX("Loop step overflow.");
  106. }
  107. }
  108. InputIterator minus = std::find(first, colon, '-');
  109. if (minus == colon) {
  110. throw DL_ABORT_EX("Loop range missing.");
  111. }
  112. if (util::isNumber(first, minus) && util::isNumber(minus + 1, colon)) {
  113. uint32_t start, end;
  114. if (!util::parseUIntNoThrow(start, std::string(first, minus)) ||
  115. !util::parseUIntNoThrow(end, std::string(minus + 1, colon))) {
  116. throw DL_ABORT_EX("Loop range missing.");
  117. }
  118. if (start > UINT16_MAX || end > UINT16_MAX) {
  119. throw DL_ABORT_EX("Loop range overflow.");
  120. }
  121. if (start <= end) {
  122. std::string format;
  123. if (minus - first == colon - minus - 1) {
  124. format = fmt("%%0%lud", static_cast<unsigned long>(minus - first));
  125. }
  126. else {
  127. format = "%d";
  128. }
  129. std::vector<std::string> res2;
  130. res2.reserve(res.size() * ((end + 1 - start) / step));
  131. for (std::vector<std::string>::const_iterator i = res.begin(),
  132. eoi = res.end();
  133. i != eoi; ++i) {
  134. for (uint32_t j = start; j <= end; j += step) {
  135. res2.push_back(*i);
  136. res2.back() += fmt(format.c_str(), j);
  137. }
  138. }
  139. res.swap(res2);
  140. }
  141. }
  142. else if ((util::isLowercase(first, minus) &&
  143. util::isLowercase(minus + 1, colon)) ||
  144. (util::isUppercase(first, minus) &&
  145. util::isUppercase(minus + 1, colon))) {
  146. char zero = ('a' <= *first && *first <= 'z' ? 'a' : 'A');
  147. int32_t start, end;
  148. start = fromBase26(first, minus, zero);
  149. end = fromBase26(minus + 1, colon, zero);
  150. if (start <= end) {
  151. size_t width;
  152. if (minus - first == colon - minus - 1) {
  153. width = minus - first;
  154. }
  155. else {
  156. width = 0;
  157. }
  158. std::vector<std::string> res2;
  159. res2.reserve(res.size() * ((end + 1 - start) / step));
  160. for (std::vector<std::string>::const_iterator i = res.begin(),
  161. eoi = res.end();
  162. i != eoi; ++i) {
  163. for (int32_t j = start; j <= end; j += step) {
  164. res2.push_back(*i);
  165. res2.back() += toBase26(j, zero, width);
  166. }
  167. }
  168. res.swap(res2);
  169. }
  170. }
  171. else {
  172. throw DL_ABORT_EX("Invalid loop range.");
  173. }
  174. return i + 1;
  175. }
  176. // Expand parameterized string.
  177. // The available parameters are loop [] and choice {}.
  178. //
  179. // Loop: [START-END:STEP]
  180. //
  181. // A is arbitrary string. START and END must satisfy one of following
  182. // condition:
  183. //
  184. // * both are decimal digits and START <= END.
  185. //
  186. // * both are composed of 'a' to 'z' letter and START <= END
  187. // lexicographically.
  188. //
  189. // * both are composed of 'A' to 'Z' letter and START <= END
  190. // lexicographically.
  191. //
  192. // Leading zeros in START and END are kep preserved if the length of
  193. // START and END in string representation is equal.
  194. //
  195. // When loop through START to END, we include both START and END.
  196. //
  197. // STEP is decimal number and it is used as loop step. STEP can be
  198. // omitted. If omitted, preceding ':' also must be omitted.
  199. //
  200. // START, END and STEP must be less than or equal to 65535 in decimal.
  201. //
  202. // Examples:
  203. // "alpha:[1-2]:bravo" -> ["alpha:1:bravo", "alpha:2:bravo"]
  204. // "alpha:[05-10:5]" -> ["alpha:05:bravo", "alpha:10:bravo"]
  205. //
  206. // Choice: {C1,C2,...,Cn}
  207. //
  208. // C1 to Cn are arbitrary string but they cannot contain ','.
  209. //
  210. // Examples:
  211. // "alpha:[foo,bar]:bravo" -> ["alpha:foo:bravo", "alpha:bar:bravo"]
  212. template <typename InputIterator, typename OutputIterator>
  213. void expand(InputIterator first, InputIterator last, OutputIterator out)
  214. {
  215. std::vector<std::string> res;
  216. res.push_back("");
  217. while (first != last) {
  218. InputIterator i = first;
  219. for (; i != last && *i != '{' && *i != '['; ++i)
  220. ;
  221. for (auto& re : res) {
  222. re.append(first, i);
  223. }
  224. first = i;
  225. if (first == last) {
  226. break;
  227. }
  228. if (*first == '{') {
  229. first = expandChoice(res, first, last);
  230. }
  231. else if (*first == '[') {
  232. first = expandLoop(res, first, last);
  233. }
  234. }
  235. if (res.size() != 1 || !res[0].empty()) {
  236. std::copy(res.begin(), res.end(), out);
  237. }
  238. }
  239. } // namespace paramed_string
  240. } // namespace aria2
  241. #endif // D_PARAMED_STRING_H