Skip to content

Commit 9ed0ded

Browse files
committed
Implement compare operator for Value base
1 parent 418679b commit 9ed0ded

File tree

2 files changed

+141
-91
lines changed

2 files changed

+141
-91
lines changed

ast.cpp

Lines changed: 119 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,10 +1171,9 @@ namespace Sass {
11711171
return string();
11721172
}
11731173

1174-
11751174
bool Number::operator== (Expression* rhs) const
11761175
{
1177-
if (Number* r = static_cast<Number*>(rhs)) {
1176+
if (Number* r = dynamic_cast<Number*>(rhs)) {
11781177
return (value() == r->value()) &&
11791178
(numerator_units_ == r->numerator_units_) &&
11801179
(denominator_units_ == r->denominator_units_);
@@ -1187,19 +1186,98 @@ namespace Sass {
11871186
return operator==(&rhs);
11881187
}
11891188

1190-
bool List::operator==(Expression* rhs) const
1189+
bool String_Quoted::operator== (Expression* rhs) const
11911190
{
1192-
try
1193-
{
1194-
List* r = dynamic_cast<List*>(rhs);
1195-
if (!r || length() != r->length()) return false;
1191+
if (String_Quoted* qstr = dynamic_cast<String_Quoted*>(rhs)) {
1192+
return (value() == qstr->value());
1193+
} else if (String_Constant* cstr = dynamic_cast<String_Constant*>(rhs)) {
1194+
return (value() == cstr->value());
1195+
}
1196+
return false;
1197+
}
1198+
1199+
bool String_Quoted::operator== (Expression& rhs) const
1200+
{
1201+
return operator==(&rhs);
1202+
}
1203+
1204+
bool String_Constant::operator== (Expression* rhs) const
1205+
{
1206+
if (String_Quoted* qstr = dynamic_cast<String_Quoted*>(rhs)) {
1207+
return (value() == qstr->value());
1208+
} else if (String_Constant* cstr = dynamic_cast<String_Constant*>(rhs)) {
1209+
return (value() == cstr->value());
1210+
}
1211+
return false;
1212+
}
1213+
1214+
bool String_Constant::operator== (Expression& rhs) const
1215+
{
1216+
return operator==(&rhs);
1217+
}
1218+
1219+
bool String_Schema::operator== (Expression* rhs) const
1220+
{
1221+
if (String_Schema* r = dynamic_cast<String_Schema*>(rhs)) {
1222+
if (length() != r->length()) return false;
1223+
for (size_t i = 0, L = length(); i < L; ++i) {
1224+
Expression* rv = (*r)[i];
1225+
Expression* lv = (*this)[i];
1226+
if (!lv || !rv) return false;
1227+
if (!(*lv == *rv)) return false;
1228+
}
1229+
return true;
1230+
}
1231+
return false;
1232+
}
1233+
1234+
bool String_Schema::operator== (Expression& rhs) const
1235+
{
1236+
return operator==(&rhs);
1237+
}
1238+
1239+
bool Boolean::operator== (Expression* rhs) const
1240+
{
1241+
if (Boolean* r = dynamic_cast<Boolean*>(rhs)) {
1242+
return (value() == r->value());
1243+
}
1244+
return false;
1245+
}
1246+
1247+
bool Boolean::operator== (Expression& rhs) const
1248+
{
1249+
return operator==(&rhs);
1250+
}
1251+
1252+
bool Color::operator== (Expression* rhs) const
1253+
{
1254+
if (Color* r = dynamic_cast<Color*>(rhs)) {
1255+
return r_ == r->r() &&
1256+
g_ == r->g() &&
1257+
b_ == r->b() &&
1258+
a_ == r->a();
1259+
}
1260+
return false;
1261+
}
1262+
1263+
bool Color::operator== (Expression& rhs) const
1264+
{
1265+
return operator==(&rhs);
1266+
}
1267+
1268+
bool List::operator== (Expression* rhs) const
1269+
{
1270+
if (List* r = dynamic_cast<List*>(rhs)) {
1271+
if (length() != r->length()) return false;
11961272
if (separator() != r->separator()) return false;
1197-
for (size_t i = 0, L = r->length(); i < L; ++i)
1198-
if (*elements()[i] != *(*r)[i]) return false;
1273+
for (size_t i = 0, L = length(); i < L; ++i) {
1274+
Expression* rv = (*r)[i];
1275+
Expression* lv = (*this)[i];
1276+
if (!lv || !rv) return false;
1277+
if (!(*lv == *rv)) return false;
1278+
}
11991279
return true;
12001280
}
1201-
catch (std::bad_cast&) {}
1202-
catch (...) { throw; }
12031281
return false;
12041282
}
12051283

@@ -1208,6 +1286,36 @@ namespace Sass {
12081286
return operator==(&rhs);
12091287
}
12101288

1289+
bool Map::operator== (Expression* rhs) const
1290+
{
1291+
if (Map* r = dynamic_cast<Map*>(rhs)) {
1292+
if (length() != r->length()) return false;
1293+
for (auto key : keys()) {
1294+
Expression* lv = at(key);
1295+
Expression* rv = r->at(key);
1296+
if (!rv || !lv) return false;
1297+
if (!(*lv == *rv)) return false;
1298+
}
1299+
return true;
1300+
}
1301+
return false;
1302+
}
1303+
1304+
bool Map::operator== (Expression& rhs) const
1305+
{
1306+
return operator==(&rhs);
1307+
}
1308+
1309+
bool Null::operator== (Expression* rhs) const
1310+
{
1311+
return rhs->concrete_type() == NULL_VAL;
1312+
}
1313+
1314+
bool Null::operator== (Expression& rhs) const
1315+
{
1316+
return operator==(&rhs);
1317+
}
1318+
12111319
size_t List::size() const {
12121320
if (!is_arglist_) return length();
12131321
// arglist expects a list of arguments

ast.hpp

Lines changed: 22 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ namespace Sass {
141141
: Expression(pstate, d, e, i, ct)
142142
{ }
143143
virtual bool operator== (Expression& rhs) const = 0;
144+
virtual bool operator== (Expression* rhs) const = 0;
144145
};
145146
}
146147

@@ -828,23 +829,6 @@ namespace Sass {
828829
static string type_name() { return "map"; }
829830
bool is_invisible() const { return empty(); }
830831

831-
virtual bool operator==(Expression& rhs) const
832-
{
833-
try
834-
{
835-
Map& m = dynamic_cast<Map&>(rhs);
836-
if (!(m && length() == m.length())) return false;
837-
for (auto key : keys())
838-
if (!(*at(key) == *m.at(key))) return false;
839-
return true;
840-
}
841-
catch (std::bad_cast&)
842-
{
843-
return false;
844-
}
845-
catch (...) { throw; }
846-
}
847-
848832
virtual size_t hash()
849833
{
850834
if (hash_ == 0) {
@@ -857,6 +841,9 @@ namespace Sass {
857841
return hash_;
858842
}
859843

844+
virtual bool operator== (Expression& rhs) const;
845+
virtual bool operator== (Expression* rhs) const;
846+
860847
ATTACH_OPERATIONS()
861848
};
862849

@@ -1238,20 +1225,6 @@ namespace Sass {
12381225
string type() { return "color"; }
12391226
static string type_name() { return "color"; }
12401227

1241-
virtual bool operator==(Expression& rhs) const
1242-
{
1243-
try
1244-
{
1245-
Color& c = (dynamic_cast<Color&>(rhs));
1246-
return c && r() == c.r() && g() == c.g() && b() == c.b() && a() == c.a();
1247-
}
1248-
catch (std::bad_cast&)
1249-
{
1250-
return false;
1251-
}
1252-
catch (...) { throw; }
1253-
}
1254-
12551228
virtual size_t hash()
12561229
{
12571230
if (hash_ == 0) {
@@ -1263,6 +1236,9 @@ namespace Sass {
12631236
return hash_;
12641237
}
12651238

1239+
virtual bool operator== (Expression& rhs) const;
1240+
virtual bool operator== (Expression* rhs) const;
1241+
12661242
ATTACH_OPERATIONS()
12671243
};
12681244

@@ -1282,20 +1258,6 @@ namespace Sass {
12821258
static string type_name() { return "bool"; }
12831259
virtual bool is_false() { return !value_; }
12841260

1285-
virtual bool operator==(Expression& rhs) const
1286-
{
1287-
try
1288-
{
1289-
Boolean& e = dynamic_cast<Boolean&>(rhs);
1290-
return e && value() == e.value();
1291-
}
1292-
catch (std::bad_cast&)
1293-
{
1294-
return false;
1295-
}
1296-
catch (...) { throw; }
1297-
}
1298-
12991261
virtual size_t hash()
13001262
{
13011263
if (hash_ == 0) {
@@ -1304,6 +1266,9 @@ namespace Sass {
13041266
return hash_;
13051267
}
13061268

1269+
virtual bool operator== (Expression& rhs) const;
1270+
virtual bool operator== (Expression* rhs) const;
1271+
13071272
ATTACH_OPERATIONS()
13081273
};
13091274

@@ -1319,6 +1284,8 @@ namespace Sass {
13191284
{ concrete_type(STRING); }
13201285
static string type_name() { return "string"; }
13211286
virtual ~String() = 0;
1287+
virtual bool operator==(Expression& rhs) const = 0;
1288+
virtual bool operator==(Expression* rhs) const = 0;
13221289
ATTACH_OPERATIONS()
13231290
};
13241291
inline String::~String() { };
@@ -1337,23 +1304,6 @@ namespace Sass {
13371304
string type() { return "string"; }
13381305
static string type_name() { return "string"; }
13391306

1340-
virtual bool operator==(Expression& rhs) const
1341-
{
1342-
try
1343-
{
1344-
String_Schema& e = dynamic_cast<String_Schema&>(rhs);
1345-
if (!(e && length() == e.length())) return false;
1346-
for (size_t i = 0, L = length(); i < L; ++i)
1347-
if (!((*this)[i] == e[i])) return false;
1348-
return true;
1349-
}
1350-
catch (std::bad_cast&)
1351-
{
1352-
return false;
1353-
}
1354-
catch (...) { throw; }
1355-
}
1356-
13571307
virtual size_t hash()
13581308
{
13591309
if (hash_ == 0) {
@@ -1363,6 +1313,9 @@ namespace Sass {
13631313
return hash_;
13641314
}
13651315

1316+
virtual bool operator==(Expression& rhs) const;
1317+
virtual bool operator==(Expression* rhs) const;
1318+
13661319
ATTACH_OPERATIONS()
13671320
};
13681321

@@ -1391,20 +1344,6 @@ namespace Sass {
13911344
string type() { return "string"; }
13921345
static string type_name() { return "string"; }
13931346

1394-
virtual bool operator==(Expression& rhs) const
1395-
{
1396-
try
1397-
{
1398-
String_Constant& e = dynamic_cast<String_Constant&>(rhs);
1399-
return e && value_ == e.value_;
1400-
}
1401-
catch (std::bad_cast&)
1402-
{
1403-
return false;
1404-
}
1405-
catch (...) { throw; }
1406-
}
1407-
14081347
virtual size_t hash()
14091348
{
14101349
if (hash_ == 0) {
@@ -1413,6 +1352,9 @@ namespace Sass {
14131352
return hash_;
14141353
}
14151354

1355+
virtual bool operator==(Expression& rhs) const;
1356+
virtual bool operator==(Expression* rhs) const;
1357+
14161358
// static char auto_quote() { return '*'; }
14171359
static char double_quote() { return '"'; }
14181360
static char single_quote() { return '\''; }
@@ -1430,6 +1372,8 @@ namespace Sass {
14301372
{
14311373
value_ = unquote(value_, &quote_mark_);
14321374
}
1375+
virtual bool operator==(Expression& rhs) const;
1376+
virtual bool operator==(Expression* rhs) const;
14331377
ATTACH_OPERATIONS()
14341378
};
14351379

@@ -1588,10 +1532,8 @@ namespace Sass {
15881532
operator bool() { return false; }
15891533
bool is_false() { return true; }
15901534

1591-
virtual bool operator==(Expression& rhs) const
1592-
{
1593-
return rhs.concrete_type() == NULL_VAL;
1594-
}
1535+
virtual bool operator== (Expression& rhs) const;
1536+
virtual bool operator== (Expression* rhs) const;
15951537

15961538
virtual size_t hash()
15971539
{

0 commit comments

Comments
 (0)