Version: SMASH-1.5
smash::utf8 Namespace Reference

Functions

std::string fill_left (const std::string &s, size_t width, char fill=' ')
 Fill string with characters to the left until the given width is reached. More...
 
std::string fill_right (const std::string &s, size_t width, char fill=' ')
 Fill string with characters to the right until the given width is reached. More...
 
std::string fill_both (const std::string &s, size_t width, char fill=' ')
 Fill string with characters at both sides until the given width is reached. More...
 
template<typename octet_type >
uint8_t mask8 (octet_type oc)
 Extract the first byte from a given value. More...
 
template<typename octet_iterator >
std::iterator_traits< octet_iterator >::difference_type sequence_length (octet_iterator lead_it)
 Given an iterator to the beginning of a UTF-8 sequence, return the length of the next UTF-8 code point. More...
 
static size_t adjust (const std::string &s, size_t width)
 Adjust filling width by taking the size of unicode characters into account. More...
 

Function Documentation

◆ fill_left()

std::string smash::utf8::fill_left ( const std::string &  s,
size_t  width,
char  fill = ' ' 
)

Fill string with characters to the left until the given width is reached.

Parameters
[in]sInput string.
[in]widthTotal width of output string.
[in]fillFilling character.
Returns
Padded string.

Definition at line 47 of file stringfunctions.cc.

47  {
48  width = adjust(s, width - s.size());
49  if (width > 0) {
50  return std::string(width, fill) + s;
51  }
52  return s;
53 }
static size_t adjust(const std::string &s, size_t width)
Adjust filling width by taking the size of unicode characters into account.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_right()

std::string smash::utf8::fill_right ( const std::string &  s,
size_t  width,
char  fill = ' ' 
)

Fill string with characters to the right until the given width is reached.

Parameters
[in]sInput string.
[in]widthTotal width of output string.
[in]fillFilling character.
Returns
Padded string.

Definition at line 55 of file stringfunctions.cc.

55  {
56  width = adjust(s, width - s.size());
57  if (width > 0) {
58  return s + std::string(width, fill);
59  }
60  return s;
61 }
static size_t adjust(const std::string &s, size_t width)
Adjust filling width by taking the size of unicode characters into account.
Here is the call graph for this function:

◆ fill_both()

std::string smash::utf8::fill_both ( const std::string &  s,
size_t  width,
char  fill = ' ' 
)

Fill string with characters at both sides until the given width is reached.

Parameters
[in]sInput string.
[in]widthTotal width of output string.
[in]fillFilling character.
Returns
Padded string.

Definition at line 63 of file stringfunctions.cc.

63  {
64  width = adjust(s, width - s.size());
65  if (width > 0) {
66  const int l = width / 2;
67  const int r = width - l;
68  return std::string(l, fill) + s + std::string(r, fill);
69  }
70  return s;
71 }
static size_t adjust(const std::string &s, size_t width)
Adjust filling width by taking the size of unicode characters into account.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mask8()

template<typename octet_type >
uint8_t smash::utf8::mask8 ( octet_type  oc)
inline

Extract the first byte from a given value.

This function was taken from the Boost-licensed library UTF8-CPP. See http://utfcpp.sourceforge.net/.

Template Parameters
octet_typeType for one byte

Definition at line 90 of file stringfunctions.h.

90  {
91  return static_cast<uint8_t>(0xff & oc);
92 }
Here is the caller graph for this function:

◆ sequence_length()

template<typename octet_iterator >
std::iterator_traits<octet_iterator>::difference_type smash::utf8::sequence_length ( octet_iterator  lead_it)
inline

Given an iterator to the beginning of a UTF-8 sequence, return the length of the next UTF-8 code point.

This function was taken from the Boost-licensed library UTF8-CPP. See http://utfcpp.sourceforge.net/.

Definition at line 103 of file stringfunctions.h.

103  {
104  uint8_t lead = mask8(*lead_it);
105  if (lead < 0x80)
106  return 1;
107  else if ((lead >> 5) == 0x6)
108  return 2;
109  else if ((lead >> 4) == 0xe)
110  return 3;
111  else if ((lead >> 3) == 0x1e)
112  return 4;
113  else
114  return 0;
115 }
uint8_t mask8(octet_type oc)
Extract the first byte from a given value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ adjust()

static size_t smash::utf8::adjust ( const std::string &  s,
size_t  width 
)
inlinestatic

Adjust filling width by taking the size of unicode characters into account.

This is necessary, because UTF-8 characters can be represented by more than byte.

Parameters
[in]sString to be filled.
[in]widthWidth (in bytes) to be adjusted.
Returns
Adjusted width.

Definition at line 27 of file stringfunctions.cc.

27  {
28  for (unsigned char c : s) {
29  if (c >= 0xFC) {
30  width += 5;
31  } else if (c >= 0xF8) {
32  width += 4;
33  } else if (c >= 0xF0) {
34  width += 3;
35  } else if (c >= 0xE0) {
36  width += 2;
37  } else if (c == 0xCC || c == 0xCD) {
38  // combining character (2 Bytes) - doesn't appear at all
39  width += 2;
40  } else if (c >= 0xC0) {
41  width += 1;
42  }
43  }
44  return width;
45 }
Here is the caller graph for this function: