HaikuDepot: Added Insert() and Remove() to TextSpan.

This commit is contained in:
Stephan Aßmus 2013-08-18 17:16:35 +02:00
parent bbb3f9ac5e
commit 92d83d4edc
2 changed files with 85 additions and 11 deletions

View File

@ -9,6 +9,7 @@
TextSpan::TextSpan() TextSpan::TextSpan()
: :
fText(), fText(),
fCharCount(0),
fStyle() fStyle()
{ {
} }
@ -17,6 +18,7 @@ TextSpan::TextSpan()
TextSpan::TextSpan(const BString& text, const TextStyle& style) TextSpan::TextSpan(const BString& text, const TextStyle& style)
: :
fText(text), fText(text),
fCharCount(text.CountChars()),
fStyle(style) fStyle(style)
{ {
} }
@ -25,6 +27,7 @@ TextSpan::TextSpan(const BString& text, const TextStyle& style)
TextSpan::TextSpan(const TextSpan& other) TextSpan::TextSpan(const TextSpan& other)
: :
fText(other.fText), fText(other.fText),
fCharCount(other.fCharCount),
fStyle(other.fStyle) fStyle(other.fStyle)
{ {
} }
@ -69,24 +72,83 @@ TextSpan::SetStyle(const TextStyle& style)
} }
bool
TextSpan::Insert(int32 offset, const BString& text)
{
_TruncateInsert(offset);
fText.InsertChars(text, offset);
int32 charCount = fText.CountChars();
bool success = charCount > fCharCount;
fCharCount = charCount;
return success;
}
bool
TextSpan::Remove(int32 start, int32 count)
{
_TruncateRemove(start, count);
if (count > 0) {
fText.RemoveChars(start, count);
int32 charCount = fText.CountChars();
bool success = charCount < fCharCount;
fCharCount = charCount;
return success;
}
return true;
}
TextSpan TextSpan
TextSpan::SubSpan(int32 start, int32 count) const TextSpan::SubSpan(int32 start, int32 count) const
{ {
_TruncateRemove(start, count);
BString subString;
if (count > 0)
fText.CopyCharsInto(subString, start, count);
return TextSpan(subString, fStyle);
}
// #pragma mark - private
void
TextSpan::_TruncateInsert(int32& start) const
{
if (start < 0)
start = 0;
if (start >= fCharCount)
start = fCharCount;
}
void
TextSpan::_TruncateRemove(int32& start, int32& count) const
{
if (count < 0) {
count = 0;
return;
}
if (start < 0) { if (start < 0) {
count += start; count += start;
start = 0; start = 0;
} }
BString subString; if (start < fCharCount) {
if (start + count > fCharCount)
int32 charCount = fText.CountChars(); count = fCharCount - start;
if (start < charCount) { } else {
if (start + count > charCount) count = 0;
count = charCount - start;
fText.CopyCharsInto(subString, start, count);
} }
return TextSpan(subString, fStyle);
} }

View File

@ -30,10 +30,22 @@ public:
inline const TextStyle& Style() const inline const TextStyle& Style() const
{ return fStyle; } { return fStyle; }
inline int32 CharCount() const
{ return fCharCount; }
bool Insert(int32 offset, const BString& text);
bool Remove(int32 start, int32 count);
TextSpan SubSpan(int32 start, int32 count) const; TextSpan SubSpan(int32 start, int32 count) const;
private:
void _TruncateInsert(int32& start) const;
void _TruncateRemove(int32& start,
int32& count) const;
private: private:
BString fText; BString fText;
int32 fCharCount;
TextStyle fStyle; TextStyle fStyle;
}; };