diff options
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r-- | core/ustring.cpp | 76 |
1 files changed, 70 insertions, 6 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 8273ed144b..415494ddc8 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -1755,7 +1755,7 @@ static double built_in_strtod(const C *string, /* A decimal ASCII floating-point register int c; int exp = 0; /* Exponent read from "EX" field. */ int fracExp = 0; /* Exponent that derives from the fractional - * part. Under normal circumstatnces, it is + * part. Under normal circumstances, it is * the negative of the number of digits in F. * However, if I is very long, the last digits * of I get dropped (otherwise a long I with a @@ -2332,12 +2332,12 @@ int String::findn(String p_str, int p_from) const { int String::rfind(String p_str, int p_from) const { - //stabilish a limit + // establish a limit int limit = length() - p_str.length(); if (limit < 0) return -1; - //stabilish a starting point + // establish a starting point if (p_from < 0) p_from = limit; else if (p_from > limit) @@ -2347,7 +2347,7 @@ int String::rfind(String p_str, int p_from) const { int len = length(); if (src_len == 0 || len == 0) - return -1; //wont find anything! + return -1; // won't find anything! const CharType *src = c_str(); @@ -2378,12 +2378,12 @@ int String::rfind(String p_str, int p_from) const { } int String::rfindn(String p_str, int p_from) const { - //stabilish a limit + // establish a limit int limit = length() - p_str.length(); if (limit < 0) return -1; - //stabilish a starting point + // establish a starting point if (p_from < 0) p_from = limit; else if (p_from > limit) @@ -2476,6 +2476,11 @@ bool String::begins_with(const char *p_string) const { return *p_string == 0; } +bool String::is_enclosed_in(const String &p_string) const { + + return begins_with(p_string) && ends_with(p_string); +} + bool String::is_subsequence_of(const String &p_string) const { return _base_is_subsequence_of(p_string, false); @@ -2486,6 +2491,11 @@ bool String::is_subsequence_ofi(const String &p_string) const { return _base_is_subsequence_of(p_string, true); } +bool String::is_quoted() const { + + return is_enclosed_in("\"") || is_enclosed_in("'"); +} + bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const { int len = length(); @@ -2747,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(); @@ -3906,6 +3958,18 @@ String String::sprintf(const Array &values, bool *error) const { return formatted; } +String String::quote(String quotechar) const { + return quotechar + *this + quotechar; +} + +String String::unquote() const { + if (!is_quoted()) { + return *this; + } + + return substr(1, length() - 2); +} + #include "translation.h" #ifdef TOOLS_ENABLED |