Skip to content

Commit 2ecd0ba

Browse files
committed
update string utils
1 parent 687c8c1 commit 2ecd0ba

File tree

9 files changed

+86
-87
lines changed

9 files changed

+86
-87
lines changed

Include/SevenBit/DI/Details/Utils/Formatter.hpp renamed to Include/SevenBit/DI/Details/Helpers/Formatter.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "SevenBit/DI/LibraryConfig.hpp"
77

8-
#include "SevenBit/DI/Details/Utils/String.hpp"
8+
#include "SevenBit/DI/Details/Utils/StringUtils.hpp"
99

1010
namespace sb::di::details
1111
{
@@ -16,7 +16,7 @@ namespace sb::di::details
1616
std::string result;
1717

1818
public:
19-
explicit Formatter(std::string_view formatString) : formatString(formatString)
19+
explicit Formatter(const std::string_view formatString) : formatString(formatString)
2020
{
2121
result.reserve(formatString.size());
2222
}
@@ -41,7 +41,7 @@ namespace sb::di::details
4141
{
4242
throw std::runtime_error("Invalid format string '{' should end with '}'");
4343
}
44-
result += String::toString(std::forward<Arg>(arg), formatString.substr(start, end - start));
44+
result += StringUtils::toString(std::forward<Arg>(arg), formatString.substr(start, end - start));
4545
current = end + 1;
4646
return true;
4747
}

Include/SevenBit/DI/Details/Utils/Container.hpp renamed to Include/SevenBit/DI/Details/Utils/ContainerUtils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace sb::di::details
88
{
9-
struct Container
9+
struct ContainerUtils
1010
{
1111
template <class ForwardIt, class UnaryPredicate>
1212
static ForwardIt removeIf(ForwardIt first, ForwardIt last, UnaryPredicate &&p)

Include/SevenBit/DI/Details/Utils/Format.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
#include "SevenBit/DI/LibraryConfig.hpp"
77

8-
#include "SevenBit/DI/Details/Utils/Formatter.hpp"
8+
#include "SevenBit/DI/Details/Helpers/Formatter.hpp"
99

1010
namespace sb::di::details
1111
{
12-
class Format
12+
struct Format
1313
{
14-
public:
15-
template <class... Args> static std::string fmt(std::string_view formatString, Args &&...args)
14+
template <class... Args> static std::string fmt(const std::string_view formatString, Args &&...args)
1615
{
1716
Formatter formatter{formatString};
1817
formatter.format(args...);

Include/SevenBit/DI/Details/Utils/Impl/String.hpp

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include "SevenBit/DI/LibraryConfig.hpp"
4+
5+
#include "SevenBit/DI/Details/Utils/StringUtils.hpp"
6+
7+
namespace sb::di::details
8+
{
9+
INLINE std::string StringUtils::toString(const char *arg, std::string_view fmt)
10+
{
11+
if (!fmt.empty())
12+
{
13+
const auto format = makeArgFmt(fmt, "s");
14+
return dataToString(format.c_str(), const_cast<char *>(arg));
15+
}
16+
return std::string(arg);
17+
}
18+
19+
INLINE std::string StringUtils::toString(const std::string &arg, std::string_view fmt)
20+
{
21+
if (!fmt.empty())
22+
{
23+
const auto format = makeArgFmt(fmt, "s");
24+
return dataToString(format.c_str(), const_cast<std::string &>(arg).c_str());
25+
}
26+
return arg;
27+
}
28+
29+
INLINE std::string StringUtils::toString(std::string &&arg, std::string_view fmt)
30+
{
31+
if (!fmt.empty())
32+
{
33+
const auto format = makeArgFmt(fmt, "s");
34+
return dataToString(format.c_str(), arg.c_str());
35+
}
36+
return std::move(arg);
37+
}
38+
39+
INLINE std::string StringUtils::toString(std::string_view arg, std::string_view fmt)
40+
{
41+
std::string strArg{arg};
42+
if (!fmt.empty())
43+
{
44+
const auto format = makeArgFmt(fmt, "s");
45+
return dataToString(format.c_str(), strArg.c_str());
46+
}
47+
return strArg;
48+
}
49+
50+
INLINE std::string StringUtils::toString(int arg, std::string_view fmt) { return toString(arg, fmt, "d"); }
51+
INLINE std::string StringUtils::toString(long arg, std::string_view fmt) { return toString(arg, fmt, "ld"); }
52+
INLINE std::string StringUtils::toString(long long arg, std::string_view fmt) { return toString(arg, fmt, "lld"); }
53+
INLINE std::string StringUtils::toString(unsigned arg, std::string_view fmt) { return toString(arg, fmt, "u"); }
54+
INLINE std::string StringUtils::toString(unsigned long arg, std::string_view fmt) { return toString(arg, fmt, "lu"); }
55+
INLINE std::string StringUtils::toString(unsigned long long arg, std::string_view fmt)
56+
{
57+
return toString(arg, fmt, "llu");
58+
}
59+
INLINE std::string StringUtils::toString(float arg, std::string_view fmt) { return toString(arg, fmt, "f"); }
60+
INLINE std::string StringUtils::toString(double arg, std::string_view fmt) { return toString(arg, fmt, "f"); }
61+
INLINE std::string StringUtils::toString(long double arg, std::string_view fmt) { return toString(arg, fmt, "Lf"); }
62+
63+
INLINE std::string StringUtils::makeArgFmt(std::string_view fmt, std::string_view type)
64+
{
65+
return "%" + std::string{fmt} + std::string{type};
66+
}
67+
} // namespace sb::di::details

Include/SevenBit/DI/Details/Utils/String.hpp renamed to Include/SevenBit/DI/Details/Utils/StringUtils.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
namespace sb::di::details
99
{
10-
class EXPORT String
10+
struct EXPORT StringUtils
1111
{
12-
public:
1312
static std::string toString(const char *arg, std::string_view fmt = "");
1413
static std::string toString(const std::string &arg, std::string_view fmt = "");
1514
static std::string toString(std::string &&arg, std::string_view fmt = "");
@@ -32,11 +31,11 @@ namespace sb::di::details
3231
return std::string(buffer, size);
3332
}
3433

35-
template <class Number> static std::string toString(Number arg, std::string_view fmt, std::string_view type)
34+
template <class Number> static std::string toString(Number arg, const std::string_view fmt, const std::string_view type)
3635
{
3736
if (!fmt.empty())
3837
{
39-
auto format = makeArgFmt(fmt, type);
38+
const auto format = makeArgFmt(fmt, type);
4039
return dataToString(format.c_str(), arg);
4140
}
4241
return std::to_string(arg);
@@ -47,5 +46,5 @@ namespace sb::di::details
4746
} // namespace sb::di::details
4847

4948
#ifdef _7BIT_DI_ADD_IMPL
50-
#include "SevenBit/DI/Details/Utils/Impl/String.hpp"
49+
#include "SevenBit/DI/Details/Utils/Impl/StringUtils.hpp"
5150
#endif

Include/SevenBit/DI/Exceptions.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,27 @@ namespace sb::di
4040

4141
struct EXPORT ServiceNotFoundException : InjectorException
4242
{
43-
ServiceNotFoundException(TypeId typeIndex, const std::string &reason);
43+
ServiceNotFoundException(TypeId typeId, const std::string &reason);
4444
};
4545

4646
struct EXPORT ServiceAlreadyRegisteredException : InjectorException
4747
{
48-
explicit ServiceAlreadyRegisteredException(TypeId typeIndex);
48+
explicit ServiceAlreadyRegisteredException(TypeId typeId);
4949
};
5050

5151
struct EXPORT ServiceLifeTimeMismatchException : InjectorException
5252
{
53-
ServiceLifeTimeMismatchException(TypeId typeIndex, TypeId interface);
53+
ServiceLifeTimeMismatchException(TypeId typeId, TypeId interface);
5454
};
5555

5656
struct EXPORT ServiceAliasMismatchException : InjectorException
5757
{
58-
ServiceAliasMismatchException(TypeId typeIndex, TypeId interface, bool shouldBeAlias);
58+
ServiceAliasMismatchException(TypeId typeId, TypeId interface, bool shouldBeAlias);
5959
};
6060

6161
struct EXPORT CircularDependencyException : InjectorException
6262
{
63-
explicit CircularDependencyException(TypeId typeIndex);
63+
explicit CircularDependencyException(TypeId typeId);
6464
};
6565
} // namespace sb::di
6666

Include/SevenBit/DI/ServiceCollection.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "SevenBit/DI/LibraryConfig.hpp"
99

10-
#include "SevenBit/DI/Details/Utils/Container.hpp"
10+
#include "SevenBit/DI/Details/Utils/ContainerUtils.hpp"
1111
#include "SevenBit/DI/ServiceDescriber.hpp"
1212
#include "SevenBit/DI/ServiceLifeTimes.hpp"
1313
#include "SevenBit/DI/ServiceProvider.hpp"
@@ -348,7 +348,7 @@ namespace sb::di
348348
*/
349349
template <class TPred> std::size_t removeIf(TPred pred)
350350
{
351-
auto it = details::Container::removeIf(begin(), end(), std::move(pred));
351+
auto it = details::ContainerUtils::removeIf(begin(), end(), std::move(pred));
352352
auto r = std::distance(it, end());
353353
removeRange(it, end());
354354
return r;

Source/Source.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
#include <SevenBit/DI/Details/Helpers/Impl/ScopedGuard.hpp>
1313
#include <SevenBit/DI/Details/Utils/Impl/RequireDescriptor.hpp>
1414
#include <SevenBit/DI/Details/Utils/Impl/RequireInstance.hpp>
15+
#include <SevenBit/DI/Details/Utils/Impl/StringUtils.hpp>
1516
#include <SevenBit/DI/Impl/Exceptions.hpp>
1617
#include <SevenBit/DI/Impl/ServiceCollection.hpp>

0 commit comments

Comments
 (0)