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

realm / realm-core / jonathan.reams_3586

27 Jan 2025 07:39PM UTC coverage: 91.115% (-0.02%) from 91.132%
jonathan.reams_3586

Pull #8068

Evergreen

jbreams
pass shared_ptr directly
Pull Request #8068: Reuse transaction when refreshing subscriptions after commit

102728 of 181516 branches covered (56.59%)

6 of 6 new or added lines in 1 file covered. (100.0%)

87 existing lines in 12 files now uncovered.

217362 of 238557 relevant lines covered (91.12%)

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

48
char parse_xdigit(char ch)
49
{
4,245,120✔
50
    if (ch >= '0' && ch <= '9')
4,245,120✔
51
        return ch - '0';
2,681,271✔
52
    if (ch >= 'a' && ch <= 'f')
1,563,849✔
53
        return ch - 'a' + 10;
1,563,057✔
54
    if (ch >= 'A' && ch <= 'F')
792✔
55
        return ch - 'A' + 10;
792✔
UNCOV
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,816✔
64
    if (str.size() != size_of_uuid_string) {
132,816✔
65
        return false;
48✔
66
    }
48✔
67
    if (str[hyphen_pos_0] != hyphen || str[hyphen_pos_1] != hyphen || str[hyphen_pos_2] != hyphen ||
132,768✔
68
        str[hyphen_pos_3] != hyphen) {
132,768✔
69
        return false;
18✔
70
    }
18✔
71
    for (size_t i = 0; i < size_of_uuid_string; ++i) {
4,380,558✔
72
        if (i == hyphen_pos_0 || i == hyphen_pos_1 || i == hyphen_pos_2 || i == hyphen_pos_3) {
4,247,814✔
73
            ++i;
530,976✔
74
        }
530,976✔
75
        if (!::is_hex_digit(str[i])) {
4,247,814✔
76
            return false;
6✔
77
        }
6✔
78
    }
4,247,814✔
79
    return true;
132,744✔
80
}
132,750✔
81

82
UUID::UUID(StringData init)
83
    : m_bytes{}
66,267✔
84
{
132,666✔
85
    if (!is_valid_string(init)) {
132,666✔
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,660✔
91
    for (size_t i = 0; i < m_bytes.size(); i++) {
2,255,220✔
92
        if (j == hyphen_pos_0 || j == hyphen_pos_1 || j == hyphen_pos_2 || j == hyphen_pos_3) {
2,122,560✔
93
            j++;
530,640✔
94
        }
530,640✔
95
        m_bytes[i] = parse_xdigit(init[j++]) << 4;
2,122,560✔
96
        m_bytes[i] += parse_xdigit(init[j++]);
2,122,560✔
97
    }
2,122,560✔
98
}
132,660✔
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

© 2026 Coveralls, Inc