-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
49 lines (41 loc) · 1.35 KB
/
StringExtensions.cs
File metadata and controls
49 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
namespace Pluton.Core
{
using System;
using System.Linq;
using System.Collections.Generic;
public static class StringExtensions
{
public static string JsonPretty(this string self, string indent = "\t")
{
return String.Join(Environment.NewLine, self.yieldPretty(indent).ToArray()).TrimStart('\n', '\r', ' ', '\t').Replace(":",
": ");
}
static IEnumerable<string> yieldPretty(this string json, string indent)
{
int depth = 0;
foreach (string line in json.Replace("{", "{\n").Replace("}", "\n}").Replace(",\"", ",\n\"").Replace("[", "[\n").Replace("]", "\n]").Replace("},{", "},\n{").Replace("],[", "],\n[").Split('\n')) {
if (line.Contains("}") || line.Contains("]"))
depth -= 1;
yield return indent.Multiply(depth) + line;
if (line.Contains("{") || line.Contains("["))
depth += 1;
}
}
public static string Multiply(this string self, int multiply)
{
if (multiply < 0)
throw new ArgumentOutOfRangeException("multiply",
multiply, $"Can't multiply a string by x{multiply}");
string result = String.Empty;
for (int i = 0; i < multiply; i++)
result += self;
return result;
}
public static string QuoteSafe(this string self)
{
self = self.Replace("\"", "\\\"");
self = self.TrimEnd(new char[] { '\\' });
return "\"" + self + "\"";
}
}
}