forked from dotnet/ResXResourceManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextExtensions.cs
153 lines (123 loc) · 4.45 KB
/
TextExtensions.cs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
namespace tomenglertde.ResXManager.Model
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
public static class TextExtensions
{
private const string Quote = "\"";
/// <summary>
/// The text column separator
/// </summary>
private const char TextColumnSeparator = '\t';
[NotNull]
public static string ToTextString([NotNull, ItemNotNull] this IList<IList<string>> table, char separator = TextColumnSeparator)
{
if ((table.Count == 1) && (table[0] != null) && (table[0].Count == 1) && string.IsNullOrWhiteSpace(table[0][0]))
return Quote + (table[0][0] ?? string.Empty) + Quote;
return string.Join(Environment.NewLine, table.Select(line => string.Join(separator.ToString(), line.Select(cell => Quoted(cell, separator)))));
}
[NotNull]
private static string Quoted([CanBeNull] string value, char separator)
{
if (string.IsNullOrEmpty(value))
return string.Empty;
if (value.Any(IsLineFeed) || value.Contains(separator) || value.StartsWith(Quote, StringComparison.Ordinal))
{
return Quote + value.Replace(Quote, Quote + Quote) + Quote;
}
return value;
}
[CanBeNull, ItemNotNull]
internal static IList<IList<string>> ParseTable([NotNull] this string text, char separator = TextColumnSeparator)
{
var table = new List<IList<string>>();
using (var reader = new StringReader(text))
{
while (reader.Peek() != -1)
{
table.Add(ReadTableLine(reader, separator));
}
}
if (!table.Any())
return null;
var headerColumns = table.First();
return table.Any(columns => columns?.Count != headerColumns?.Count) ? null : table;
}
[NotNull, ItemNotNull]
private static IList<string> ReadTableLine([NotNull] TextReader reader, char separator)
{
var columns = new List<string>();
while (true)
{
columns.Add(ReadTableColumn(reader, separator));
if ((char)reader.Peek() == separator)
{
reader.Read();
continue;
}
while (IsLineFeed(reader.Peek()))
{
reader.Read();
}
break;
}
return columns;
}
[NotNull]
internal static string ReadTableColumn([NotNull] TextReader reader, char separator)
{
var stringBuilder = new StringBuilder();
int nextChar;
if (IsDoubleQuote(reader.Peek()))
{
reader.Read();
while ((nextChar = reader.Read()) != -1)
{
if (IsDoubleQuote(nextChar))
{
if (IsDoubleQuote(reader.Peek()))
{
reader.Read();
stringBuilder.Append((char)nextChar);
}
else
{
break;
}
}
else
{
stringBuilder.Append((char)nextChar);
}
}
}
else
{
while ((nextChar = reader.Peek()) != -1)
{
if (IsLineFeed(nextChar) || (nextChar == separator))
break;
reader.Read();
stringBuilder.Append((char)nextChar);
}
}
return stringBuilder.ToString();
}
private static bool IsDoubleQuote(int c)
{
return (c == '"');
}
private static bool IsLineFeed(int c)
{
return (c == '\r') || (c == '\n');
}
private static bool IsLineFeed(char c)
{
return IsLineFeed((int)c);
}
}
}