diff options
author | Andreas Haas <Hinsbart@users.noreply.github.com> | 2017-10-03 00:13:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-03 00:13:40 +0200 |
commit | b0194a33f65970948ef66819913bf3034a3a22e8 (patch) | |
tree | 35e21b53b10bbd525506bb5b72b7f89214e1234f /modules/mono/editor/GodotSharpTools/StringExtensions.cs | |
parent | 5cd68abf8896fd86a33c048d6fece61c3cd3f8e5 (diff) | |
parent | d5caf71c3fcdeb422d1b0ea97a836fcdb57a8713 (diff) |
Merge pull request #11739 from neikeq/tengo-el-mono
Moved mono module here
Diffstat (limited to 'modules/mono/editor/GodotSharpTools/StringExtensions.cs')
-rw-r--r-- | modules/mono/editor/GodotSharpTools/StringExtensions.cs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/modules/mono/editor/GodotSharpTools/StringExtensions.cs b/modules/mono/editor/GodotSharpTools/StringExtensions.cs new file mode 100644 index 0000000000..b66c86f8ce --- /dev/null +++ b/modules/mono/editor/GodotSharpTools/StringExtensions.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; + +namespace GodotSharpTools +{ + public static class StringExtensions + { + public static string RelativeToPath(this string path, string dir) + { + // Make sure the directory ends with a path separator + dir = Path.Combine(dir, " ").TrimEnd(); + + if (Path.DirectorySeparatorChar == '\\') + dir = dir.Replace("/", "\\") + "\\"; + + Uri fullPath = new Uri(Path.GetFullPath(path), UriKind.Absolute); + Uri relRoot = new Uri(Path.GetFullPath(dir), UriKind.Absolute); + + return relRoot.MakeRelativeUri(fullPath).ToString(); + } + + public static string NormalizePath(this string path) + { + bool rooted = path.IsAbsolutePath(); + + path = path.Replace('\\', '/'); + + string[] parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + + path = string.Join(Path.DirectorySeparatorChar.ToString(), parts).Trim(); + + return rooted ? Path.DirectorySeparatorChar.ToString() + path : path; + } + + private static readonly string driveRoot = Path.GetPathRoot(Environment.CurrentDirectory); + + public static bool IsAbsolutePath(this string path) + { + return path.StartsWith("/") || path.StartsWith("\\") || path.StartsWith(driveRoot); + } + + public static string CsvEscape(this string value, char delimiter = ',') + { + bool hasSpecialChar = value.IndexOfAny(new char[] { '\"', '\n', '\r', delimiter }) != -1; + + if (hasSpecialChar) + return "\"" + value.Replace("\"", "\"\"") + "\""; + + return value; + } + } +} |