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

Razakhel / RaZ / 18062341368

27 Sep 2025 04:20PM UTC coverage: 74.05%. Remained the same
18062341368

push

github

Razakhel
[Utils/FilePath] Added custom formatters for FilePath

- This allows file paths to be formatted by a call to std::format()

6 of 13 new or added lines in 2 files covered. (46.15%)

8341 of 11264 relevant lines covered (74.05%)

1754.73 hits per line

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

98.21
/src/RaZ/Utils/FilePath.cpp
1
#include "RaZ/Utils/FilePath.hpp"
2
#include "RaZ/Utils/StrUtils.hpp"
3

4
#include <ostream>
5

6
namespace Raz {
7

8
namespace {
9

10
constexpr std::size_t recoverLastSeparatorPos(std::size_t firstSeparatorPos, std::size_t secondSeparatorPos, std::size_t invalidPos) {
73✔
11
  if (firstSeparatorPos == invalidPos)
73✔
12
    return secondSeparatorPos;
30✔
13

14
  // The first separator has been found
15

16
  if (secondSeparatorPos == invalidPos)
43✔
17
    return firstSeparatorPos;
40✔
18

19
  // Both separators have been found; the max (rightmost) position must be picked
20

21
  return std::max(firstSeparatorPos, secondSeparatorPos);
3✔
22
}
23

24
constexpr std::size_t recoverLastSeparatorPos(const std::string& pathStr) {
67✔
25
  const std::size_t lastSlashPos     = pathStr.find_last_of('/');
67✔
26
  const std::size_t lastBackslashPos = pathStr.find_last_of('\\');
67✔
27

28
  return recoverLastSeparatorPos(lastSlashPos, lastBackslashPos, std::string::npos);
67✔
29
}
30

31
constexpr std::size_t recoverLastSeparatorPos(const std::wstring& pathStr) {
6✔
32
  const std::size_t lastSlashPos     = pathStr.find_last_of(L'/');
6✔
33
  const std::size_t lastBackslashPos = pathStr.find_last_of(L'\\');
6✔
34

35
  return recoverLastSeparatorPos(lastSlashPos, lastBackslashPos, std::wstring::npos);
6✔
36
}
37

38
} // namespace
39

40
FilePath::FilePath(const char* pathStr)
292✔
41
#if defined(RAZ_PLATFORM_WINDOWS) && !defined(RAZ_PLATFORM_CYGWIN)
42
  : m_path{ StrUtils::toWide(pathStr) } {}
43
#else
44
  : m_path{ pathStr } {}
876✔
45
#endif
46

47
FilePath::FilePath(const wchar_t* pathStr)
7✔
48
#if defined(RAZ_PLATFORM_WINDOWS) && !defined(RAZ_PLATFORM_CYGWIN)
49
  : m_path{ pathStr } {}
50
#else
51
  : m_path{ StrUtils::toUtf8(pathStr) } {}
14✔
52
#endif
53

54
FilePath FilePath::recoverPathToFile(const std::string& pathStr) {
51✔
55
  return pathStr.substr(0, recoverLastSeparatorPos(pathStr) + 1);
51✔
56
}
57

58
FilePath FilePath::recoverPathToFile(const std::wstring& pathStr) {
2✔
59
  return pathStr.substr(0, recoverLastSeparatorPos(pathStr) + 1);
2✔
60
}
61

62
FilePath FilePath::recoverFileName(const std::string& pathStr, bool keepExtension) {
16✔
63
  std::string fileName = pathStr.substr(recoverLastSeparatorPos(pathStr) + 1);
16✔
64

65
  if (!keepExtension) {
16✔
66
    const std::size_t lastPointPos = fileName.find_last_of('.');
11✔
67

68
    if (lastPointPos != std::string::npos)
11✔
69
      fileName = fileName.erase(lastPointPos);
11✔
70
  }
71

72
  return fileName;
32✔
73
}
16✔
74

75
FilePath FilePath::recoverFileName(const std::wstring& pathStr, bool keepExtension) {
4✔
76
  std::wstring fileName = pathStr.substr(recoverLastSeparatorPos(pathStr) + 1);
4✔
77

78
  if (!keepExtension) {
4✔
79
    const std::size_t lastPointPos = fileName.find_last_of(L'.');
2✔
80

81
    if (lastPointPos != std::wstring::npos)
2✔
82
      fileName = fileName.erase(lastPointPos);
1✔
83
  }
84

85
  return fileName;
8✔
86
}
4✔
87

88
FilePath FilePath::recoverExtension(const std::string& pathStr) {
34✔
89
  const std::size_t lastPointPos = pathStr.find_last_of('.');
34✔
90

91
  if (lastPointPos == std::string::npos)
34✔
NEW
92
    return {};
×
93

94
  return pathStr.substr(lastPointPos + 1);
34✔
95
}
96

97
FilePath FilePath::recoverExtension(const std::wstring& pathStr) {
2✔
98
  const std::size_t lastPointPos = pathStr.find_last_of(L'.');
2✔
99

100
  if (lastPointPos == std::wstring::npos)
2✔
101
    return {};
1✔
102

103
  return pathStr.substr(lastPointPos + 1);
1✔
104
}
105

106
#if defined(RAZ_PLATFORM_WINDOWS) && !defined(RAZ_PLATFORM_CYGWIN)
107
std::string FilePath::toUtf8() const {
108
  return StrUtils::toUtf8(m_path);
109
}
110
#else
111
std::wstring FilePath::toWide() const {
16✔
112
  return StrUtils::toWide(m_path);
16✔
113
}
114
#endif
115

116
std::ostream& operator<<(std::ostream& stream, const FilePath& filePath) {
2✔
117
  stream << filePath.toUtf8();
2✔
118
  return stream;
2✔
119
}
120

121
std::wostream& operator<<(std::wostream& stream, const FilePath& filePath) {
1✔
122
  stream << filePath.toWide();
1✔
123
  return stream;
1✔
124
}
125

126
} // namespace Raz
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc