Audacity 3.2.0
StringUtils.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: StringUtils.h
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
7#pragma once
8
9#include <algorithm>
10#include <cstring>
11#include <cwchar>
12#include <string>
13#include <string_view>
14#include <type_traits>
15#include <numeric>
16
17#include <wx/string.h>
18
19inline std::size_t StringLength(const char str)
20{
21 return 1;
22}
23
24inline std::size_t StringLength(const wchar_t str)
25{
26 return 1;
27}
28
29inline std::size_t StringLength(const char* str)
30{
31 return std::strlen(str);
32}
33
34inline std::size_t StringLength(const wchar_t* str)
35{
36 return std::wcslen(str);
37}
38
39template<typename CharType, size_t N>
40std::size_t StringLength (const CharType (&)[N])
41{
42 return N - 1;
43}
44
45template<typename StringType>
46std::size_t StringLength (const StringType& str)
47{
48 return str.length();
49}
50
51template<template<typename, typename...> typename ContainerType, typename ResultType, typename SeparatorType, typename... Rest>
52ResultType Join(
53 const ContainerType<ResultType, Rest...>& container,
54 const SeparatorType& separator)
55{
56 if (container.empty())
57 return ResultType {};
58
59 const auto sepratorLength = StringLength(separator);
60 const auto totalSeparatorLength = sepratorLength * (container.size() - 1);
61
62 ResultType result;
63
64 const auto size = std::accumulate(
65 container.begin(), container.end(), totalSeparatorLength,
66 [](size_t size, const ResultType& item)
67 { return size + StringLength(item); });
68
69 result.reserve(size);
70
71 bool first = true;
72 for (const auto& item : container)
73 {
74 if (!first)
75 result += separator;
76 result += item;
77 first = false;
78 }
79
80 return result;
81}
82
83STRING_UTILS_API std::string ToLower(const std::string& str);
84STRING_UTILS_API std::string ToLower(const std::string_view& str);
85STRING_UTILS_API std::string ToLower(const char* str);
86STRING_UTILS_API std::wstring ToLower(const std::wstring& str);
87STRING_UTILS_API std::wstring ToLower(const std::wstring_view& str);
88STRING_UTILS_API std::wstring ToLower(const wchar_t* str);
89STRING_UTILS_API wxString ToLower(const wxString& str);
90
91STRING_UTILS_API std::string ToUpper(const std::string& str);
92STRING_UTILS_API std::string ToUpper(const std::string_view& str);
93STRING_UTILS_API std::string ToUpper(const char* str);
94STRING_UTILS_API std::wstring ToUpper(const std::wstring& str);
95STRING_UTILS_API std::wstring ToUpper(const std::wstring_view& str);
96STRING_UTILS_API std::wstring ToUpper(const wchar_t* str);
97STRING_UTILS_API wxString ToUpper(const wxString& str);
98
99namespace details
100{
101inline const char* begin(const char* str) noexcept
102{
103 return str;
104}
105
106inline const char* end(const char* str) noexcept
107{
108 if (str == nullptr)
109 return nullptr;
110
111 return str + StringLength(str);
112}
113
114inline const wchar_t* begin(const wchar_t* str) noexcept
115{
116 return str;
117}
118
119inline const wchar_t* end(const wchar_t* str) noexcept
120{
121 if (str == nullptr)
122 return nullptr;
123
124 return str + StringLength(str);
125}
126} // namespace details
127
128template<typename HayType, typename PrefixType>
130 const HayType& hay, const PrefixType& prefix)
131{
132 if (StringLength(hay) < StringLength(prefix))
133 return false;
134
135 using namespace std;
136 using namespace details;
137
138 const auto prefixBegin = begin(prefix);
139 const auto prefixEnd = end(prefix);
140 const auto hayBegin = begin(hay);
141
142 return std::mismatch(prefixBegin, prefixEnd, hayBegin).first == prefixEnd;
143}
144
145template <typename HayType, typename PrefixType>
146bool IsPrefixedInsensitive(const HayType& hay, const PrefixType& prefix)
147{
148 return IsPrefixed(ToLower(hay), ToLower(prefix));
149}
#define str(a)
bool IsPrefixedInsensitive(const HayType &hay, const PrefixType &prefix)
Definition: StringUtils.h:146
STRING_UTILS_API std::string ToLower(const std::string &str)
Definition: StringUtils.cpp:11
bool IsPrefixed(const HayType &hay, const PrefixType &prefix)
Definition: StringUtils.h:129
STRING_UTILS_API std::string ToUpper(const std::string &str)
Definition: StringUtils.cpp:46
ResultType Join(const ContainerType< ResultType, Rest... > &container, const SeparatorType &separator)
Definition: StringUtils.h:52
std::size_t StringLength(const char str)
Definition: StringUtils.h:19
Tail< TypeList > Rest
Synonym for Tail.
Definition: TypeList.h:113
const wchar_t * end(const wchar_t *str) noexcept
Definition: StringUtils.h:119
const wchar_t * begin(const wchar_t *str) noexcept
Definition: StringUtils.h:114
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
STL namespace.