diff options
Diffstat (limited to 'modules/mono/glue/cs_files/StringExtensions.cs')
-rw-r--r-- | modules/mono/glue/cs_files/StringExtensions.cs | 108 |
1 files changed, 53 insertions, 55 deletions
diff --git a/modules/mono/glue/cs_files/StringExtensions.cs b/modules/mono/glue/cs_files/StringExtensions.cs index 5c3ceff97d..21090fb68d 100644 --- a/modules/mono/glue/cs_files/StringExtensions.cs +++ b/modules/mono/glue/cs_files/StringExtensions.cs @@ -1,4 +1,5 @@ //using System; + using System; using System.Collections.Generic; using System.Globalization; @@ -43,11 +44,9 @@ namespace Godot { return instance.Substring(prev, i - prev); } - else - { - count++; - prev = i + 1; - } + + count++; + prev = i + 1; } i++; @@ -83,7 +82,7 @@ namespace Godot // </summary> public static string[] Bigrams(this string instance) { - string[] b = new string[instance.Length - 1]; + var b = new string[instance.Length - 1]; for (int i = 0; i < b.Length; i++) { @@ -98,7 +97,7 @@ namespace Godot // </summary> public static string CEscape(this string instance) { - StringBuilder sb = new StringBuilder(string.Copy(instance)); + var sb = new StringBuilder(string.Copy(instance)); sb.Replace("\\", "\\\\"); sb.Replace("\a", "\\a"); @@ -120,7 +119,7 @@ namespace Godot // </summary> public static string CUnescape(this string instance) { - StringBuilder sb = new StringBuilder(string.Copy(instance)); + var sb = new StringBuilder(string.Copy(instance)); sb.Replace("\\a", "\a"); sb.Replace("\\b", "\b"); @@ -143,7 +142,7 @@ namespace Godot public static string Capitalize(this string instance) { string aux = instance.Replace("_", " ").ToLower(); - string cap = string.Empty; + var cap = string.Empty; for (int i = 0; i < aux.GetSliceCount(" "); i++) { @@ -178,13 +177,13 @@ namespace Godot { if (to[to_idx] == 0 && instance[instance_idx] == 0) return 0; // We're equal - else if (instance[instance_idx] == 0) + if (instance[instance_idx] == 0) return -1; // If this is empty, and the other one is not, then we're less... I think? - else if (to[to_idx] == 0) + if (to[to_idx] == 0) return 1; // Otherwise the other one is smaller... - else if (instance[instance_idx] < to[to_idx]) // More than + if (instance[instance_idx] < to[to_idx]) // More than return -1; - else if (instance[instance_idx] > to[to_idx]) // Less than + if (instance[instance_idx] > to[to_idx]) // Less than return 1; instance_idx++; @@ -260,12 +259,12 @@ namespace Godot { int basepos = instance.Find("://"); - string rs = string.Empty; - string @base = string.Empty; + string rs; + var @base = string.Empty; if (basepos != -1) { - int end = basepos + 3; + var end = basepos + 3; rs = instance.Substring(end, instance.Length); @base = instance.Substring(0, end); } @@ -287,7 +286,7 @@ namespace Godot if (sep == -1) return @base; - return @base + rs.substr(0, sep); + return @base + rs.Substr(0, sep); } // <summary> @@ -312,8 +311,8 @@ namespace Godot int hashv = 5381; int c; - while ((c = (int)instance[index++]) != 0) - hashv = ((hashv << 5) + hashv) + c; // hash * 33 + c + while ((c = instance[index++]) != 0) + hashv = (hashv << 5) + hashv + c; // hash * 33 + c return hashv; } @@ -379,7 +378,7 @@ namespace Godot while (instance[src] != 0 && text[tgt] != 0) { - bool match = false; + bool match; if (case_insensitive) { @@ -455,7 +454,10 @@ namespace Godot return false; // Don't start with number plz } - bool valid_char = (instance[i] >= '0' && instance[i] <= '9') || (instance[i] >= 'a' && instance[i] <= 'z') || (instance[i] >= 'A' && instance[i] <= 'Z') || instance[i] == '_'; + bool valid_char = instance[i] >= '0' && + instance[i] <= '9' || instance[i] >= 'a' && + instance[i] <= 'z' || instance[i] >= 'A' && + instance[i] <= 'Z' || instance[i] == '_'; if (!valid_char) return false; @@ -478,7 +480,7 @@ namespace Godot // </summary> public static bool IsValidIpAddress(this string instance) { - string[] ip = instance.split("."); + string[] ip = instance.Split("."); if (ip.Length != 4) return false; @@ -489,7 +491,7 @@ namespace Godot if (!n.IsValidInteger()) return false; - int val = n.to_int(); + int val = n.ToInt(); if (val < 0 || val > 255) return false; } @@ -502,7 +504,7 @@ namespace Godot // </summary> public static string JsonEscape(this string instance) { - StringBuilder sb = new StringBuilder(string.Copy(instance)); + var sb = new StringBuilder(string.Copy(instance)); sb.Replace("\\", "\\\\"); sb.Replace("\b", "\\b"); @@ -551,7 +553,7 @@ namespace Godot case '\0': return instance[0] == 0; case '*': - return ExprMatch(expr + 1, instance, caseSensitive) || (instance[0] != 0 && ExprMatch(expr, instance + 1, caseSensitive)); + return ExprMatch(expr + 1, instance, caseSensitive) || instance[0] != 0 && ExprMatch(expr, instance + 1, caseSensitive); case '?': return instance[0] != 0 && instance[0] != '.' && ExprMatch(expr + 1, instance + 1, caseSensitive); default: @@ -571,7 +573,7 @@ namespace Godot // <summary> // Do a simple case insensitive expression match, using ? and * wildcards (see [method expr_match]). // </summary> - public static bool matchn(this string instance, string expr) + public static bool Matchn(this string instance, string expr) { return instance.ExprMatch(expr, false); } @@ -610,13 +612,13 @@ namespace Godot { if (to[to_idx] == 0 && instance[instance_idx] == 0) return 0; // We're equal - else if (instance[instance_idx] == 0) + if (instance[instance_idx] == 0) return -1; // If this is empty, and the other one is not, then we're less... I think? - else if (to[to_idx] == 0) + if (to[to_idx] == 0) return 1; // Otherwise the other one is smaller.. - else if (char.ToUpper(instance[instance_idx]) < char.ToUpper(to[to_idx])) // More than + if (char.ToUpper(instance[instance_idx]) < char.ToUpper(to[to_idx])) // More than return -1; - else if (char.ToUpper(instance[instance_idx]) > char.ToUpper(to[to_idx])) // Less than + if (char.ToUpper(instance[instance_idx]) > char.ToUpper(to[to_idx])) // Less than return 1; instance_idx++; @@ -724,8 +726,7 @@ namespace Godot { if (instance.Length > 0 && instance[instance.Length - 1] == '/') return instance + file; - else - return instance + "/" + file; + return instance + "/" + file; } // <summary> @@ -771,7 +772,7 @@ namespace Godot if (pos < 0) return string.Empty; - return instance.Substring(pos, (instance.Length - pos)); + return instance.Substring(pos, instance.Length - pos); } public static byte[] Sha256Buffer(this string instance) @@ -824,23 +825,23 @@ namespace Godot } } - return (2.0f * inter) / sum; + return 2.0f * inter / sum; } // <summary> // Split the string by a divisor string, return an array of the substrings. Example "One,Two,Three" will return ["One","Two","Three"] if split by ",". // </summary> - public static string[] split(this string instance, string divisor, bool allow_empty = true) + public static string[] Split(this string instance, string divisor, bool allow_empty = true) { - return instance.Split(new string[] { divisor }, StringSplitOptions.RemoveEmptyEntries); + return instance.Split(new[] { divisor }, StringSplitOptions.RemoveEmptyEntries); } // <summary> // Split the string in floats by using a divisor string, return an array of the substrings. Example "1,2.5,3" will return [1,2.5,3] if split by ",". // </summary> - public static float[] split_floats(this string instance, string divisor, bool allow_empty = true) + public static float[] SplitFloats(this string instance, string divisor, bool allow_empty = true) { - List<float> ret = new List<float>(); + var ret = new List<float>(); int from = 0; int len = instance.Length; @@ -849,7 +850,7 @@ namespace Godot int end = instance.Find(divisor, from); if (end < 0) end = len; - if (allow_empty || (end > from)) + if (allow_empty || end > from) ret.Add(float.Parse(instance.Substring(from))); if (end == len) break; @@ -872,25 +873,22 @@ namespace Godot // <summary> // Return a copy of the string stripped of any non-printable character at the beginning and the end. The optional arguments are used to toggle stripping on the left and right edges respectively. // </summary> - public static string strip_edges(this string instance, bool left = true, bool right = true) + public static string StripEdges(this string instance, bool left = true, bool right = true) { if (left) { if (right) return instance.Trim(non_printable); - else - return instance.TrimStart(non_printable); - } - else - { - return instance.TrimEnd(non_printable); + return instance.TrimStart(non_printable); } + + return instance.TrimEnd(non_printable); } // <summary> // Return part of the string from the position [code]from[/code], with length [code]len[/code]. // </summary> - public static string substr(this string instance, int from, int len) + public static string Substr(this string instance, int from, int len) { return instance.Substring(from, len); } @@ -898,7 +896,7 @@ namespace Godot // <summary> // Convert the String (which is a character array) to PoolByteArray (which is an array of bytes). The conversion is speeded up in comparison to to_utf8() with the assumption that all the characters the String contains are only ASCII characters. // </summary> - public static byte[] to_ascii(this string instance) + public static byte[] ToAscii(this string instance) { return Encoding.ASCII.GetBytes(instance); } @@ -906,7 +904,7 @@ namespace Godot // <summary> // Convert a string, containing a decimal number, into a [code]float[/code]. // </summary> - public static float to_float(this string instance) + public static float ToFloat(this string instance) { return float.Parse(instance); } @@ -914,7 +912,7 @@ namespace Godot // <summary> // Convert a string, containing an integer number, into an [code]int[/code]. // </summary> - public static int to_int(this string instance) + public static int ToInt(this string instance) { return int.Parse(instance); } @@ -922,7 +920,7 @@ namespace Godot // <summary> // Return the string converted to lowercase. // </summary> - public static string to_lower(this string instance) + public static string ToLower(this string instance) { return instance.ToLower(); } @@ -930,7 +928,7 @@ namespace Godot // <summary> // Return the string converted to uppercase. // </summary> - public static string to_upper(this string instance) + public static string ToUpper(this string instance) { return instance.ToUpper(); } @@ -938,7 +936,7 @@ namespace Godot // <summary> // Convert the String (which is an array of characters) to PoolByteArray (which is an array of bytes). The conversion is a bit slower than to_ascii(), but supports all UTF-8 characters. Therefore, you should prefer this function over to_ascii(). // </summary> - public static byte[] to_utf8(this string instance) + public static byte[] ToUtf8(this string instance) { return Encoding.UTF8.GetBytes(instance); } @@ -946,7 +944,7 @@ namespace Godot // <summary> // Return a copy of the string with special characters escaped using the XML standard. // </summary> - public static string xml_escape(this string instance) + public static string XmlEscape(this string instance) { return SecurityElement.Escape(instance); } @@ -954,7 +952,7 @@ namespace Godot // <summary> // Return a copy of the string with escaped characters replaced by their meanings according to the XML standard. // </summary> - public static string xml_unescape(this string instance) + public static string XmlUnescape(this string instance) { return SecurityElement.FromString(instance).Text; } |