cppx-core
string-util.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 #include <cppx-core/collections/Span_.hpp> // cppx::(Span_, all_but_first_of)
3 #include <cppx-core/collections/is_empty.hpp> // cppx::is_empty
5 #include <cppx-core/text/ascii/ascii-util.hpp> // cppx::*
6 #include <cppx-core/text/data/Symbol_strings.hpp> // cppx::best_effort::*
7 #include <cppx-core/text/pointers-from-string_view.hpp> // cppx::(p_first_of, p_beyond_of)
8 #include <cppx-core/text/unicode/utf8-iteration.hpp> // cppx::utf8::(n_code_points_in, *)
9 
10 #include <iterator> // std::next
11 #include <utility> // std::move
12 
13 namespace cppx
14 {
15  CPPX_USE_STD( basic_string_view, move, next, string, string_view );
16 
17  //---------------------------------------- Adjustment (mostly for console output):
18  //
19  // This is not at all perfect, especially wrt. Unicode character modifiers, but the
20  // simpleton approach of counting code points for character positions is usually Good
21  // Enough™, and it plays nicely with console windows that do not compose characters.
22 
23  inline auto spaces( const int n )
24  -> string
25  { return (n <= 0? "" : string( n, ' ')); }
26 
27  inline namespace string_repeat
28  {
29  inline auto repeated( const int n, const string_view& s )
30  -> string
31  {
32  if( n <= 0 ) { return ""; }
33 
34  string result;
35  result.reserve( n*s.length() );
36  for( int i = 1; i <= n; ++i )
37  {
38  result += s;
39  }
40  return result;
41  }
42 
43  inline auto operator*( const int n, const string_view& s )
44  -> string
45  { return repeated( n, s ); }
46  }
47 
48  inline auto left( const string_view& s, const int width )
49  -> string
50  { return string( s ) + spaces( width - utf8::n_code_points_in( s ) ); }
51 
52  inline auto right( const string_view& s, const int width )
53  -> string
54  { return spaces( width - utf8::n_code_points_in( s ) ) + string( s ); }
55 
56 
57  //---------------------------------------- Misc:
58 
59  // Uses simple straight ASCII quotes if CPPX_ASCII_PLEASE is defined.
60  inline auto quoted( const string_view& sv )
61  -> string
62  {
63  string s;
65  s += sv;
67  return s;
68  }
69 
70  inline auto quoted( const char ch )
71  -> string
72  { return quoted( string_view( &ch, 1 ) ); }
73 
74  inline auto trimmed( const string_view& sv )
75  -> string_view
76  {
77  if( is_empty( sv ) )
78  {
79  return sv;
80  }
81  P_<const char> p_first = p_first_of( sv );
82  P_<const char> p_beyond = p_beyond_of( sv );
83  while( p_first != p_beyond and ascii::is_whitespace( *p_first ) )
84  {
85  ++p_first;
86  }
87  while( p_beyond != p_first and ascii::is_whitespace( *p_beyond ) )
88  {
89  --p_beyond;
90  }
91  return string_view( p_first, p_beyond - p_first );
92  }
93 
94  inline auto trimmed( const string& s )
95  -> string
96  { return string( trimmed( string_view( s ) ) ); }
97 
98  inline auto trimmed( string&& s )
99  -> string
100  {
101  const string_view t = trimmed( string_view( s ) );
102  if( t.length() == s.length() )
103  {
104  return move( s );
105  }
106  else
107  {
108  return string( t );
109  }
110  }
111 
112  inline auto split() -> void; // TODO
113 
114  template< class Iterator >
115  inline auto joined(
116  const Span_<Iterator> range,
117  const string& separator = " "
118  ) -> string
119  {
120  if( is_empty( range ) ) { return ""; }
121 
122  string result = range.front();
123  for( const auto& item : all_but_first_of( range ) )
124  {
125  result += separator;
126  result += item;
127  }
128  return result;
129  }
130 
131 } // namespace cppx
auto n_code_points_in(const string_view &view) -> Size
Some_type * P_
Creates a raw pointer type.
auto left(const string_view &s, const int width) -> string
Definition: string-util.hpp:48
auto operator *(const int n, const string_view &s) -> string
Definition: string-util.hpp:43
auto all_but_first_of(Container &&c) -> Span_< decltype(begin(c))>
Definition: Span_.hpp:112
auto repeated(const int n, const string_view &s) -> string
Definition: string-util.hpp:29
auto is_whitespace(const Char ch) -> Truth
Definition: ascii-util.hpp:99
CPPX_USE_STD(basic_string, basic_string_view, bitset, char_traits, size)
auto right(const string_view &s, const int width) -> string
Definition: string-util.hpp:52
auto trimmed(const string_view &sv) -> string_view
Definition: string-util.hpp:74
auto p_first_of(const basic_string_view< Char > &view) noexcept -> C_str_< Char >
constexpr auto & left_quote_str
Simple type builders Type_, P_, R_, Raw_array_ and Raw_array_of_.
auto is_empty(const Collection &c) -> Truth
Definition: is_empty.hpp:33
auto next(P_< const char > p) -> P_< const char >
auto spaces(const int n) -> string
Definition: string-util.hpp:23
auto split() -> void
auto quoted(const string_view &sv) -> string
Definition: string-util.hpp:60
auto joined(const Span_< Iterator > range, const string &separator=" ") -> string
constexpr auto & right_quote_str
auto p_beyond_of(const basic_string_view< Char > &view) noexcept -> C_str_< Char >