1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/*---------------------------------------------------------------------------*\
____ _ _ __ _ __ ___ _ _
|_ / || | '_ \ '_ \/ -_) '_|
/__|\_, | .__/ .__/\___|_|
|__/|_| |_|
\*---------------------------------------------------------------------------*/
#include <libintl.h>
#include <cwchar>
#include <cstring>
#include <ostream>
#include "text.h"
using namespace std;
const char*
_(const char* msgid)
{
return dgettext("snapper", msgid);
}
const char*
_(const char* msgid, const char* msgid_plural, unsigned long int n)
{
return dngettext("snapper", msgid, msgid_plural, n);
}
// A non-ASCII string has 3 different lengths:
// - bytes
// - characters (non-ASCII ones have multiple bytes in UTF-8)
// - columns (Chinese characters are 2 columns wide)
// In #328918 see how confusing these leads to misalignment.
// return the number of columns in str, or -1 if there's an error
static
int mbs_width_e (const string & str)
{
// from smpppd.src.rpm/format.cc, thanks arvin
const char* ptr = str.c_str ();
size_t s_bytes = str.length ();
int s_cols = 0;
bool in_ctrlseq = false;
mbstate_t shift_state;
memset (&shift_state, 0, sizeof (shift_state));
wchar_t wc;
size_t c_bytes;
// mbrtowc produces one wide character from a multibyte string
while ((c_bytes = mbrtowc (&wc, ptr, s_bytes, &shift_state)) > 0)
{
if (c_bytes >= (size_t) -2) // incomplete (-2) or invalid (-1) sequence
return -1;
// ignore the length of terminal control sequences in order
// to compute the length of colored text correctly
if (!in_ctrlseq && ::wcsncmp(&wc, L"\033", 1) == 0)
in_ctrlseq = true;
else if (in_ctrlseq && ::wcsncmp(&wc, L"m", 1) == 0)
in_ctrlseq = false;
else if (!in_ctrlseq)
s_cols += ::wcwidth(wc);
s_bytes -= c_bytes;
ptr += c_bytes;
// end of string
if (s_bytes == 0) break;
}
return s_cols;
}
unsigned mbs_width (const string& str)
{
int c = mbs_width_e(str);
if (c < 0)
return str.length(); // fallback if there was an error
else
return (unsigned) c;
}
// ---------------------------------------------------------------------------
std::string mbs_substr_by_width(
const std::string & str,
std::string::size_type pos,
std::string::size_type n)
{
if (n == 0)
return string();
const char * ptr = str.c_str();
const char * sptr = NULL;
const char * eptr = NULL;
size_t s_bytes = str.length();
int s_cols = 0, s_cols_prev;
bool in_ctrlseq = false;
mbstate_t shift_state;
memset (&shift_state, 0, sizeof (shift_state));
wchar_t wc;
size_t c_bytes;
// mbrtowc produces one wide character from a multibyte string
while ((c_bytes = mbrtowc (&wc, ptr, s_bytes, &shift_state)) > 0)
{
if (c_bytes >= (size_t) -2) // incomplete (-2) or invalid (-1) sequence
return str.substr(pos, n); // default to normal string substr
s_cols_prev = s_cols;
// ignore the length of terminal control sequences in order
// to compute the length of the string correctly
if (!in_ctrlseq && ::wcsncmp(&wc, L"\033", 1) == 0)
in_ctrlseq = true;
else if (in_ctrlseq && ::wcsncmp(&wc, L"m", 1) == 0)
in_ctrlseq = false;
else if (!in_ctrlseq)
s_cols += ::wcwidth(wc);
// mark the beginning
if (sptr == NULL && (unsigned) s_cols >= pos)
{
// cut at the right column, include also the current character
if ((unsigned) s_cols_prev == pos)
sptr = ptr;
// current character cut into pieces, don't include it
else
sptr = ptr + c_bytes;
}
// mark the end
if (n != string::npos && (unsigned) s_cols >= pos + n)
{
// cut at the right column, include also the current character
if ((unsigned) s_cols == pos + n)
eptr = ptr + c_bytes;
// current character cut into pieces, don't include it
else
eptr = ptr;
break;
}
s_bytes -= c_bytes;
ptr += c_bytes;
// end of string
if (s_bytes == 0) break;
}
if (eptr == NULL)
eptr = ptr;
if (eptr == sptr)
return string();
return string(sptr, eptr - sptr);
}
// ---------------------------------------------------------------------------
void mbs_write_wrapped(ostream & out, const string & text,
unsigned indent, unsigned wrap_width, int initial)
{
const char * s = text.c_str();
size_t s_bytes = text.length();
const char * prevwp = s;
const char * linep = s;
wchar_t wc;
size_t bytes_read;
bool in_word = false;
bool in_ctrlseq = false;
mbstate_t shift_state;
memset (&shift_state, 0, sizeof (shift_state));
unsigned col = 0;
unsigned toindent = initial < 0 ? indent : initial;
do
{
// indentation
if (!col)
{
out << string(toindent, ' ');
col = toindent;
}
bytes_read = mbrtowc (&wc, s, s_bytes, &shift_state);
if (bytes_read > 0)
{
// ignore the length of terminal control sequences in order
// to wrap colored text correctly
if (!in_ctrlseq && ::wcsncmp(&wc, L"\033", 1) == 0)
in_ctrlseq = true;
else if (in_ctrlseq && ::wcsncmp(&wc, L"m", 1) == 0)
in_ctrlseq = false;
else if (!in_ctrlseq)
col += ::wcwidth(wc);
if (::iswspace(wc))
in_word = false;
//else if (::wcscmp(wc, L"\n"))
//{
// if (!in_word)
// prevwp = s;
// in_word = true;
//}
else
{
if (!in_word)
prevwp = s;
in_word = true;
}
// current wc exceeded the wrap width
if (col > wrap_width)
{
bool wanthyphen = false;
// A single word is longer than the wrap width. Split the word and
// append a hyphen at the end of the line. This won't normally happen,
// but it can eventually happen e.g. with paths or too low screen widths.
//! \todo make word-splitting more intelligent, e.g. if the word already
//! contains a hyphen, split there; do not split because
//! of a non-alphabet character; do not leave orphans, etc...
if (linep == prevwp)
{
prevwp = s - 2;
wanthyphen = true;
}
// update the size of the string to read
s_bytes -= (prevwp - linep);
// print the line, leave linep point to the start of the last word.
for (; linep < prevwp; ++linep)
out << *linep;
if (wanthyphen)
out << "-";
out << endl;
// reset column counter
col = 0;
toindent = indent;
// reset original text pointer (points to current wc, not the start of the word)
s = linep;
// reset shift state
::memset (&shift_state, 0, sizeof (shift_state));
}
else
s += bytes_read;
}
// we're at the end of the string
else
{
// print the rest of the text
for(; *linep; ++linep)
out << *linep;
}
}
while(bytes_read > 0);
}
|