change naming conventions in NC namespace
This commit is contained in:
135
src/strbuffer.h
135
src/strbuffer.h
@@ -30,14 +30,12 @@ namespace NC {//
|
||||
/// Buffer template class that can store text along with its
|
||||
/// format attributes. The content can be easily printed to
|
||||
/// window or taken as raw string at any time.
|
||||
///
|
||||
template <typename C> class basic_buffer
|
||||
{
|
||||
friend class Scrollpad;
|
||||
|
||||
/// Struct used for storing information about
|
||||
/// one color/format flag along with its position
|
||||
///
|
||||
struct FormatPos
|
||||
{
|
||||
size_t Position;
|
||||
@@ -55,27 +53,21 @@ template <typename C> class basic_buffer
|
||||
};
|
||||
|
||||
/// Internal buffer for storing raw text
|
||||
///
|
||||
std::basic_string<C> itsString;
|
||||
std::basic_string<C> m_string;
|
||||
|
||||
/// List used for storing formatting informations
|
||||
///
|
||||
std::list<FormatPos> itsFormat;
|
||||
|
||||
std::list<FormatPos> m_format;
|
||||
|
||||
public:
|
||||
/// Constructs an empty buffer
|
||||
///
|
||||
basic_buffer() { }
|
||||
|
||||
/// Constructs a buffer from the existed one
|
||||
/// @param b copied buffer
|
||||
///
|
||||
basic_buffer(const basic_buffer &b);
|
||||
|
||||
/// @return raw content of the buffer without formatting informations
|
||||
///
|
||||
const std::basic_string<C> &Str() const;
|
||||
const std::basic_string<C> &str() const;
|
||||
|
||||
/// Searches for given string in buffer and sets format/color at the
|
||||
/// beginning and end of it using val_b and val_e flags accordingly
|
||||
@@ -86,8 +78,7 @@ public:
|
||||
/// @param for_each indicates whether function searches through whole buffer and sets
|
||||
/// the format for all occurences of given string or stops after the first one
|
||||
/// @return true if at least one occurence of the string was found, false otherwise
|
||||
///
|
||||
bool SetFormatting(short val_b, std::basic_string<C> s, short val_e,
|
||||
bool setFormatting(short val_b, std::basic_string<C> s, short val_e,
|
||||
bool case_sensitive, bool for_each = 1);
|
||||
|
||||
/// Searches for given string in buffer and removes given
|
||||
@@ -98,13 +89,11 @@ public:
|
||||
/// @param case_sensitive indicates whether algorithm should care about case sensitivity
|
||||
/// @param for_each indicates whether function searches through whole buffer and removes
|
||||
/// given format from all occurences of given string or stops after the first one
|
||||
///
|
||||
void RemoveFormatting(short val_b, std::basic_string<C> pattern, short val_e,
|
||||
void removeFormatting(short val_b, std::basic_string<C> pattern, short val_e,
|
||||
bool case_sensitive, bool for_each = 1);
|
||||
|
||||
/// Removes all formating applied to string in buffer.
|
||||
///
|
||||
void RemoveFormatting();
|
||||
void removeFormatting();
|
||||
|
||||
/// Prints to window object given part of the string, loading all needed formatting info
|
||||
/// and cleaning up after. The main goal of this function is to provide interface for
|
||||
@@ -115,89 +104,84 @@ public:
|
||||
/// @param width width of the string to be printed
|
||||
/// @param separator additional text to be placed between the end and the beginning of
|
||||
/// the string
|
||||
///
|
||||
void Write(Window &w, size_t &start_pos, size_t width,
|
||||
void write(Window &w, size_t &start_pos, size_t width,
|
||||
const std::basic_string<C> &separator) const;
|
||||
|
||||
/// Clears the content of the buffer and its formatting informations
|
||||
///
|
||||
void Clear();
|
||||
void clear();
|
||||
|
||||
basic_buffer<C> &operator<<(int n)
|
||||
{
|
||||
itsString += intTo< std::basic_string<C> >::apply(n);
|
||||
m_string += intTo< std::basic_string<C> >::apply(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(long int n)
|
||||
{
|
||||
itsString += longIntTo< std::basic_string<C> >::apply(n);
|
||||
m_string += longIntTo< std::basic_string<C> >::apply(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(unsigned int n)
|
||||
{
|
||||
itsString += unsignedIntTo< std::basic_string<C> >::apply(n);
|
||||
m_string += unsignedIntTo< std::basic_string<C> >::apply(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(unsigned long int n)
|
||||
{
|
||||
itsString += unsignedLongIntTo< std::basic_string<C> >::apply(n);
|
||||
m_string += unsignedLongIntTo< std::basic_string<C> >::apply(n);
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(char c)
|
||||
{
|
||||
itsString += c;
|
||||
m_string += c;
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(wchar_t wc)
|
||||
{
|
||||
itsString += wc;
|
||||
m_string += wc;
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(const C *s)
|
||||
{
|
||||
itsString += s;
|
||||
m_string += s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
basic_buffer<C> &operator<<(const std::basic_string<C> &s)
|
||||
{
|
||||
itsString += s;
|
||||
m_string += s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Handles colors
|
||||
/// @return reference to itself
|
||||
///
|
||||
basic_buffer<C> &operator<<(Color color);
|
||||
|
||||
/// Handles format flags
|
||||
/// @return reference to itself
|
||||
///
|
||||
basic_buffer<C> &operator<<(Format f);
|
||||
|
||||
/// Handles copying one buffer to another using operator<<()
|
||||
/// @param buf buffer to be copied
|
||||
/// @return reference to itself
|
||||
///
|
||||
basic_buffer<C> &operator<<(const basic_buffer<C> &buf);
|
||||
|
||||
/// Friend operator that handles printing
|
||||
/// the content of buffer to window object
|
||||
friend Window &operator<<(Window &w, const basic_buffer<C> &buf)
|
||||
{
|
||||
const std::basic_string<C> &s = buf.itsString;
|
||||
if (buf.itsFormat.empty())
|
||||
const std::basic_string<C> &s = buf.m_string;
|
||||
if (buf.m_format.empty())
|
||||
w << s;
|
||||
else
|
||||
{
|
||||
std::basic_string<C> tmp;
|
||||
auto b = buf.itsFormat.begin(), e = buf.itsFormat.end();
|
||||
auto b = buf.m_format.begin(), e = buf.m_format.end();
|
||||
for (size_t i = 0; i < s.length() || b != e; ++i)
|
||||
{
|
||||
while (b != e && i == b->Position)
|
||||
@@ -207,7 +191,7 @@ public:
|
||||
w << tmp;
|
||||
tmp.clear();
|
||||
}
|
||||
buf.LoadAttribute(w, b->Value);
|
||||
buf.loadAttribute(w, b->Value);
|
||||
b++;
|
||||
}
|
||||
tmp += s[i];
|
||||
@@ -222,27 +206,24 @@ private:
|
||||
/// Loads an attribute to given window object
|
||||
/// @param w window object we want to load attribute to
|
||||
/// @param value value of attribute to be loaded
|
||||
///
|
||||
void LoadAttribute(Window &w, short value) const;
|
||||
void loadAttribute(Window &w, short value) const;
|
||||
};
|
||||
|
||||
/// Standard buffer that uses narrow characters
|
||||
///
|
||||
typedef basic_buffer<char> Buffer;
|
||||
|
||||
/// Standard buffer that uses wide characters
|
||||
///
|
||||
typedef basic_buffer<wchar_t> WBuffer;
|
||||
|
||||
template <typename C> basic_buffer<C>::basic_buffer(const basic_buffer &b)
|
||||
: itsString(b.itsString), itsFormat(b.itsFormat) { }
|
||||
: m_string(b.m_string), m_format(b.m_format) { }
|
||||
|
||||
template <typename C> const std::basic_string<C> &basic_buffer<C>::Str() const
|
||||
template <typename C> const std::basic_string<C> &basic_buffer<C>::str() const
|
||||
{
|
||||
return itsString;
|
||||
return m_string;
|
||||
}
|
||||
|
||||
template <typename C> bool basic_buffer<C>::SetFormatting(
|
||||
template <typename C> bool basic_buffer<C>::setFormatting(
|
||||
short val_b,
|
||||
std::basic_string<C> s,
|
||||
short val_e,
|
||||
@@ -253,7 +234,7 @@ template <typename C> bool basic_buffer<C>::SetFormatting(
|
||||
if (s.empty())
|
||||
return false;
|
||||
bool result = false;
|
||||
std::basic_string<C> base = itsString;
|
||||
std::basic_string<C> base = m_string;
|
||||
if (!case_sensitive)
|
||||
{
|
||||
lowercase(s);
|
||||
@@ -265,19 +246,19 @@ template <typename C> bool basic_buffer<C>::SetFormatting(
|
||||
result = true;
|
||||
fp.Value = val_b;
|
||||
fp.Position = i;
|
||||
itsFormat.push_back(fp);
|
||||
m_format.push_back(fp);
|
||||
i += s.length();
|
||||
fp.Value = val_e;
|
||||
fp.Position = i;
|
||||
itsFormat.push_back(fp);
|
||||
m_format.push_back(fp);
|
||||
if (!for_each)
|
||||
break;
|
||||
}
|
||||
itsFormat.sort();
|
||||
m_format.sort();
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename C> void basic_buffer<C>::RemoveFormatting(
|
||||
template <typename C> void basic_buffer<C>::removeFormatting(
|
||||
short val_b,
|
||||
std::basic_string<C> pattern,
|
||||
short val_e,
|
||||
@@ -287,7 +268,7 @@ template <typename C> void basic_buffer<C>::RemoveFormatting(
|
||||
{
|
||||
if (pattern.empty())
|
||||
return;
|
||||
std::basic_string<C> base = itsString;
|
||||
std::basic_string<C> base = m_string;
|
||||
if (!case_sensitive)
|
||||
{
|
||||
lowercase(pattern);
|
||||
@@ -298,49 +279,49 @@ template <typename C> void basic_buffer<C>::RemoveFormatting(
|
||||
{
|
||||
fp.Value = val_b;
|
||||
fp.Position = i;
|
||||
itsFormat.remove(fp);
|
||||
m_format.remove(fp);
|
||||
i += pattern.length();
|
||||
fp.Value = val_e;
|
||||
fp.Position = i;
|
||||
itsFormat.remove(fp);
|
||||
m_format.remove(fp);
|
||||
if (!for_each)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename C> void basic_buffer<C>::RemoveFormatting()
|
||||
template <typename C> void basic_buffer<C>::removeFormatting()
|
||||
{
|
||||
itsFormat.clear();
|
||||
m_format.clear();
|
||||
}
|
||||
|
||||
template <typename C> void basic_buffer<C>::Write(
|
||||
template <typename C> void basic_buffer<C>::write(
|
||||
Window &w,
|
||||
size_t &start_pos,
|
||||
size_t width,
|
||||
const std::basic_string<C> &separator
|
||||
) const
|
||||
{
|
||||
std::basic_string<C> s = itsString;
|
||||
size_t len = Window::Length(s);
|
||||
std::basic_string<C> s = m_string;
|
||||
size_t len = Window::length(s);
|
||||
|
||||
if (len > width)
|
||||
{
|
||||
s += separator;
|
||||
len = 0;
|
||||
|
||||
auto lb = itsFormat.begin();
|
||||
if (itsFormat.back().Position > start_pos) // if there is no attributes from current position, don't load them
|
||||
auto lb = m_format.begin();
|
||||
if (m_format.back().Position > start_pos) // if there is no attributes from current position, don't load them
|
||||
{
|
||||
// load all attributes that are before start position
|
||||
for (; lb->Position < start_pos; ++lb)
|
||||
LoadAttribute(w, lb->Value);
|
||||
loadAttribute(w, lb->Value);
|
||||
}
|
||||
|
||||
for (size_t i = start_pos; i < s.length() && len < width; ++i)
|
||||
{
|
||||
while (i == lb->Position && lb != itsFormat.end())
|
||||
while (i == lb->Position && lb != m_format.end())
|
||||
{
|
||||
LoadAttribute(w, lb->Value);
|
||||
loadAttribute(w, lb->Value);
|
||||
++lb;
|
||||
}
|
||||
if ((len += wcwidth(s[i])) > width)
|
||||
@@ -351,12 +332,12 @@ template <typename C> void basic_buffer<C>::Write(
|
||||
start_pos = 0;
|
||||
|
||||
if (len < width)
|
||||
lb = itsFormat.begin();
|
||||
lb = m_format.begin();
|
||||
for (size_t i = 0; len < width; ++i)
|
||||
{
|
||||
while (i == lb->Position && lb != itsFormat.end())
|
||||
while (i == lb->Position && lb != m_format.end())
|
||||
{
|
||||
LoadAttribute(w, lb->Value);
|
||||
loadAttribute(w, lb->Value);
|
||||
++lb;
|
||||
}
|
||||
if ((len += wcwidth(s[i])) > width)
|
||||
@@ -364,20 +345,20 @@ template <typename C> void basic_buffer<C>::Write(
|
||||
w << s[i];
|
||||
}
|
||||
// load all remained attributes to clean up
|
||||
for (; lb != itsFormat.end(); ++lb)
|
||||
LoadAttribute(w, lb->Value);
|
||||
for (; lb != m_format.end(); ++lb)
|
||||
loadAttribute(w, lb->Value);
|
||||
}
|
||||
else
|
||||
w << *this;
|
||||
}
|
||||
|
||||
template <typename C> void basic_buffer<C>::Clear()
|
||||
template <typename C> void basic_buffer<C>::clear()
|
||||
{
|
||||
itsString.clear();
|
||||
itsFormat.clear();
|
||||
m_string.clear();
|
||||
m_format.clear();
|
||||
}
|
||||
|
||||
template <typename C> void basic_buffer<C>::LoadAttribute(Window &w, short value) const
|
||||
template <typename C> void basic_buffer<C>::loadAttribute(Window &w, short value) const
|
||||
{
|
||||
if (value < fmtNone)
|
||||
w << Color(value);
|
||||
@@ -388,9 +369,9 @@ template <typename C> void basic_buffer<C>::LoadAttribute(Window &w, short value
|
||||
template <typename C> basic_buffer<C> &basic_buffer<C>::operator<<(Color color)
|
||||
{
|
||||
FormatPos f;
|
||||
f.Position = itsString.length();
|
||||
f.Position = m_string.length();
|
||||
f.Value = color;
|
||||
itsFormat.push_back(f);
|
||||
m_format.push_back(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -401,13 +382,13 @@ template <typename C> basic_buffer<C> &basic_buffer<C>::operator<<(Format f)
|
||||
|
||||
template <typename C> basic_buffer<C> &basic_buffer<C>::operator<<(const basic_buffer<C> &buf)
|
||||
{
|
||||
size_t length = itsString.length();
|
||||
itsString += buf.itsString;
|
||||
std::list<FormatPos> tmp = buf.itsFormat;
|
||||
size_t length = m_string.length();
|
||||
m_string += buf.m_string;
|
||||
std::list<FormatPos> tmp = buf.m_format;
|
||||
if (length)
|
||||
for (auto it = tmp.begin(); it != tmp.end(); ++it)
|
||||
it->Position += length;
|
||||
itsFormat.merge(tmp);
|
||||
m_format.merge(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user