[英]Check if std::string is a valid uuid using boost
I want to check if a given string is a valid UUID using boost. 我想检查给定的字符串是否是使用boost的有效UUID。
This is what I have come up with by looking at the documentation on the boost website: 这是我通过查看boost网站上的文档得出的:
void validate_uuid(const std::string& value)
{
try
{
boost::uuids::string_generator stringGenerator;
(void)stringGenerator(value);
}
catch (const std::exception& ex)
{
// ...
}
}
However, this does not always work. 但是,这并不总是有效。
If I call the function with a string that is too short for a valid UUID, an exception is thrown as expected. 如果我使用对于有效UUID而言太短的字符串调用该函数,则会按预期抛出异常。 But if I call the function with an invalid UUID (eg
00000000-0000-0000-0000-00000000000K
) no exception is thrown. 但是,如果我使用无效的UUID(例如
00000000-0000-0000-0000-00000000000K
)调用该函数,则不会抛出异常。
Please can someone clarify why this is happening. 有人可以澄清为什么会这样。
Also, I've seen the use of boost::lexical_cast to read a string as a UUID as posted here . 另外,我已经看到使用boost :: lexical_cast将字符串读取为此处发布的UUID。 I'm wondering if I should follow that approach.
我想知道我是否应该采用这种方法。 Any advice appreciated.
任何建议表示赞赏
Since you already use boost you can use regex to check whether your string is a valid UUID 由于您已经使用了boost,因此您可以使用正则表达式来检查您的字符串是否是有效的UUID
Eg for UUID version 4 you could use the following code 例如,对于UUID版本4,您可以使用以下代码
bool validate_uuid(const std::string& s)
{
static const boost::regex e("[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}");
return regex_match(s, e); // note: case sensitive now
}
(As mentioned in this answer and in the wiki there should be a valid version digit and another "special" digit). (正如在这个答案和维基中所提到的那样,应该有一个有效的版本数字和另一个“特殊”数字)。
The code you had does nothing in terms of validation.
你所拥有的代码在验证方面没有任何作用。
Instead it generates a UUID based on the constant passed (like a hash function).
相反,它会根据传递的常量生成一个UUID(就像一个哈希函数)。
Looking closer I was mistaken. 仔细观察我错了。 The missing bit of validation appears to be a check on version:
遗漏的验证似乎是对版本的检查:
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/string_generator.hpp>
#include <iostream>
bool is_valid_uuid(std::string const& maybe_uuid, boost::uuids::uuid& result) {
using namespace boost::uuids;
try {
result = string_generator()(maybe_uuid);
return result.version() != uuid::version_unknown;
} catch(...) {
return false;
}
}
int main() {
std::string maybe_uuid;
std::cout << "Enter a UUID: ";
while (std::cin >> maybe_uuid)
{
boost::uuids::uuid result;
bool is_valid = is_valid_uuid(maybe_uuid, result);
std::cout << "\n'" << maybe_uuid << "' valid: " << std::boolalpha << is_valid << "\n";
if (is_valid)
std::cout << "Parsed value: " << result << "\n";
}
}
Sample output from Coliru: echo 00000000-0000-{0,4}000-0000-000000000000 $(uuidgen) "{$(uuidgen)}" | ./a.out
Coliru的样本输出:
echo 00000000-0000-{0,4}000-0000-000000000000 $(uuidgen) "{$(uuidgen)}" | ./a.out
echo 00000000-0000-{0,4}000-0000-000000000000 $(uuidgen) "{$(uuidgen)}" | ./a.out
: echo 00000000-0000-{0,4}000-0000-000000000000 $(uuidgen) "{$(uuidgen)}" | ./a.out
:
Enter a UUID:
'00000000-0000-0000-0000-000000000000' valid: false
'00000000-0000-4000-0000-000000000000' valid: true
Parsed value: 00000000-0000-4000-0000-000000000000
'a2c59f5c-6c9b-4800-afb8-282fc5e743cc' valid: true
Parsed value: a2c59f5c-6c9b-4800-afb8-282fc5e743cc
'{82a31d37-6fe4-4b80-b608-c63ec5ecd578}' valid: true
Parsed value: 82a31d37-6fe4-4b80-b608-c63ec5ecd578
This seems way easier: 这似乎更容易:
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
#include <sstream>
int main()
{
std::string t1("01234567-89ab-cdef-0123-456789abcdef");
std::string t2("Test UUID");
boost::uuids::uuid u;
std::istringstream iss(t1);
iss >> u;
if (iss.good())
std::cerr << "'" << t1 << "' is a good UUID\n";
else
std::cerr << "'" << t1 << "' is not a good UUID\n";
iss.str(t2);
iss >> u;
if (iss.good())
std::cerr << "'" << t2 << "' is a good UUID\n";
else
std::cerr << "'" << t2 << "' is not a good UUID\n";
return 0;
}
$ g++ -I/usr/local/include -std=c++11 test1.cpp
$ a.out
'01234567-89ab-cdef-0123-456789abcdef' is a good UUID
'Test UUID' is not a good UUID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.