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

realm / realm-core / 2516

24 Jul 2024 01:55AM UTC coverage: 91.07% (+0.05%) from 91.018%
2516

push

Evergreen

web-flow
RCORE-2205 Remove unused changeset compaction code (#7915)

102642 of 181450 branches covered (56.57%)

2 of 2 new or added lines in 2 files covered. (100.0%)

75 existing lines in 16 files now uncovered.

216274 of 237482 relevant lines covered (91.07%)

5685048.21 hits per line

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

92.86
/src/realm/util/to_string.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 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/util/to_string.hpp>
20

21
#include <realm/util/assert.hpp>
22
#include <realm/string_data.hpp>
23

24
#include <cstring>
25
#include <iomanip>
26
#include <locale>
27
#include <sstream>
28

29
namespace {
30
std::locale locale_classic = std::locale::classic();
31

32
template <typename T, typename = decltype(std::quoted(std::declval<T>()))>
33
void quoted(std::ostream& out, T&& str, int)
34
{
210✔
35
    out << std::quoted(str);
210✔
36
}
210✔
37
template <typename T>
38
void quoted(std::ostream& out, T&& str, ...)
39
{
40
    out << '"' << str << '"';
41
}
42
} // namespace
43

44
namespace realm {
45
namespace util {
46

47
Printable::Printable(StringData value)
48
    : m_type(Type::String)
801,708✔
49
{
1,648,797✔
50
    if (value.is_null()) {
1,648,797✔
51
        m_string = "<null>";
×
52
    }
×
53
    else {
1,648,797✔
54
        m_string = std::string_view(value.data(), value.size());
1,648,797✔
55
    }
1,648,797✔
56
}
1,648,797✔
57

58
void Printable::print(std::ostream& out, bool quote) const
59
{
31,153,659✔
60
    switch (m_type) {
31,153,659✔
61
        case Printable::Type::Bool:
320,958✔
62
            out << (m_uint ? "true" : "false");
320,958✔
63
            break;
320,958✔
64
        case Printable::Type::Uint:
19,141,074✔
65
            out << m_uint;
19,141,074✔
66
            break;
19,141,074✔
67
        case Printable::Type::Int:
3,797,226✔
68
            out << m_int;
3,797,226✔
69
            break;
3,797,226✔
70
        case Printable::Type::Double:
10,116✔
71
            out << m_double;
10,116✔
72
            break;
10,116✔
73
        case Printable::Type::String:
6,778,200✔
74
            if (quote)
6,778,200✔
75
                quoted(out, m_string, 0);
210✔
76
            else
6,777,990✔
77
                out << m_string;
6,777,990✔
78
            break;
6,778,200✔
79
        case Printable::Type::Callback:
1,122,708✔
80
            m_callback.fn(out, m_callback.data);
1,122,708✔
81
            break;
1,122,708✔
82
    }
31,153,659✔
83
}
31,153,659✔
84

85
void Printable::print_all(std::ostream& out, const std::initializer_list<Printable>& values, bool quote)
86
{
18✔
87
    if (values.size() == 0)
18✔
88
        return;
6✔
89

90
    bool is_first = true;
12✔
91
    out << " [";
12✔
92
    for (auto&& value : values) {
36✔
93
        if (!is_first)
36✔
94
            out << ", ";
24✔
95
        is_first = false;
36✔
96
        value.print(out, quote);
36✔
97
    }
36✔
98
    out << "]";
12✔
99
}
12✔
100

101
std::string Printable::str() const
102
{
1,296,456✔
103
    std::ostringstream ss;
1,296,456✔
104
    ss.imbue(locale_classic);
1,296,456✔
105
    ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
1,296,456✔
106
    print(ss, true);
1,296,456✔
107
    return ss.str();
1,296,456✔
108
}
1,296,456✔
109

110
void format(std::ostream& os, const char* fmt, std::initializer_list<Printable> values)
111
{
13,380,252✔
112
    while (*fmt) {
43,276,728✔
113
        auto next = strchr(fmt, '%');
36,529,473✔
114

115
        // emit the rest of the format string if there are no more percents
116
        if (!next) {
36,529,473✔
117
            os << fmt;
6,632,997✔
118
            break;
6,632,997✔
119
        }
6,632,997✔
120

121
        // emit everything up to the next percent
122
        os.write(fmt, next - fmt);
29,896,476✔
123
        ++next;
29,896,476✔
124
        REALM_ASSERT(*next);
29,896,476✔
125

126
        // %% produces a single escaped %
127
        if (*next == '%') {
29,896,476✔
UNCOV
128
            os << '%';
×
UNCOV
129
            fmt = next + 1;
×
UNCOV
130
            continue;
×
UNCOV
131
        }
×
132
        REALM_ASSERT(isdigit(*next));
29,896,476✔
133

134
        // The const_cast is safe because stroul does not actually modify
135
        // the pointed-to string, but it lacks a const overload
136
        auto index = strtoul(next, const_cast<char**>(&fmt), 10) - 1;
29,896,476✔
137
        REALM_ASSERT(index < values.size());
29,896,476✔
138
        (values.begin() + index)->print(os, false);
29,896,476✔
139
    }
29,896,476✔
140
}
13,380,252✔
141

142
std::string format(const char* fmt, std::initializer_list<Printable> values)
143
{
13,371,564✔
144
    std::stringstream ss;
13,371,564✔
145
    ss.imbue(locale_classic);
13,371,564✔
146
    format(ss, fmt, values);
13,371,564✔
147
    return ss.str();
13,371,564✔
148
}
13,371,564✔
149

150
} // namespace util
151
} // 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