21
21
#include < ql/time/ecb.hpp>
22
22
#include < ql/settings.hpp>
23
23
#include < ql/utilities/dataparsers.hpp>
24
- #include < boost/algorithm/string/case_conv.hpp>
25
24
#include < boost/utility/string_view.hpp>
25
+ #include < boost/bimap.hpp>
26
26
#include < algorithm>
27
- #include < array>
28
27
#include < string>
29
28
#include < stdio.h>
30
29
31
- using boost::algorithm::to_upper_copy;
32
30
using std::string;
33
31
34
32
namespace QuantLib {
35
33
36
34
namespace {
37
- const std::array<boost::string_view, 12 > MONTHS{
38
- " JAN" ," FEB" ," MAR" ," APR" ," MAY" ," JUN" ,
39
- " JUL" ," AUG" ," SEP" ," OCT" ," NOV" ," DEC" };
35
+ const boost::bimap<boost::string_view, Month> MONTHS = []() {
36
+ boost::bimap<boost::string_view, Month> months;
37
+ months.insert ({" JAN" , January});
38
+ months.insert ({" FEB" , February});
39
+ months.insert ({" MAR" , March});
40
+ months.insert ({" APR" , April});
41
+ months.insert ({" MAY" , May});
42
+ months.insert ({" JUN" , June});
43
+ months.insert ({" JUL" , July});
44
+ months.insert ({" AUG" , August});
45
+ months.insert ({" SEP" , September});
46
+ months.insert ({" OCT" , October});
47
+ months.insert ({" NOV" , November});
48
+ months.insert ({" DEC" , December});
49
+ return months;
50
+ }();
40
51
41
52
// clang-format off
42
53
// Start of maintenance period
@@ -151,14 +162,11 @@ namespace QuantLib {
151
162
QL_REQUIRE (isECBcode (ecbCode),
152
163
ecbCode << " is not a valid ECB code" );
153
164
154
- char upperMonthCode[3 ];
155
- to_upper_copy (upperMonthCode, boost::string_view (ecbCode.data (), 3 ));
156
- const auto it = std::find (MONTHS.begin (), MONTHS.end (), boost::string_view (upperMonthCode, 3 ));
157
- QL_ASSERT (it != MONTHS.end () ," not an ECB month (and it should have been). code: " + ecbCode);
158
-
159
- // QuantLib::Month is 1-based!
160
- const Month m = static_cast <QuantLib::Month>(std::distance (MONTHS.begin (), it) + 1 );
165
+ // convert first 3 characters to `Month m`
166
+ const boost::string_view monthCode (ecbCode.data (), 3 );
167
+ const Month m = MONTHS.left .at (monthCode);
161
168
169
+ // convert 4th, 5th characters to `Year y`
162
170
Year y = ToInteger (ecbCode[3 ])*10 + ToInteger (ecbCode[4 ]);
163
171
Date referenceDate = (refDate != Date () ?
164
172
refDate :
@@ -177,8 +185,7 @@ namespace QuantLib {
177
185
ecbDate << " is not a valid ECB date" );
178
186
179
187
// 3 characters for the month
180
- const int monthOneBased = static_cast <int >(ecbDate.month ());
181
- const boost::string_view month = MONTHS[monthOneBased-1 ];
188
+ const boost::string_view month = MONTHS.right .at (ecbDate.month ());
182
189
183
190
// last two digits of the year
184
191
const unsigned int y = ecbDate.year () % 100 ;
@@ -228,10 +235,7 @@ namespace QuantLib {
228
235
// first 3 characters need to represent month, case insensitive
229
236
{
230
237
const boost::string_view month (ecbCode.data (), 3 );
231
- char monthUpper[3 ];
232
- to_upper_copy (monthUpper, month);
233
- const auto itMonth = std::find (MONTHS.begin (), MONTHS.end (), boost::string_view (monthUpper, 3 ));
234
- if (itMonth == MONTHS.end ())
238
+ if (MONTHS.left .find (month) == MONTHS.left .end ())
235
239
return false ;
236
240
}
237
241
@@ -245,16 +249,13 @@ namespace QuantLib {
245
249
ecbCode << " is not a valid ECB code" );
246
250
247
251
const boost::string_view month (ecbCode.data (), 3 );
248
- char monthUpper[3 ];
249
- to_upper_copy (monthUpper, month);
250
- const auto itMonth = std::find (MONTHS.begin (), MONTHS.end (), boost::string_view (monthUpper, 3 ));
251
- QL_ASSERT (itMonth != MONTHS.end (), " not an ECB month (and it should have been)" );
252
+ auto itMonth = MONTHS.left .find (month);
252
253
253
254
string nextCodeStr;
254
255
nextCodeStr.reserve (5 );
255
- if (* itMonth != " DEC" ) {
256
+ if (itMonth-> first != " DEC" ) {
256
257
// use next month
257
- const boost::string_view nextMonth = *(itMonth + 1 ) ;
258
+ const boost::string_view nextMonth = (++itMonth)-> first ;
258
259
nextCodeStr.append (nextMonth.data (), 3 );
259
260
260
261
// copy year
0 commit comments