1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
// Copyright Joakim Karlsson & Kim Gr�sman 2010-2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef IGLOO_COLLECTIONCONSTRAINTEVALUATOR_H
#define IGLOO_COLLECTIONCONSTRAINTEVALUATOR_H
#include <string>
#include "../invalidexpressionexception.h"
namespace snowhouse
{
template<typename ConstraintListType, typename ActualType>
struct CollectionConstraintEvaluator
{
static unsigned int Evaluate(const ConstraintOperator& op,
ConstraintListType& expression, ResultStack& result,
OperatorStack& operators, const ActualType& actual)
{
ConstraintOperator::EvaluateOperatorsWithLessOrEqualPrecedence(op,
operators, result);
unsigned int passed_elements = 0;
typename ActualType::const_iterator it;
for(it = actual.begin(); it != actual.end(); it++)
{
if(ConstraintOperator::EvaluateElementAgainstRestOfExpression(expression,
*it))
{
passed_elements++;
}
}
return passed_elements;
}
};
struct StringLineParser
{
static void Parse(const std::string& str, std::vector<std::string>& res)
{
size_t start = 0;
size_t newline = FindNewline(str, start);
while(newline != std::string::npos)
{
StoreLine(str, start, newline, res);
start = MoveToNextLine(str, newline);
newline = FindNewline(str, start);
}
if(start < str.size())
{
StoreLine(str, start, std::string::npos, res);
}
}
private:
static size_t FindNewline(const std::string& str, size_t start)
{
return str.find_first_of("\r\n", start);
}
static void StoreLine(const std::string& str, size_t start, size_t end,
std::vector<std::string>& res)
{
std::string line = str.substr(start, end - start);
res.push_back(line);
}
static size_t MoveToNextLine(const std::string& str, size_t newline)
{
if(str.find("\r\n", newline) == newline)
{
return newline + 2;
}
if(str.find("\n", newline) == newline)
{
return newline + 1;
}
if(str.find("\r", newline) == newline)
{
return newline + 1;
}
std::ostringstream stm;
stm << "This string seems to contain an invalid line ending at position "
<< newline << ":\n" << str << std::endl;
throw InvalidExpressionException(stm.str());
}
};
template<typename ConstraintListType>
struct CollectionConstraintEvaluator<ConstraintListType, std::string>
{
static unsigned int Evaluate(const ConstraintOperator& op,
ConstraintListType& expression, ResultStack& result,
OperatorStack& operators, const std::string& actual)
{
std::vector<std::string> lines;
StringLineParser::Parse(actual, lines);
return CollectionConstraintEvaluator<ConstraintListType, std::vector<std::string> >::Evaluate(op, expression, result, operators, lines);
}
};
}
#endif
|