Created
January 8, 2012 13:10
-
-
Save bolero-MURAKAMI/1578326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SPROUT_CONFIG_SUPPORT_TEMPORARY_CONTAINER_ITERATION | |
#include <iostream> | |
#include <sprout/string.hpp> | |
#include <sprout/weed.hpp> | |
int main() { | |
using sprout::weed::hex8f; // matching a 8-bit hex | |
using sprout::weed::repeat; // repeat directive | |
using sprout::weed::lim; // set the limits of repeat (because variable length containers cannot handling) | |
using sprout::weed::eoi; // matching EOI | |
using sprout::weed::parse; // parsing function | |
// generate uuid parser | |
static constexpr auto unseparated_uuid_p | |
= repeat[lim<16>(hex8f)] | |
; | |
static constexpr auto separated_uuid_p | |
= repeat[lim<4>(hex8f)] | |
>> '-' >> repeat[lim<2>(hex8f)] | |
>> '-' >> repeat[lim<2>(hex8f)] | |
>> '-' >> repeat[lim<2>(hex8f)] | |
>> '-' >> repeat[lim<6>(hex8f)] | |
; | |
static constexpr auto unbracketed_uuid_p | |
= unseparated_uuid_p | |
| separated_uuid_p | |
; | |
static constexpr auto bracketed_uuid_p | |
= '{' >> unbracketed_uuid_p >> '}' | |
; | |
static constexpr auto any_uuid_p | |
= unbracketed_uuid_p | |
| bracketed_uuid_p | |
; | |
static constexpr auto uuid_p | |
= any_uuid_p >> eoi | |
; | |
// compile-time string (like a std::string) | |
static constexpr auto s = sprout::to_string(U"{550E8400-E29B-41D4-A716-446655440000}"); | |
// run parsing | |
static constexpr auto parsed = parse(s.begin(), s.end(), uuid_p); | |
static_assert(parsed.success(), ""); // parsing succeeded | |
static_assert(parsed.current() == s.end(), ""); // full matched | |
// get attribute | |
static constexpr auto uuid = parsed.attr(); | |
// verification | |
static_assert(uuid[0] == 0x55, ""); | |
static_assert(uuid[1] == 0x0E, ""); | |
static_assert(uuid[2] == 0x84, ""); | |
static_assert(uuid[3] == 0x00, ""); | |
static_assert(uuid[4] == 0xE2, ""); | |
static_assert(uuid[5] == 0x9B, ""); | |
static_assert(uuid[6] == 0x41, ""); | |
static_assert(uuid[7] == 0xD4, ""); | |
static_assert(uuid[8] == 0xA7, ""); | |
static_assert(uuid[9] == 0x16, ""); | |
static_assert(uuid[10] == 0x44, ""); | |
static_assert(uuid[11] == 0x66, ""); | |
static_assert(uuid[12] == 0x55, ""); | |
static_assert(uuid[13] == 0x44, ""); | |
static_assert(uuid[14] == 0x00, ""); | |
static_assert(uuid[15] == 0x00, ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment