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

realm / realm-core / jorgen.edelbo_138

13 Mar 2024 08:41AM UTC coverage: 91.77% (-0.3%) from 92.078%
jorgen.edelbo_138

Pull #7356

Evergreen

jedelbo
Add ability to get path to modified collections in object notifications
Pull Request #7356: Add ability to get path to modified collections in object notifications

94532 of 174642 branches covered (54.13%)

118 of 163 new or added lines in 16 files covered. (72.39%)

765 existing lines in 41 files now uncovered.

242808 of 264584 relevant lines covered (91.77%)

5878961.32 hits per line

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

97.01
/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,247,718✔
45
    return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
4,247,718✔
46
}
4,247,718✔
47

48
char parse_xdigit(char ch)
49
{
4,245,024✔
50
    if (ch >= '0' && ch <= '9')
4,245,024✔
51
        return ch - '0';
2,682,285✔
52
    if (ch >= 'a' && ch <= 'f')
1,562,739✔
53
        return ch - 'a' + 10;
1,561,947✔
54
    if (ch >= 'A' && ch <= 'F')
792✔
55
        return ch - 'A' + 10;
792✔
UNCOV
56
    return char(-1);
×
UNCOV
57
}
×
58
} // anonymous namespace
59

60
namespace realm {
61

62
bool UUID::is_valid_string(StringData str) noexcept
63
{
132,813✔
64
    if (str.size() != size_of_uuid_string) {
132,813✔
65
        return false;
48✔
66
    }
48✔
67
    if (str[hyphen_pos_0] != hyphen || str[hyphen_pos_1] != hyphen || str[hyphen_pos_2] != hyphen ||
132,765✔
68
        str[hyphen_pos_3] != hyphen) {
132,756✔
69
        return false;
18✔
70
    }
18✔
71
    for (size_t i = 0; i < size_of_uuid_string; ++i) {
4,380,459✔
72
        if (i == hyphen_pos_0 || i == hyphen_pos_1 || i == hyphen_pos_2 || i == hyphen_pos_3) {
4,247,718✔
73
            ++i;
530,964✔
74
        }
530,964✔
75
        if (!::is_hex_digit(str[i])) {
4,247,718✔
76
            return false;
6✔
77
        }
6✔
78
    }
4,247,718✔
79
    return true;
132,744✔
80
}
132,747✔
81

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

66,306✔
90
    size_t j = 0;
132,657✔
91
    for (size_t i = 0; i < m_bytes.size(); i++) {
2,255,169✔
92
        if (j == hyphen_pos_0 || j == hyphen_pos_1 || j == hyphen_pos_2 || j == hyphen_pos_3) {
2,122,512✔
93
            j++;
530,628✔
94
        }
530,628✔
95
        m_bytes[i] = parse_xdigit(init[j++]) << 4;
2,122,512✔
96
        m_bytes[i] += parse_xdigit(init[j++]);
2,122,512✔
97
    }
2,122,512✔
98
}
132,657✔
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) {
41,607✔
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

123✔
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