summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/string/ustring.cpp21
-rw-r--r--core/string/ustring.h1
-rw-r--r--core/variant/variant_call.cpp1
3 files changed, 23 insertions, 0 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index ac8e2ece12..779270fe47 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -3507,6 +3507,27 @@ char32_t String::unicode_at(int p_idx) const {
return operator[](p_idx);
}
+String String::indent(const String &p_prefix) const {
+ String new_string;
+ int line_start = 0;
+
+ for (int i = 0; i < length(); i++) {
+ const char32_t c = operator[](i);
+ if (c == '\n') {
+ if (i == line_start) {
+ new_string += c; // Leave empty lines empty.
+ } else {
+ new_string += p_prefix + substr(line_start, i - line_start + 1);
+ }
+ line_start = i + 1;
+ }
+ }
+ if (line_start != length()) {
+ new_string += p_prefix + substr(line_start);
+ }
+ return new_string;
+}
+
String String::dedent() const {
String new_string;
String indent;
diff --git a/core/string/ustring.h b/core/string/ustring.h
index 396c996050..780515c12e 100644
--- a/core/string/ustring.h
+++ b/core/string/ustring.h
@@ -356,6 +356,7 @@ public:
String left(int p_pos) const;
String right(int p_pos) const;
+ String indent(const String &p_prefix) const;
String dedent() const;
String strip_edges(bool left = true, bool right = true) const;
String strip_escapes() const;
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 82f547e78c..51b9119933 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1411,6 +1411,7 @@ static void _register_variant_builtin_methods() {
bind_method(String, get_basename, sarray(), varray());
bind_method(String, plus_file, sarray("file"), varray());
bind_method(String, unicode_at, sarray("at"), varray());
+ bind_method(String, indent, sarray("prefix"), varray());
bind_method(String, dedent, sarray(), varray());
bind_method(String, hash, sarray(), varray());
bind_method(String, md5_text, sarray(), varray());