diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-11-01 07:58:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 07:58:27 +0100 |
commit | f8ed1144dfb0497d7b6f7b8254cf509596682a9f (patch) | |
tree | fd3549fc53848ff2cda8d44ab007aa7fa85509b4 /core/ustring.cpp | |
parent | 7a5594bdb79f0859ce67166b7fb6ed89098e5bcc (diff) | |
parent | 0804dd53364107001f31948a9ec9fe331bc92a4a (diff) |
Merge pull request #12025 from leezh/dedent
Added String::dedent() to remove text indentation
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r-- | core/ustring.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 80881f1adb..415494ddc8 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2757,6 +2757,48 @@ CharType String::ord_at(int p_idx) const { return operator[](p_idx); } +String String::dedent() const { + + String new_string; + String indent; + bool has_indent = false; + bool has_text = false; + int line_start = 0; + int indent_stop = -1; + + for (int i = 0; i < length(); i++) { + + CharType c = operator[](i); + if (c == '\n') { + if (has_text) + new_string += substr(indent_stop, i - indent_stop); + new_string += "\n"; + has_text = false; + line_start = i + 1; + indent_stop = -1; + } else if (!has_text) { + if (c > 32) { + has_text = true; + if (!has_indent) { + has_indent = true; + indent = substr(line_start, i - line_start); + indent_stop = i; + } + } + if (has_indent && indent_stop < 0) { + int j = i - line_start; + if (j >= indent.length() || c != indent[j]) + indent_stop = i; + } + } + } + + if (has_text) + new_string += substr(indent_stop, length() - indent_stop); + + return new_string; +} + String String::strip_edges(bool left, bool right) const { int len = length(); |