vfs: check userland buffers before reading them.
[haiku.git] / src / apps / haikudepot / textview / TextSpan.cpp
blobf9fbd588e13bbd57624c7d6fa6a1fb7943f7567c
1 /*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include "TextSpan.h"
9 TextSpan::TextSpan()
11 fText(),
12 fCharCount(0),
13 fStyle()
18 TextSpan::TextSpan(const BString& text, const CharacterStyle& style)
20 fText(text),
21 fCharCount(text.CountChars()),
22 fStyle(style)
27 TextSpan::TextSpan(const TextSpan& other)
29 fText(other.fText),
30 fCharCount(other.fCharCount),
31 fStyle(other.fStyle)
36 TextSpan&
37 TextSpan::operator=(const TextSpan& other)
39 fText = other.fText;
40 fCharCount = other.fCharCount;
41 fStyle = other.fStyle;
43 return *this;
47 bool
48 TextSpan::operator==(const TextSpan& other) const
50 return fCharCount == other.fCharCount
51 && fStyle == other.fStyle
52 && fText == other.fText;
56 bool
57 TextSpan::operator!=(const TextSpan& other) const
59 return !(*this == other);
63 void
64 TextSpan::SetText(const BString& text)
66 fText = text;
67 fCharCount = fText.CountChars();
71 void
72 TextSpan::SetStyle(const CharacterStyle& style)
74 fStyle = style;
78 bool
79 TextSpan::Append(const BString& text)
81 return Insert(fCharCount, text);
85 bool
86 TextSpan::Insert(int32 offset, const BString& text)
88 _TruncateInsert(offset);
90 fText.InsertChars(text, offset);
92 int32 charCount = fText.CountChars();
93 bool success = charCount > fCharCount;
94 fCharCount = charCount;
96 return success;
100 bool
101 TextSpan::Remove(int32 start, int32 count)
103 _TruncateRemove(start, count);
105 if (count > 0) {
106 fText.RemoveChars(start, count);
108 int32 charCount = fText.CountChars();
109 bool success = charCount < fCharCount;
110 fCharCount = charCount;
112 return success;
114 return true;
118 TextSpan
119 TextSpan::SubSpan(int32 start, int32 count) const
121 _TruncateRemove(start, count);
123 BString subString;
124 if (count > 0)
125 fText.CopyCharsInto(subString, start, count);
127 return TextSpan(subString, fStyle);
131 // #pragma mark - private
134 void
135 TextSpan::_TruncateInsert(int32& start) const
137 if (start < 0)
138 start = 0;
140 if (start >= fCharCount)
141 start = fCharCount;
145 void
146 TextSpan::_TruncateRemove(int32& start, int32& count) const
148 if (count < 0) {
149 count = 0;
150 return;
153 if (start < 0) {
154 count += start;
155 start = 0;
158 if (start < fCharCount) {
159 if (start + count > fCharCount)
160 count = fCharCount - start;
161 } else {
162 count = 0;