| 160 | } |
| 161 | |
| 162 | bool parse(const std::string& s) |
| 163 | { |
| 164 | if (s.length() != 36) |
| 165 | return false; |
| 166 | |
| 167 | // Format validation. |
| 168 | const char *cp = s.data(); |
| 169 | for (size_t i = 0; i < 36; i++) { |
| 170 | if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) |
| 171 | { |
| 172 | if (*cp != '-') |
| 173 | return false; |
| 174 | } |
| 175 | else if (!isxdigit(*cp)) |
| 176 | return false; |
| 177 | ++cp; |
| 178 | } |
| 179 | |
| 180 | cp = s.data(); |
| 181 | m_data.time_low = strtoul(cp, NULL, 16); |
| 182 | m_data.time_mid = (uint16_t)strtoul(cp + 9, NULL, 16); |
| 183 | m_data.time_hi_and_version = (uint16_t)strtoul(cp + 14, NULL, 16); |
| 184 | m_data.clock_seq = (uint16_t)strtoul(cp + 19, NULL, 16); |
| 185 | |
| 186 | // Extract bytes as pairs of hex digits. |
| 187 | cp = s.data() + 24; |
| 188 | char buf[3]; |
| 189 | buf[2] = 0; |
| 190 | for (size_t i = 0; i < 6; i++) { |
| 191 | buf[0] = *cp++; |
| 192 | buf[1] = *cp++; |
| 193 | m_data.node[i] = (uint8_t)strtoul(buf, NULL, 16); |
| 194 | } |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | std::string unparse() const |
| 199 | { |