Skip to content

Commit 43994da

Browse files
committed
- string extension updated
1 parent 9a8f1bf commit 43994da

File tree

2 files changed

+137
-102
lines changed

2 files changed

+137
-102
lines changed

Core.Common.Standard/Extension/ListExtension.cs

Lines changed: 106 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,123 +3,146 @@
33
using System.Diagnostics;
44
using System.Linq;
55

6-
namespace KY.Core
6+
namespace KY.Core;
7+
8+
public static class ListExtension
79
{
8-
public static class ListExtension
10+
[DebuggerHidden]
11+
public static IList<T> AddRange<T>(this IList<T> list, IEnumerable<T> items)
912
{
10-
[DebuggerHidden]
11-
public static IList<T> AddRange<T>(this IList<T> list, IEnumerable<T> items)
13+
foreach (T item in items)
1214
{
13-
foreach (T item in items)
14-
{
15-
list.Add(item);
16-
}
17-
return list;
15+
list.Add(item);
1816
}
17+
return list;
18+
}
1919

20-
[DebuggerHidden]
21-
public static List<T> YieldList<T>(this T item)
22-
{
23-
return new List<T>(item.Yield());
24-
}
20+
[DebuggerHidden]
21+
public static List<T> YieldList<T>(this T item)
22+
{
23+
return new List<T>(item.Yield());
24+
}
2525

26-
[DebuggerHidden]
27-
public static IList<T> InsertRangeReverse<T>(this IList<T> list, int index, IEnumerable<T> items)
26+
[DebuggerHidden]
27+
public static IList<T> InsertRangeReverse<T>(this IList<T> list, int index, IEnumerable<T> items)
28+
{
29+
foreach (T item in items)
2830
{
29-
foreach (T item in items)
30-
{
31-
list.Insert(index, item);
32-
}
33-
return list;
31+
list.Insert(index, item);
3432
}
33+
return list;
34+
}
3535

36-
[DebuggerHidden]
37-
public static IList<T> InsertRange<T>(this IList<T> list, int index, IEnumerable<T> items)
36+
[DebuggerHidden]
37+
public static IList<T> InsertRange<T>(this IList<T> list, int index, IEnumerable<T> items)
38+
{
39+
foreach (T item in items)
3840
{
39-
foreach (T item in items)
40-
{
41-
list.Insert(index++, item);
42-
}
43-
return list;
41+
list.Insert(index++, item);
4442
}
43+
return list;
44+
}
4545

46-
[DebuggerHidden]
47-
public static IList<T> RemoveRange<T>(this IList<T> list, IEnumerable<T> items)
46+
[DebuggerHidden]
47+
public static IList<T> RemoveRange<T>(this IList<T> list, IEnumerable<T> items)
48+
{
49+
foreach (T item in items)
4850
{
49-
foreach (T item in items)
50-
{
51-
list.Remove(item);
52-
}
53-
return list;
51+
list.Remove(item);
5452
}
53+
return list;
54+
}
5555

56-
[DebuggerHidden]
57-
public static IList<T> ReplaceRange<T>(this IList<T> list, IEnumerable<T> items)
58-
{
59-
return list.RemoveRange(items).AddRange(items);
60-
}
56+
[DebuggerHidden]
57+
public static IList<T> ReplaceRange<T>(this IList<T> list, IEnumerable<T> items)
58+
{
59+
return list.RemoveRange(items).AddRange(items);
60+
}
6161

62-
[DebuggerHidden]
63-
public static IList<T> Replace<T>(this IList<T> list, T replace, T with)
62+
[DebuggerHidden]
63+
public static IList<T> Replace<T>(this IList<T> list, T replace, T with)
64+
{
65+
int index = list.IndexOf(replace);
66+
if (index < 0)
6467
{
65-
int index = list.IndexOf(replace);
66-
if (index < 0)
67-
throw new InvalidOperationException("Item to replace not found in list");
68-
69-
list[index] = with;
70-
return list;
68+
throw new InvalidOperationException("Item to replace not found in list");
7169
}
7270

73-
[DebuggerHidden]
74-
public static T Min<T>(this IList<T> source, Func<T, T> selector, T defaultValue)
75-
{
76-
if (source == null || source.Count == 0)
77-
return defaultValue;
78-
return source.Min(selector);
79-
}
71+
list[index] = with;
72+
return list;
73+
}
8074

81-
[DebuggerHidden]
82-
public static TSource MinBy<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> selector, TSource defaultValue)
75+
[DebuggerHidden]
76+
public static T Min<T>(this IList<T> source, Func<T, T> selector, T defaultValue)
77+
{
78+
if (source == null || source.Count == 0)
8379
{
84-
if (source == null || source.Count == 0)
85-
return defaultValue;
86-
return source.MinBy(selector);
80+
return defaultValue;
8781
}
82+
return source.Min(selector);
83+
}
8884

89-
[DebuggerHidden]
90-
public static TKey MinValue<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> selector, TKey defaultValue)
85+
[DebuggerHidden]
86+
public static TSource MinBy<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> selector, TSource defaultValue)
87+
{
88+
if (source == null || source.Count == 0)
9189
{
92-
if (source == null || source.Count == 0)
93-
return defaultValue;
94-
return selector(source.MinBy(selector));
90+
return defaultValue;
9591
}
92+
return source.MinBy(selector);
93+
}
9694

97-
[DebuggerHidden]
98-
public static TKey MaxValue<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> selector, TKey defaultValue)
95+
[DebuggerHidden]
96+
public static TKey MinValue<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> selector, TKey defaultValue)
97+
{
98+
if (source == null || source.Count == 0)
9999
{
100-
if (source == null || source.Count == 0)
101-
return defaultValue;
102-
return selector(source.MaxBy(selector));
100+
return defaultValue;
103101
}
102+
return selector(source.MinBy(selector));
103+
}
104104

105-
public static T Dequeue<T>(this IList<T> list)
105+
[DebuggerHidden]
106+
public static TKey MaxValue<TSource, TKey>(this IList<TSource> source, Func<TSource, TKey> selector, TKey defaultValue)
107+
{
108+
if (source == null || source.Count == 0)
106109
{
107-
T first = list.FirstOrDefault();
108-
list.Remove(first);
109-
return first;
110+
return defaultValue;
110111
}
112+
return selector(source.MaxBy(selector));
113+
}
114+
115+
public static T Dequeue<T>(this IList<T> list)
116+
{
117+
T first = list.FirstOrDefault();
118+
list.Remove(first);
119+
return first;
120+
}
111121

112-
public static IList<T> Unique<T>(this IList<T> source)
122+
public static IList<T> Unique<T>(this IList<T> source)
123+
{
124+
List<T> list = new();
125+
foreach (T entry in source)
113126
{
114-
List<T> list = new List<T>();
115-
foreach (T entry in source)
127+
if (!list.Contains(entry))
116128
{
117-
if (!list.Contains(entry))
118-
{
119-
list.Add(entry);
120-
}
129+
list.Add(entry);
121130
}
122-
return list;
123131
}
132+
return list;
133+
}
134+
135+
public static void AddIfNotExists<T>(this IList<T> list, IEnumerable<T> entries)
136+
{
137+
entries.ForEach(list.AddIfNotExists);
138+
}
139+
140+
public static void AddIfNotExists<T>(this IList<T> list, T entry)
141+
{
142+
if (list.Contains(entry))
143+
{
144+
return;
145+
}
146+
list.Add(entry);
124147
}
125-
}
148+
}
Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
using System.Text;
22

3-
namespace KY.Core.Extension
3+
namespace KY.Core.Extension;
4+
5+
public static class StringBuilderExtension
46
{
5-
public static class StringBuilderExtension
7+
public static StringBuilder TrimEnd(this StringBuilder builder)
68
{
7-
public static StringBuilder TrimEnd(this StringBuilder builder)
9+
if (builder == null || builder.Length == 0)
810
{
9-
if (builder == null || builder.Length == 0)
10-
{
11-
return builder;
12-
}
13-
int index = builder.Length - 1;
14-
for (; index >= 0; index--)
15-
{
16-
if (!char.IsWhiteSpace(builder[index]))
17-
{
18-
break;
19-
}
20-
}
21-
if (index < builder.Length - 1)
11+
return builder;
12+
}
13+
int index = builder.Length - 1;
14+
for (; index >= 0; index--)
15+
{
16+
if (!char.IsWhiteSpace(builder[index]))
2217
{
23-
builder.Length = index + 1;
18+
break;
2419
}
25-
return builder;
2620
}
21+
if (index < builder.Length - 1)
22+
{
23+
builder.Length = index + 1;
24+
}
25+
return builder;
26+
}
27+
28+
/// <summary>
29+
/// Appends a value to the builder. When the string builder is not empty, the separator will added first
30+
/// </summary>
31+
public static StringBuilder AppendSeparated(this StringBuilder builder, string separator, string value)
32+
{
33+
if (builder.Length > 0)
34+
{
35+
builder.Append(separator);
36+
}
37+
builder.Append(value);
38+
return builder;
2739
}
28-
}
40+
}

0 commit comments

Comments
 (0)