Newer
Older
#include <libwccl/values/strset.h>
#include <libpwrutils/foreach.h>
#include <libpwrutils/util.h>
#include <sstream>
#include <boost/algorithm/string.hpp>
namespace Wccl {
const char* StrSet::type_name = "StrSet";
std::string StrSet::to_raw_string() const
{
std::stringstream ss;
Adam Wardyński
committed
ss << "[";
value_type::const_iterator it = set_.begin();
Adam Wardyński
committed
while(it != set_.end()) {
ss << '\"';
std::string item = PwrNlp::to_utf8(*it);
boost::algorithm::replace_all(item, "\\", "\\\\");
boost::algorithm::replace_all(item, "\"", "\\\"");
ss << item;
ss << '\"';
Adam Wardyński
committed
if(++it != set_.end()) {
ss << ", ";
Adam Wardyński
committed
ss << "]";
UnicodeString StrSet::to_raw_string_u() const
{
UnicodeString u;
u.append(UNICODE_STRING("[", 1));
value_type::const_iterator it = set_.begin();
while(it != set_.end()) {
u.append(UNICODE_STRING("\"", 1));
UnicodeString item = *it;
item.findAndReplace(UNICODE_STRING("\\", 1), UNICODE_STRING("\\\\", 2));
item.findAndReplace(UNICODE_STRING("\"", 1), UNICODE_STRING("\\\"", 2));
u.append(item);
u.append(UNICODE_STRING("\"", 1));
if(++it != set_.end()) {
u.append(UNICODE_STRING(", ", 2));
}
}
u.append(UNICODE_STRING("]", 1));
return u;
}
Adam Wardyński
committed
bool StrSet::intersects(const StrSet &other) const {
if (empty() || other.empty()) {
Adam Wardyński
committed
return false;
}
//We just want to check if there is an intersection, no
Adam Wardyński
committed
//need to actually compute it to check if it's empty.
//Doing it like below sounds faster than, say, sorting
//the sets and using set_intersection.
//It's faster to iterate through the smaller set and check in
//the larger than it is to do the opposite, hence the &?: below.
const value_type& smaller = size() < other.size() ? set_ : other.set_;
const value_type& bigger = size() < other.size() ? other.set_ : set_;
Adam Wardyński
committed
foreach (const UnicodeString& u, smaller) {
if (bigger.find(u) != bigger.end()) {
Adam Wardyński
committed
return true;
}
}
return false;
}
bool StrSet::is_subset_of(const StrSet &other) const
{
if (size() > other.size()) {
Adam Wardyński
committed
return false;
}
foreach (const UnicodeString& u, set_) {
if (other.set_.find(u) == other.set_.end()) {
Adam Wardyński
committed
return false;
}
}
return true;
}
std::string StrSet::var_repr(const std::string &var_name)
{
std::stringstream ss;