aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorColin Snover2017-10-07 22:01:13 -0500
committerColin Snover2017-11-10 09:57:41 -0600
commitdda0f77bcf093dced1ad833982be8a9e0951a85e (patch)
tree15c83704faf94b687af600e9b265ff46ec5db32e /common
parent216376477a5b3eb35e3f3450461b284ecfed8244 (diff)
downloadscummvm-rg350-dda0f77bcf093dced1ad833982be8a9e0951a85e.tar.gz
scummvm-rg350-dda0f77bcf093dced1ad833982be8a9e0951a85e.tar.bz2
scummvm-rg350-dda0f77bcf093dced1ad833982be8a9e0951a85e.zip
COMMON: Add basic fixed-width word wrap to Common::String
Diffstat (limited to 'common')
-rw-r--r--common/str.cpp38
-rw-r--r--common/str.h11
2 files changed, 49 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());
}
diff --git a/common/str.h b/common/str.h
index ba1e0b8341..fd77fa90c8 100644
--- a/common/str.h
+++ b/common/str.h
@@ -235,6 +235,17 @@ public:
*/
void trim();
+ /**
+ * Wraps the text in the string to the given line maximum. Lines will be
+ * broken at any whitespace character. New lines are assumed to be
+ * represented using '\n'.
+ *
+ * This is a very basic line wrap which does not perform tab stop
+ * calculation, consecutive whitespace collapsing, auto-hyphenation, or line
+ * balancing.
+ */
+ void wordWrap(const uint32 maxLength);
+
uint hash() const;
/**@{