-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathAdminShellBasicExtensions.cs
More file actions
125 lines (108 loc) · 3.5 KB
/
Copy pathAdminShellBasicExtensions.cs
File metadata and controls
125 lines (108 loc) · 3.5 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
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
/*
Copyright (c) 2018-2023 Festo SE & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>
Author: Michael Hoffmeister
This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/
using AasxCompatibilityModels;
using Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
namespace AdminShellNS
{
/// <summary>
/// Holds some extensions which are very fundamental in order to
/// shortcut some hanlding of basic datat types.
/// </summary>
public static class AdminShellBasicExtensions
{
/// <summary>
/// Check if real content is in a string
/// </summary>
public static bool HasContent(this string str)
{
return str != null && str.Trim() != "";
}
public static IEnumerable<T> ForEachSafe<T>(this List<T> list)
{
if (list == null)
yield break;
foreach (var x in list)
yield return x;
}
public static void ForEach<T>(this IList<T> list, Action <T> action)
{
if (list == null)
return;
foreach (var x in list)
action?.Invoke(x);
}
/// <summary>
/// Multiple a string into a iterable list
/// </summary>
public static IEnumerable<T> Times<T>(this T value, int num = 1)
{
for (int i= 0; i < num; i++)
yield return value;
}
/// <summary>
/// Adds an item to a sequence of strings.
/// </summary>
public static IEnumerable<T> Add<T>(this IEnumerable<T> seq, T value)
{
foreach (var s in seq)
yield return s;
yield return value;
}
/// <summary>
/// Adds items to a sequence of strings.
/// </summary>
public static IEnumerable<T> Add<T>(this IEnumerable<T> seq, IEnumerable<T> others)
{
foreach (var s in seq.AsNotNull())
yield return s;
foreach (var s in others.AsNotNull())
yield return s;
}
//public static IEnumerable<T> Times<T>(T value, int num = 1) where T : class
//{
// for(int i = 0; i < num; i++)
// yield return value;
//}
public static string SubstringMax(this string str, int pos, int len)
{
if (!str.HasContent() || str.Length <= pos)
return "";
return str.Substring(pos, Math.Min(len, str.Length));
}
public static void SetIfNoContent(ref string s, string input)
{
if (!input.HasContent())
return;
if (!s.HasContent())
s = input;
}
public static string AddWithDelimiter(this string str, string content, string delimter = "")
{
var res = str;
if (res == null)
return null;
if (res.HasContent())
res += "" + delimter;
res += content;
return res;
}
public static IEnumerable<T> AsNotNull<T>(this IEnumerable<T> original)
{
return original ?? Enumerable.Empty<T>();
}
}
}