Remove some unnecessary casts
[openal-soft.git] / common / alstring.h
blobc039361367243055f43c97420d95edca2cba0a90
1 #ifndef AL_STRING_H
2 #define AL_STRING_H
4 #include <algorithm>
5 #include <cstddef>
6 #include <limits>
7 #include <string>
8 #include <string_view>
11 namespace al {
13 template<typename ...Ts>
14 [[nodiscard]] constexpr
15 auto sizei(const std::basic_string_view<Ts...> str) noexcept -> int
16 { return static_cast<int>(std::min<std::size_t>(str.size(), std::numeric_limits<int>::max())); }
18 template<typename ...Ts>
19 [[nodiscard]] constexpr
20 auto sizei(const std::basic_string<Ts...> &str) noexcept -> int
21 { return static_cast<int>(std::min<std::size_t>(str.size(), std::numeric_limits<int>::max())); }
24 [[nodiscard]]
25 constexpr bool contains(const std::string_view str0, const std::string_view str1) noexcept
26 { return str0.find(str1) != std::string_view::npos; }
28 [[nodiscard]]
29 constexpr bool starts_with(const std::string_view str0, const std::string_view str1) noexcept
30 { return str0.substr(0, std::min(str0.size(), str1.size())) == str1; }
32 [[nodiscard]]
33 constexpr bool ends_with(const std::string_view str0, const std::string_view str1) noexcept
34 { return str0.substr(str0.size() - std::min(str0.size(), str1.size())) == str1; }
36 [[nodiscard]]
37 int case_compare(const std::string_view str0, const std::string_view str1) noexcept;
39 [[nodiscard]]
40 int case_compare(const std::wstring_view str0, const std::wstring_view str1) noexcept;
42 /* C++20 changes path::u8string() to return a string using a new/distinct
43 * char8_t type for UTF-8 strings. However, support for this with standard
44 * string functions is totally inadequate, and we already hold UTF-8 with plain
45 * char strings. So this function is used to reinterpret a char8_t string as a
46 * char string_view.
48 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201907L
49 inline auto u8_as_char(const std::u8string_view str) -> std::string_view
50 #else
51 inline auto u8_as_char(const std::string_view str) -> std::string_view
52 #endif
54 return std::string_view{reinterpret_cast<const char*>(str.data()), str.size()};
57 } // namespace al
59 #endif /* AL_STRING_H */