forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringContainsValidator.cpp
68 lines (61 loc) · 2.11 KB
/
StringContainsValidator.cpp
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/StringContainsValidator.h"
#include <memory>
namespace Mantid {
namespace Kernel {
/** Constructor
*/
StringContainsValidator::StringContainsValidator() { m_requiredStrings = std::vector<std::string>(); }
/** Constructor with required sub strings
*/
StringContainsValidator::StringContainsValidator(const std::vector<std::string> &strings) {
m_requiredStrings = strings;
}
/**
* @return A clone of the current state of the validator
*/
IValidator_sptr StringContainsValidator::clone() const { return std::make_shared<StringContainsValidator>(*this); }
/**
* @param strings The vector of sub strings that need to be included to pass
* validation
*/
void StringContainsValidator::setRequiredStrings(const std::vector<std::string> &strings) {
m_requiredStrings = strings;
}
/**
* @param value A string to check if it contains required sub strings
* @return An empty string if the value is valid or an string containing
* a description of the error otherwise
*/
std::string StringContainsValidator::checkValidity(const std::string &value) const {
std::string error;
if (m_requiredStrings.empty() && !value.empty()) {
return "";
} else {
if (value.empty()) {
error += "A value must be entered for this parameter.";
} else {
size_t validityCount = 0;
const size_t total = m_requiredStrings.size();
for (size_t i = 0; i < total; i++) {
auto position = value.find(m_requiredStrings.at(i));
if (position != std::string::npos) {
validityCount++;
}
}
if (validityCount != total) {
error += "Error not all the required substrings were contained within "
"the input '" +
value + "'.";
}
}
}
return error;
}
} // namespace Kernel
} // namespace Mantid