1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
10 #include <anna/core/util/MultiRangeExpression.hpp>
12 #include <anna/core/util/Tokenizer.hpp>
13 #include <anna/core/functions.hpp>
19 //using namespace anna;
24 //------------------------------------------------------------------------------
25 //---------------------------------------------- MultiRangeExpression::refresh()
26 //------------------------------------------------------------------------------
27 void anna::MultiRangeExpression::refresh(void) throw() {
28 anna::Tokenizer ranges, borders;
29 anna::Tokenizer::const_iterator ranges_it, borders_it;
31 unsigned int min, max;
34 if(a_literal == "") return;
36 ranges.apply(a_literal, ",");
38 for(ranges_it = ranges.begin(); ranges_it != ranges.end(); ranges_it ++) {
39 range = anna::Tokenizer::data(ranges_it);
40 borders.apply(range, "-");
41 borders_it = borders.begin();
43 if(borders_it != borders.end()) {
44 min = atoi(anna::Tokenizer::data(borders_it));
48 if(borders_it != borders.end()) {
49 max = atoi(anna::Tokenizer::data(borders_it));
53 for(unsigned int k = min; k <= max; k++) {
56 if(k == UINT_MAX/* overflow */) break;
63 //------------------------------------------------------------------------------
64 //----------------------------------- MultiRangeExpression::getExpandedLiteral()
65 //------------------------------------------------------------------------------
66 std::string anna::MultiRangeExpression::getExpandedLiteral(void) const throw() {
68 std::map < unsigned int, int/*dummy*/ >::const_iterator it;
69 std::map < unsigned int, int/*dummy*/ >::const_iterator it_min(a_data.begin());
70 std::map < unsigned int, int/*dummy*/ >::const_iterator it_max(a_data.end());
72 for(it = it_min; it != it_max; it++) {
73 result += anna::functions::asString((*it).first);
77 int pos = result.size();
79 if(pos) result.erase(pos - 1);
85 //------------------------------------------------------------------------------
86 //-------------------------------------- MultiRangeExpression::simplifyLiteral()
87 //------------------------------------------------------------------------------
88 const char * anna::MultiRangeExpression::simplifyLiteral(void) throw() {
89 if(a_data.size() == 0) return NULL;
91 std::map < unsigned int, int/*dummy*/ >::const_iterator it;
92 std::map < unsigned int, int/*dummy*/ >::const_iterator it_min(a_data.begin());
93 std::map < unsigned int, int/*dummy*/ >::const_iterator it_max(a_data.end());
94 unsigned int min = UINT_MAX;
97 unsigned int prevValue = a_data.begin()->first;
100 for(it = it_min; it != it_max; it++) {
103 if(value < min) min = value;
105 if(value - prevValue > 1) {
106 a_literal += anna::functions::asString(min);
108 a_literal += anna::functions::asString(max);
113 if(value > max) max = value;
118 a_literal += anna::functions::asString(min);
122 a_literal += anna::functions::asString(max);
125 return a_literal.c_str();