From 92d83d4edc79d4d4e4d85d29d9b4cd334f2cbc35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 18 Aug 2013 17:16:35 +0200 Subject: [PATCH] HaikuDepot: Added Insert() and Remove() to TextSpan. --- src/apps/haiku-depot/textview/TextSpan.cpp | 84 +++++++++++++++++++--- src/apps/haiku-depot/textview/TextSpan.h | 12 ++++ 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/src/apps/haiku-depot/textview/TextSpan.cpp b/src/apps/haiku-depot/textview/TextSpan.cpp index 98e39b1bca..a42fb0da47 100644 --- a/src/apps/haiku-depot/textview/TextSpan.cpp +++ b/src/apps/haiku-depot/textview/TextSpan.cpp @@ -9,6 +9,7 @@ TextSpan::TextSpan() : fText(), + fCharCount(0), fStyle() { } @@ -17,6 +18,7 @@ TextSpan::TextSpan() TextSpan::TextSpan(const BString& text, const TextStyle& style) : fText(text), + fCharCount(text.CountChars()), fStyle(style) { } @@ -25,6 +27,7 @@ TextSpan::TextSpan(const BString& text, const TextStyle& style) TextSpan::TextSpan(const TextSpan& other) : fText(other.fText), + fCharCount(other.fCharCount), 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::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) { count += start; start = 0; } - BString subString; - - int32 charCount = fText.CountChars(); - if (start < charCount) { - if (start + count > charCount) - count = charCount - start; - - fText.CopyCharsInto(subString, start, count); + if (start < fCharCount) { + if (start + count > fCharCount) + count = fCharCount - start; + } else { + count = 0; } - - return TextSpan(subString, fStyle); } - diff --git a/src/apps/haiku-depot/textview/TextSpan.h b/src/apps/haiku-depot/textview/TextSpan.h index e9edff8ff7..88bbddcee8 100644 --- a/src/apps/haiku-depot/textview/TextSpan.h +++ b/src/apps/haiku-depot/textview/TextSpan.h @@ -30,10 +30,22 @@ public: inline const TextStyle& Style() const { 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; +private: + void _TruncateInsert(int32& start) const; + void _TruncateRemove(int32& start, + int32& count) const; + private: BString fText; + int32 fCharCount; TextStyle fStyle; };