E-MailRelay
gstringwrap.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gstringwrap.h
19///
20
21#ifndef G_STRING_WRAP_H
22#define G_STRING_WRAP_H
23
24#include "gdef.h"
25#include "gstringview.h"
26#include <string>
27
28namespace G
29{
30 class StringWrap ;
31}
32
33//| \class G::StringWrap
34/// A word-wrap class.
35///
37{
38public:
39 static std::string wrap( const std::string & text ,
40 const std::string & prefix_first , const std::string & prefix_other ,
41 std::size_t width_first = 70U , std::size_t width_other = 0U ,
42 bool preserve_spaces = false , const std::locale & = defaultLocale() ) ;
43 ///< Does word-wrapping. The return value is a string with embedded
44 ///< newlines. If 'preserve_spaces' is true then all space characters
45 ///< between input words that end up in the middle of an output line
46 ///< are preserved. There is no special handling of tabs or carriage
47 ///< returns. The 'first/other' parameters distinguish between the
48 ///< first output line and the rest.
49
50 static std::locale defaultLocale() ;
51 ///< Returns a locale with at least the CTYPE and codecvt facets
52 ///< initialised according to the C locale's CTYPE. Returns the
53 ///< classic locale on error.
54 ///<
55 ///< A motivating use-case is where the gettext() library has been
56 ///< initialised by setting the C locale's CTYPE and MESSAGES facets,
57 ///< meaning that all gettext() strings are automatically converted
58 ///< to the relevant character encoding. If the strings from
59 ///< gettext() are to be word-wrapped accurately then the
60 ///< word-wrapper's CTYPE and codecvt facets need to match the
61 ///< C locale's CTYPE.
62
63 static std::size_t wordsize( const std::string & mbcs , const std::locale & ) ;
64 ///< Returns the number of wide characters after converting the input
65 ///< string using the locale's codecvt facet. Conversion errors are
66 ///< ignored.
67
68public:
69 StringWrap() = delete ;
70
71private:
72 struct Config ;
73 class WordWrapper ;
74 static std::size_t wordsize( G::string_view mbcs , const std::locale & ) ;
75 static void wrap( std::istream & , WordWrapper & ) ;
76} ;
77
78#endif
Private implementation structure for G::StringWrap.
Definition: gstringwrap.cpp:41
A word-wrap class.
Definition: gstringwrap.h:37
static std::locale defaultLocale()
Returns a locale with at least the CTYPE and codecvt facets initialised according to the C locale's C...
static std::size_t wordsize(const std::string &mbcs, const std::locale &)
Returns the number of wide characters after converting the input string using the locale's codecvt fa...
static std::string wrap(const std::string &text, const std::string &prefix_first, const std::string &prefix_other, std::size_t width_first=70U, std::size_t width_other=0U, bool preserve_spaces=false, const std::locale &=defaultLocale())
Does word-wrapping.
A class template like c++17's std::basic_string_view.
Definition: gstringview.h:73
Low-level classes.
Definition: galign.h:28
Private implementation structure for G::StringWrap.
Definition: gstringwrap.cpp:32