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
19namespace toolbox {
20inline namespace util {
21using namespace std;
22
23namespace {
24constexpr char Space[] = " \t\n\v\f\r";
25} // namespace
26
27void ltrim(string_view& s) noexcept
28{
29 const auto pos = s.find_first_not_of(Space);
30 s.remove_prefix(pos != string_view::npos ? pos : s.size());
31}
32
33void ltrim(string& s) noexcept
34{
35 const auto pos = s.find_first_not_of(Space);
36 s.erase(0, pos != string_view::npos ? pos : s.size());
37}
38
39void rtrim(string_view& s) noexcept
40{
41 const auto pos = s.find_last_not_of(Space);
42 s.remove_suffix(s.size() - (pos != string_view::npos ? pos + 1 : 0));
43}
44
45void rtrim(string& s) noexcept
46{
47 const auto pos = s.find_last_not_of(Space);
48 s.erase(pos != string_view::npos ? pos + 1 : 0);
49}
50
52{
53 const auto pos = s.find_first_of(delim);
54 string_view key, val;
55 if (pos == string_view::npos) {
56 key = s;
57 } else {
58 key = s.substr(0, pos);
59 val = s.substr(pos + 1);
60 }
61 return {key, val};
62}
63
65{
66 const auto pos = s.find_first_of(delim);
67 string key, val;
68 if (pos == string::npos) {
69 key = s;
70 } else {
71 key = s.substr(0, pos);
72 val = s.substr(pos + 1);
73 }
74 return {key, val};
75}
76
77} // namespace util
78} // namespace toolbox
STL namespace.
pair< string_view, string_view > split_pair(string_view s, char delim) noexcept
Definition String.cpp:51
void rtrim(string_view &s) noexcept
Definition String.cpp:39
void ltrim(string_view &s) noexcept
Definition String.cpp:27
constexpr auto bind() noexcept
Definition Slot.hpp:92