• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / jonathan.reams_3390

31 Jul 2024 07:00PM UTC coverage: 91.105% (-0.01%) from 91.116%
jonathan.reams_3390

Pull #7938

Evergreen

jbreams
RCORE-2212 Make apply-to-state tool handle all batch states properly
Pull Request #7938: RCORE-2212 Make apply-to-state tool handle all batch states properly

102768 of 181570 branches covered (56.6%)

216827 of 237996 relevant lines covered (91.11%)

5898131.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.48
/src/realm/uuid.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2020 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <realm/uuid.hpp>
20
#include <realm/string_data.hpp>
21
#include <realm/util/assert.hpp>
22
#include <realm/util/base64.hpp>
23

24
#include <atomic>
25
#include <cctype>
26

27
namespace {
28
constexpr char hex_digits[] = "0123456789abcdef";
29
constexpr size_t size_of_uuid_string = 36;
30
constexpr char null_uuid_string[] = "00000000-0000-0000-0000-000000000000";
31
static_assert(sizeof(realm::UUID::UUIDBytes) == 16, "A change to the size of UUID is a file format breaking change");
32
static_assert(realm::UUID::num_bytes == 16, "A change to the size of UUID is a file format breaking change");
33
static_assert(sizeof(null_uuid_string) - 1 == (sizeof(realm::UUID::UUIDBytes) * 2) + 4,
34
              "size mismatch on uuid content and it's string representation");
35
static_assert(sizeof(null_uuid_string) - 1 == size_of_uuid_string,
36
              "size mismatch on uuid content and it's string representation");
37
constexpr size_t hyphen_pos_0 = 8;
38
constexpr size_t hyphen_pos_1 = 13;
39
constexpr size_t hyphen_pos_2 = 18;
40
constexpr size_t hyphen_pos_3 = 23;
41
constexpr char hyphen = '-';
42

43
bool is_hex_digit(unsigned char c)
44
{
4,243,494✔
45
    return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
4,243,494✔
46
}
4,243,494✔
47

48
char parse_xdigit(char ch)
49
{
4,240,800✔
50
    if (ch >= '0' && ch <= '9')
4,240,800✔
51
        return ch - '0';
2,678,322✔
52
    if (ch >= 'a' && ch <= 'f')
1,562,478✔
53
        return ch - 'a' + 10;
1,561,686✔
54
    if (ch >= 'A' && ch <= 'F')
792✔
55
        return ch - 'A' + 10;
792✔
56
    return char(-1);
×
57
}
792✔
58
} // anonymous namespace
59

60
namespace realm {
61

62
bool UUID::is_valid_string(StringData str) noexcept
63
{
132,681✔
64
    if (str.size() != size_of_uuid_string) {
132,681✔
65
        return false;
48✔
66
    }
48✔
67
    if (str[hyphen_pos_0] != hyphen || str[hyphen_pos_1] != hyphen || str[hyphen_pos_2] != hyphen ||
132,633✔
68
        str[hyphen_pos_3] != hyphen) {
132,633✔
69
        return false;
18✔
70
    }
18✔
71
    for (size_t i = 0; i < size_of_uuid_string; ++i) {
4,376,103✔
72
        if (i == hyphen_pos_0 || i == hyphen_pos_1 || i == hyphen_pos_2 || i == hyphen_pos_3) {
4,243,494✔
73
            ++i;
530,436✔
74
        }
530,436✔
75
        if (!::is_hex_digit(str[i])) {
4,243,494✔
76
            return false;
6✔
77
        }
6✔
78
    }
4,243,494✔
79
    return true;
132,609✔
80
}
132,615✔
81

82
UUID::UUID(StringData init)
83
    : m_bytes{}
66,306✔
84
{
132,531✔
85
    if (!is_valid_string(init)) {
132,531✔
86
        throw InvalidUUIDString{
6✔
87
            util::format("Invalid string format encountered when constructing a UUID: '%1'.", init)};
6✔
88
    }
6✔
89

90
    size_t j = 0;
132,525✔
91
    for (size_t i = 0; i < m_bytes.size(); i++) {
2,252,925✔
92
        if (j == hyphen_pos_0 || j == hyphen_pos_1 || j == hyphen_pos_2 || j == hyphen_pos_3) {
2,120,400✔
93
            j++;
530,100✔
94
        }
530,100✔
95
        m_bytes[i] = parse_xdigit(init[j++]) << 4;
2,120,400✔
96
        m_bytes[i] += parse_xdigit(init[j++]);
2,120,400✔
97
    }
2,120,400✔
98
}
132,525✔
99

100
std::string UUID::to_string() const
101
{
2,871✔
102
    std::string ret(null_uuid_string);
2,871✔
103
    size_t mod_ndx = 0;
2,871✔
104
    for (size_t i = 0; i < m_bytes.size(); i++) {
48,807✔
105
        ret[mod_ndx++] = hex_digits[m_bytes[i] >> 4];
45,936✔
106
        ret[mod_ndx++] = hex_digits[m_bytes[i] & 0xf];
45,936✔
107
        if (mod_ndx == hyphen_pos_0 || mod_ndx == hyphen_pos_1 || mod_ndx == hyphen_pos_2 ||
45,936✔
108
            mod_ndx == hyphen_pos_3) {
45,936✔
109
            ++mod_ndx;
11,484✔
110
        }
11,484✔
111
    }
45,936✔
112
    return ret;
2,871✔
113
}
2,871✔
114

115
std::string UUID::to_base64() const
116
{
246✔
117
    char bytes[UUID::num_bytes];
246✔
118
    std::copy(std::begin(m_bytes), std::end(m_bytes), std::begin(bytes));
246✔
119

120
    std::string encode_buffer;
246✔
121
    encode_buffer.resize(util::base64_encoded_size(UUID::num_bytes));
246✔
122
    util::base64_encode(bytes, encode_buffer);
246✔
123
    return encode_buffer;
246✔
124
}
246✔
125

126
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc