summaryrefslogtreecommitdiff
path: root/src/paramed_string.h
blob: b4b330f5f7c44cab354364463ea2595711ed2dd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2011 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#ifndef D_PARAMED_STRING_H
#define D_PARAMED_STRING_H

#include "common.h"

#include <string>
#include <vector>
#include <algorithm>

#include "util.h"
#include "DlAbortEx.h"
#include "fmt.h"

namespace aria2 {

namespace paramed_string {

template<typename InputIterator>
InputIterator expandChoice
(std::vector<std::string>& res, InputIterator first, InputIterator last)
{
  ++first;
  InputIterator i = std::find(first, last, '}');
  if(i == last) {
    throw DL_ABORT_EX("Missing '}' in the parameterized string.");
  }
  std::vector<Scip> choices;
  util::splitIter(first, i, std::back_inserter(choices), ',', true, false);
  std::vector<std::string> res2;
  res2.reserve(res.size()*choices.size());
  for(std::vector<std::string>::const_iterator i = res.begin(),
        eoi = res.end(); i != eoi; ++i) {
    for(std::vector<Scip>::const_iterator j = choices.begin(),
          eoj = choices.end(); j != eoj; ++j) {
      res2.push_back(*i);
      res2.back().append((*j).first, (*j).second);
    }
  }
  res.swap(res2);
  return i+1;
}

template<typename InputIterator>
int32_t fromBase26(InputIterator first, InputIterator last, char zero)
{
  int32_t res = 0;
  for(; first != last; ++first) {
    res *= 26;
    res += *first-zero;
    if(res > static_cast<int32_t>(UINT16_MAX)) {
      throw DL_ABORT_EX("Loop range overflow.");
    }
  }
  return res;
}

std::string toBase26(int32_t n, char zero, size_t width);

template<typename InputIterator>
InputIterator expandLoop
(std::vector<std::string>& res, InputIterator first, InputIterator last)
{
  ++first;
  InputIterator i = std::find(first, last, ']');
  if(i == last) {
    throw DL_ABORT_EX("Missing ']' in the parameterized string.");
  }
  InputIterator colon = std::find(first, i, ':');
  uint32_t step;
  if(colon == i) {
    step = 1;
  } else {
    if(!util::parseUIntNoThrow(step, std::string(colon+1, i))) {
      throw DL_ABORT_EX("A step count must be a positive number.");
    }
    if(step > UINT16_MAX) {
      throw DL_ABORT_EX("Loop step overflow.");
    }
  }
  InputIterator minus = std::find(first, colon, '-');
  if(minus == colon) {
    throw DL_ABORT_EX("Loop range missing.");
  }
  if(util::isNumber(first, minus) && util::isNumber(minus+1, colon)) {
    uint32_t start, end;
    if(!util::parseUIntNoThrow(start, std::string(first, minus)) ||
       !util::parseUIntNoThrow(end, std::string(minus+1, colon))) {
      throw DL_ABORT_EX("Loop range missing.");
    }
    if(start > UINT16_MAX || end > UINT16_MAX) {
      throw DL_ABORT_EX("Loop range overflow.");
    }
    if(start <= end) {
      std::string format;
      if(minus-first == colon-minus-1) {
        format = fmt("%%0%lud", static_cast<unsigned long>(minus-first));
      } else {
        format = "%d";
      }
      std::vector<std::string> res2;
      res2.reserve(res.size()*((end+1-start)/step));
      for(std::vector<std::string>::const_iterator i = res.begin(),
            eoi = res.end(); i != eoi; ++i) {
        for(uint32_t j = start; j <= end; j += step) {
          res2.push_back(*i);
          res2.back() += fmt(format.c_str(), j);
        }
      }
      res.swap(res2);
    }
  } else if((util::isLowercase(first, minus) &&
             util::isLowercase(minus+1, colon)) ||
            (util::isUppercase(first, minus) &&
             util::isUppercase(minus+1, colon))) {
    char zero = ('a' <= *first && *first <= 'z' ? 'a' : 'A');
    int32_t start, end;
    start = fromBase26(first, minus, zero);
    end = fromBase26(minus+1, colon, zero);
    if(start <= end) {
      size_t width;
      if(minus-first == colon-minus-1) {
        width = minus-first;
      } else {
        width = 0;
      }
      std::vector<std::string> res2;
      res2.reserve(res.size()*((end+1-start)/step));
      for(std::vector<std::string>::const_iterator i = res.begin(),
            eoi = res.end(); i != eoi; ++i) {
        for(int32_t j = start; j <= end; j += step) {
          res2.push_back(*i);
          res2.back() += toBase26(j, zero, width);
        }
      }
      res.swap(res2);
    }
  } else {
    throw DL_ABORT_EX("Invalid loop range.");
  }
  return i+1;
}

// Expand parameterized string.
// The available parameters are loop [] and choice {}.
//
// Loop: [START-END:STEP]
//
// A is arbitrary string. START and END must satisfy one of following
// condition:
//
// * both are decimal digits and START <= END.
//
// * both are composed of 'a' to 'z' letter and START <= END
//   lexicographically.
//
// * both are composed of 'A' to 'Z' letter and START <= END
//   lexicographically.
//
// Leading zeros in START and END are kep preserved if the length of
// START and END in string representation is equal.
//
// When loop through START to END, we include both START and END.
//
// STEP is dicimal number and it is used as loop step.  STEP can be
// omitted. If omitted, preceding ':' also must be omitted.
//
// START, END and STEP must be less than or equal to 65535 in decimal.
//
// Examples:
// "alpha:[1-2]:bravo" -> ["alpha:1:bravo", "alpha:2:bravo"]
// "alpha:[05-10:5]" -> ["alpha:05:bravo", "alpha:10:bravo"]
//
// Choice: {C1,C2,...,Cn}
//
// C1 to Cn are arbitrary string but they cannot contain ','.
//
// Examples:
// "alpha:[foo,bar]:bravo" -> ["alpha:foo:bravo", "alpha:bar:bravo"]
template<typename InputIterator, typename OutputIterator>
void expand
(InputIterator first,
 InputIterator last,
 OutputIterator out)
{
  std::vector<std::string> res;
  res.push_back("");
  while(first != last) {
    InputIterator i = first;
    for(; i != last && *i != '{' && *i != '['; ++i);
    for(std::vector<std::string>::iterator j = res.begin(), eoj = res.end();
        j != eoj; ++j) {
      (*j).append(first, i);
    }
    first = i;
    if(first == last) {
      break;
    }
    if(*first == '{') {
      first = expandChoice(res, first, last);
    } else if(*first == '[') {
      first = expandLoop(res, first, last);
    }
  }
  if(res.size() != 1 || !res[0].empty()) {
    std::copy(res.begin(), res.end(), out);
  }
}

} // namespace paramed_string

} // namespace aria2

#endif // D_PARAMED_STRING_H