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

mendersoftware / mender / 950534094

pending completion
950534094

push

gitlab-ci

kacf
chore: Add `StartsWith` and `EndsWith` generic utility functions.

Also use the latter in `states.cpp`.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

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

4931 of 6276 relevant lines covered (78.57%)

196.18 hits per line

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

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

15
#include <common/common.hpp>
16
#include <common/error.hpp>
17

18
#include <cerrno>
19
#include <cstdlib>
20

21
namespace mender {
22
namespace common {
23

24
mender::common::expected::ExpectedLongLong StringToLongLong(const string &str, int base) {
280✔
25
        char *end;
26
        errno = 0;
280✔
27
        long long num = strtoll(str.c_str(), &end, base);
280✔
28
        if (errno != 0) {
280✔
29
                int int_error = errno;
×
30
                return expected::unexpected(mender::common::error::Error(
×
31
                        std::generic_category().default_error_condition(int_error), ""));
×
32
        }
33
        if (end != &*str.end()) {
280✔
34
                return expected::unexpected(mender::common::error::Error(
×
35
                        std::make_error_condition(errc::invalid_argument),
×
36
                        str + " had trailing non-numeric data"));
×
37
        }
38

39
        return num;
280✔
40
}
41

42
vector<string> SplitString(const string &str, const string &delim) {
84✔
43
        vector<string> ret;
84✔
44
        for (size_t begin = 0, end = str.find(delim);;) {
84✔
45
                if (end == string::npos) {
160✔
46
                        ret.push_back(string(str.begin() + begin, str.end()));
84✔
47
                        break;
84✔
48
                } else {
49
                        ret.push_back(string(str.begin() + begin, str.begin() + end));
76✔
50
                        begin = end + 1;
76✔
51
                        end = str.find(delim, begin);
76✔
52
                }
53
        }
54
        return ret;
84✔
55
}
56

57
string JoinStrings(const vector<string> &str, const string &delim) {
64✔
58
        string ret;
64✔
59
        auto s = str.begin();
64✔
60
        if (s == str.end()) {
64✔
61
                return ret;
×
62
        }
63
        ret += *s;
64✔
64
        s++;
64✔
65
        for (; s != str.end(); s++) {
379✔
66
                ret += delim;
315✔
67
                ret += *s;
315✔
68
        }
69
        return ret;
64✔
70
}
71

72
} // namespace common
73
} // namespace mender
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