Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
String.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "String.hpp"
18#include <string_view>
19
20namespace toolbox {
21inline namespace util {
22using namespace std;
23
24namespace {
25constexpr char Space[] = " \t\n\v\f\r";
26} // namespace
27
28void ltrim(string_view& s) noexcept
29{
30 const auto pos = s.find_first_not_of(Space);
31 s.remove_prefix(pos != string_view::npos ? pos : s.size());
32}
33
34void ltrim(string& s) noexcept
35{
36 const auto pos = s.find_first_not_of(Space);
37 s.erase(0, pos != string_view::npos ? pos : s.size());
38}
39
40void rtrim(string_view& s) noexcept
41{
42 const auto pos = s.find_last_not_of(Space);
43 s.remove_suffix(s.size() - (pos != string_view::npos ? pos + 1 : 0));
44}
45
46void rtrim(string& s) noexcept
47{
48 const auto pos = s.find_last_not_of(Space);
49 s.erase(pos != string_view::npos ? pos + 1 : 0);
50}
51
52pair<string_view, string_view> split_pair(string_view s, string_view delim) noexcept
53{
54 const auto pos = s.find_first_of(delim);
55 string_view key, val;
56 if (pos == string_view::npos) {
57 key = s;
58 } else {
59 key = s.substr(0, pos);
60 val = s.substr(pos + delim.size());
61 }
62 return {key, val};
63}
64
66{
67 return split_pair(s, string_view{&delim, 1});
68}
69
70pair<string, string> split_pair(const string& s, string_view delim)
71{
72 auto [k, v] = split_pair(string_view{s}, delim);
73 return pair<string, string>{k, v};
74}
75
77{
78 auto [k, v] = split_pair(string_view{s}, delim);
79 return pair<string, string>{k, v};
80}
81
82} // namespace util
83} // namespace toolbox
STL namespace.
pair< string_view, string_view > split_pair(string_view s, string_view delim) noexcept
Definition String.cpp:52
void rtrim(string_view &s) noexcept
Definition String.cpp:40
void ltrim(string_view &s) noexcept
Definition String.cpp:28
constexpr auto bind() noexcept
Definition Slot.hpp:92