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

realm / realm-core / github_pull_request_281750

30 Oct 2023 03:37PM UTC coverage: 90.528% (-1.0%) from 91.571%
github_pull_request_281750

Pull #6073

Evergreen

jedelbo
Log free space and history sizes when opening file
Pull Request #6073: Merge next-major

95488 of 175952 branches covered (0.0%)

8973 of 12277 new or added lines in 149 files covered. (73.09%)

622 existing lines in 51 files now uncovered.

233503 of 257934 relevant lines covered (90.53%)

6533720.56 hits per line

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

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

48
char parse_xdigit(char ch)
49
{
4,241,184✔
50
    if (ch >= '0' && ch <= '9')
4,241,184✔
51
        return ch - '0';
2,680,920✔
52
    if (ch >= 'a' && ch <= 'f')
1,560,264✔
53
        return ch - 'a' + 10;
1,559,472✔
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,693✔
64
    if (str.size() != size_of_uuid_string) {
132,693✔
65
        return false;
48✔
66
    }
48✔
67
    if (str[hyphen_pos_0] != hyphen || str[hyphen_pos_1] != hyphen || str[hyphen_pos_2] != hyphen ||
132,645✔
68
        str[hyphen_pos_3] != hyphen) {
132,636✔
69
        return false;
18✔
70
    }
18✔
71
    for (size_t i = 0; i < size_of_uuid_string; ++i) {
4,376,499✔
72
        if (i == hyphen_pos_0 || i == hyphen_pos_1 || i == hyphen_pos_2 || i == hyphen_pos_3) {
4,243,878✔
73
            ++i;
530,484✔
74
        }
530,484✔
75
        if (!::is_hex_digit(str[i])) {
4,243,878✔
76
            return false;
6✔
77
        }
6✔
78
    }
4,243,878✔
79
    return true;
132,624✔
80
}
132,627✔
81

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

66,255✔
90
    size_t j = 0;
132,537✔
91
    for (size_t i = 0; i < m_bytes.size(); i++) {
2,253,129✔
92
        if (j == hyphen_pos_0 || j == hyphen_pos_1 || j == hyphen_pos_2 || j == hyphen_pos_3) {
2,120,592✔
93
            j++;
530,148✔
94
        }
530,148✔
95
        m_bytes[i] = parse_xdigit(init[j++]) << 4;
2,120,592✔
96
        m_bytes[i] += parse_xdigit(init[j++]);
2,120,592✔
97
    }
2,120,592✔
98
}
132,537✔
99

100
std::string UUID::to_string() const
101
{
2,937✔
102
    std::string ret(null_uuid_string);
2,937✔
103
    size_t mod_ndx = 0;
2,937✔
104
    for (size_t i = 0; i < m_bytes.size(); i++) {
49,929✔
105
        ret[mod_ndx++] = hex_digits[m_bytes[i] >> 4];
46,992✔
106
        ret[mod_ndx++] = hex_digits[m_bytes[i] & 0xf];
46,992✔
107
        if (mod_ndx == hyphen_pos_0 || mod_ndx == hyphen_pos_1 || mod_ndx == hyphen_pos_2 ||
46,992✔
108
            mod_ndx == hyphen_pos_3) {
42,564✔
109
            ++mod_ndx;
11,748✔
110
        }
11,748✔
111
    }
46,992✔
112
    return ret;
2,937✔
113
}
2,937✔
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
    char* bytes_ptr = &bytes[0];
246✔
120

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

127
} // 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