Skip to content
Snippets Groups Projects
strset.cpp 2.25 KiB
Newer Older
ilor's avatar
ilor committed
#include <libwccl/values/strset.h>
#include <libpwrutils/foreach.h>
#include <libpwrutils/util.h>
#include <sstream>
#include <boost/algorithm/string.hpp>
ilor's avatar
ilor committed

namespace Wccl {

const char* StrSet::type_name = "StrSet";

std::string StrSet::to_raw_string() const
{
	std::stringstream ss;
	value_type::const_iterator it = set_.begin();
		ss << '\"';
		std::string item = PwrNlp::to_utf8(*it);
		boost::algorithm::replace_all(item, "\\", "\\\\");
		boost::algorithm::replace_all(item, "\"", "\\\"");
		ss << item;
		ss << '\"';
ilor's avatar
ilor committed
		}
	}
ilor's avatar
ilor committed
	return ss.str();
}

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;
}

	if (empty() || other.empty()) {
	//We just want to check if there is an intersection, no
	//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_;
		if (bigger.find(u) != bigger.end()) {
bool StrSet::is_subset_of(const StrSet &other) const
{
	if (size() > other.size()) {
		if (other.set_.find(u) == other.set_.end()) {
std::string StrSet::var_repr(const std::string &var_name)
{
	std::stringstream ss;
	ss << "$s:" << var_name;
ilor's avatar
ilor committed
} /* end ns Wccl */