diff options
Diffstat (limited to 'common/str.cpp')
-rw-r--r-- | common/str.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/common/str.cpp b/common/str.cpp index 3a0fd6a08e..2ef67175cd 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -444,6 +444,44 @@ void String::trim() { } } +void String::wordWrap(const uint32 maxLength) { + if (_size < maxLength) { + return; + } + + makeUnique(); + + enum { kNoSpace = 0xFFFFFFFF }; + + uint32 i = 0; + while (i < _size) { + uint32 lastSpace = kNoSpace; + uint32 x = 0; + while (i < _size && x <= maxLength) { + const char c = _str[i]; + if (c == '\n') { + lastSpace = kNoSpace; + x = 0; + } else { + if (Common::isSpace(c)) { + lastSpace = i; + } + ++x; + } + ++i; + } + + if (x > maxLength) { + if (lastSpace == kNoSpace) { + insertChar('\n', i - 1); + } else { + setChar('\n', lastSpace); + i = lastSpace + 1; + } + } + } +} + uint String::hash() const { return hashit(c_str()); } |