Skip to content

Commit c5deb51

Browse files
committed
simplify month str to enum conversion
1 parent 900c02f commit c5deb51

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

ql/time/ecb.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,33 @@
2121
#include <ql/time/ecb.hpp>
2222
#include <ql/settings.hpp>
2323
#include <ql/utilities/dataparsers.hpp>
24-
#include <boost/algorithm/string/case_conv.hpp>
2524
#include <boost/utility/string_view.hpp>
25+
#include <boost/bimap.hpp>
2626
#include <algorithm>
27-
#include <array>
2827
#include <string>
2928
#include <stdio.h>
3029

31-
using boost::algorithm::to_upper_copy;
3230
using std::string;
3331

3432
namespace QuantLib {
3533

3634
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+
}();
4051

4152
//clang-format off
4253
// Start of maintenance period
@@ -151,14 +162,11 @@ namespace QuantLib {
151162
QL_REQUIRE(isECBcode(ecbCode),
152163
ecbCode << " is not a valid ECB code");
153164

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);
161168

169+
// convert 4th, 5th characters to `Year y`
162170
Year y = ToInteger(ecbCode[3])*10 + ToInteger(ecbCode[4]);
163171
Date referenceDate = (refDate != Date() ?
164172
refDate :
@@ -177,8 +185,7 @@ namespace QuantLib {
177185
ecbDate << " is not a valid ECB date");
178186

179187
// 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());
182189

183190
// last two digits of the year
184191
const unsigned int y = ecbDate.year() % 100;
@@ -228,10 +235,7 @@ namespace QuantLib {
228235
// first 3 characters need to represent month, case insensitive
229236
{
230237
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())
235239
return false;
236240
}
237241

@@ -245,16 +249,13 @@ namespace QuantLib {
245249
ecbCode << " is not a valid ECB code");
246250

247251
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);
252253

253254
string nextCodeStr;
254255
nextCodeStr.reserve(5);
255-
if (*itMonth != "DEC") {
256+
if (itMonth->first != "DEC") {
256257
// use next month
257-
const boost::string_view nextMonth = *(itMonth + 1);
258+
const boost::string_view nextMonth = (++itMonth)->first;
258259
nextCodeStr.append(nextMonth.data(), 3);
259260

260261
// copy year

0 commit comments

Comments
 (0)