libzypp 17.37.10

text.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9
10#include <cstring>
11#include <boost/utility/string_ref.hpp>
12#include "text.h"
13
14 namespace ztui {
15
16std::string mbs_substr_by_width( boost::string_ref text_r, std::string::size_type colpos_r, std::string::size_type collen_r )
17{
18 std::string ret;
19 if ( collen_r )
20 {
21 const char * spos = nullptr;
22 size_t slen = 0;
23
24 size_t colend = ( collen_r == std::string::npos ? std::string::npos : colpos_r+collen_r ); // will exploit npos == size_t(-1)
25 size_t pos = 0;
26 for( mbs::MbsIterator it( text_r ); ! it.atEnd(); ++it )
27 {
28 // collect sequences [pos,end[ in [colpos_r,colend[
29 // partial overlaps are padded
30 size_t end = pos + it.columns();
31
32 if ( pos < colpos_r ) // starts before range
33 {
34 if ( end > colpos_r ) // pad incomplete sequence at range begin
35 ret += std::string( std::min(end,colend)-colpos_r, ' ' );
36 }
37 else // starts inside range (pos < colend by the way we loop)
38 {
39 if ( end <= colend ) // completely inside
40 {
41 if ( !spos )
42 spos = it.pos();
43 slen += it.size();
44 }
45 else // partial outside
46 {
47 if ( spos )
48 {
49 ret += std::string( spos, slen );
50 spos = nullptr;
51 slen = 0; // don't collect it after loop
52 }
53 ret += std::string( colend-pos, ' ' );
54 break; // done
55 }
56 }
57
58 if ( end >= colend )
59 break;
60 pos = end;
61 }
62 if ( spos )
63 ret += std::string( spos, slen );
64 }
65 return ret;
66}
67
68}
ztui
Definition application.cc:13
ztui::mbs_substr_by_width
std::string mbs_substr_by_width(boost::string_ref text_r, std::string::size_type colpos_r, std::string::size_type collen_r)
Returns a substring of a multi-byte character string text_r starting at screen column cpos_r and bein...
Definition text.cc:16
ztui::mbs::MbsIterator
Iterate chars and ANSI SGR in a multi-byte character string.
Definition text.h:87
ztui::mbs::MbsIterator::atEnd
bool atEnd() const
Definition text.h:121
text.h