Exemple #1
0
size_t
UUID::SetfromCString (const char *cstr)
{
    if (cstr == NULL)
        return 0;

    uint32_t uuid_byte_idx = 0;
    const char *p = cstr;

    // Skip leading whitespace characters
    while (isspace(*p))
        ++p;

    // Try and decode a UUID
    while (*p != '\0')
    {
        if (isxdigit(*p) && isxdigit(p[1]))
        {
            int hi_nibble = xdigit_to_int(p[0]);
            int lo_nibble = xdigit_to_int(p[1]);
            // Translate the two hex nibble characters into a byte
            m_uuid[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;

            // Skip both hex digits
            p += 2;

            // Increment the byte that we are decoding within the UUID value
            // and break out if we are done
            if (++uuid_byte_idx == 16)
                break;
        }
        else if (*p == '-')
        {
            // Skip dashes
            p++;
        }
        else
        {
            // UUID values can only consist of hex characters and '-' chars
            return 0;
        }
    }
    // If we successfully decoded a UUID, return the amount of characters that
    // were consumed
    if (uuid_byte_idx == 16)
        return p - cstr;

    // Else return zero to indicate we were not able to parse a UUID value
    return 0;
}
Exemple #2
0
size_t
UUID::DecodeUUIDBytesFromCString (const char *p, ValueType &uuid_bytes, const char **end, uint32_t num_uuid_bytes)
{
    size_t uuid_byte_idx = 0;
    if (p)
    {
        while (*p)
        {
            if (isxdigit(p[0]) && isxdigit(p[1]))
            {
                int hi_nibble = xdigit_to_int(p[0]);
                int lo_nibble = xdigit_to_int(p[1]);
                // Translate the two hex nibble characters into a byte
                uuid_bytes[uuid_byte_idx] = (hi_nibble << 4) + lo_nibble;
                
                // Skip both hex digits
                p += 2;
                
                // Increment the byte that we are decoding within the UUID value
                // and break out if we are done
                if (++uuid_byte_idx == num_uuid_bytes)
                    break;
            }
            else if (*p == '-')
            {
                // Skip dashes
                p++;
            }
            else
            {
                // UUID values can only consist of hex characters and '-' chars
                break;
            }
        }
    }
    if (end)
        *end = p;
    // Clear trailing bytes to 0.
    for (uint32_t i = uuid_byte_idx; i < sizeof(ValueType); i++)
        uuid_bytes[i] = 0;
    return uuid_byte_idx;
}
Exemple #3
0
	bool parser::parse_string(context& ctx, string_type& value, IteratorType& ch, IteratorType end)
	{
		ctx.clear();

		if (!check_char(ctx, '"', ch, end))
		{
			return false;
		}

		while (ch != end)
		{
			if (std::iscntrl(*ch))
			{
				return false;
			}

			switch (*ch)
			{
				case '"':
					{
						// The string ends.
						++ch;

						value = ctx.str();

						return true;
					}

				case '\\':
					{
						// An escape character was provided.
						++ch;

						if (ch == end)
						{
							return false;
						}

						switch (*ch)
						{
							case '"':
								ctx.push_char('"');
								++ch;
								break;

							case '\\':
								ctx.push_char('\\');
								++ch;
								break;

							case '/':
								ctx.push_char('/');
								++ch;
								break;

							case 'b':
								ctx.push_char('\b');
								++ch;
								break;

							case 'n':
								ctx.push_char('\n');
								++ch;
								break;

							case 'f':
								ctx.push_char('\f');
								++ch;
								break;

							case 'r':
								ctx.push_char('\r');
								++ch;
								break;

							case 't':
								ctx.push_char('\t');
								++ch;
								break;

							case 'u':
								{
									++ch;

									uint16_t codepoint = 0x0000;

									for (size_t i = 0; i < 4; ++i)
									{
										if (ch == end)
										{
											return false;
										}

										if (!std::isxdigit(*ch))
										{
											return false;
										}

										codepoint *= 16;
										codepoint += xdigit_to_int(*ch);

										++ch;
									}

									ctx.push_codepoint(codepoint);

									break;
								}

							default:
								return false;
						}

						break;
					}

				default:
					{
						ctx.push_char(*ch);

						++ch;
					}
			}
		}

		return false;
	}