From 2047676a0e0c01e01188d48b33720b7e2b5c385a Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Fri, 7 Jul 2023 14:57:56 -0400 Subject: [PATCH] Added string sections. --- Changelog.md | 1 + Nerd_STF/Extensions/StringExtension.cs | 101 +++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Nerd_STF/Extensions/StringExtension.cs diff --git a/Changelog.md b/Changelog.md index 73565e7..5501c60 100644 --- a/Changelog.md +++ b/Changelog.md @@ -77,6 +77,7 @@ + ZScore(Equation, Equation, Equation) + InvokeMethod(Equation, MethodInfo, object?[]?) + InvokeMathMethod(Equation, string, object?[]?) + + StringExtension + Helpers + CordicHelper + MathfHelper diff --git a/Nerd_STF/Extensions/StringExtension.cs b/Nerd_STF/Extensions/StringExtension.cs new file mode 100644 index 0000000..d19df3f --- /dev/null +++ b/Nerd_STF/Extensions/StringExtension.cs @@ -0,0 +1,101 @@ +namespace Nerd_STF.Extensions; + +public static class StringExtension +{ + public static string? GetSection(this string str, string prefix, bool includeFix = true, int startIndex = 0, + int? endIndex = null) + { + endIndex ??= str.Length; + + int start = str.IndexOf(prefix, startIndex); + if (start == -1 || start > endIndex.Value) return null; + + int end = str.IndexOf(prefix, start + prefix.Length); + if (end == -1) end = str.Length; + else if (end > endIndex.Value) end = endIndex.Value; + + if (includeFix) + { + start += prefix.Length; + if (start > end) return null; + } + + return str[start..end]; + } + public static string? GetSection(this string str, string prefix, string suffix, bool includeFix = true, + int startIndex = 0, int? endIndex = null) + { + endIndex ??= str.Length; + + int start = str.IndexOf(prefix, startIndex); + if (start == -1 || start > endIndex.Value) return null; + + int end = str.IndexOf(suffix, start + prefix.Length); + if (end == -1) return null; + else if (end > endIndex.Value) end = endIndex.Value; + + if (includeFix) start += prefix.Length; + else end += suffix.Length; + + if (start > end) return null; + + return str[start..end]; + } + + public static string[] GetSections(this string str, string prefix, bool includeFix = true, int startIndex = 0, + int? endIndex = null) + { + endIndex ??= str.Length; + + List sections = new(); + for (int i = startIndex; i < endIndex && i < str.Length; ) + { + int start = str.IndexOf(prefix, startIndex); + if (start == -1 || start > endIndex.Value) break; + + int end = str.IndexOf(prefix, start + prefix.Length); + if (end == -1) end = str.Length; + else if (end > endIndex.Value) end = endIndex.Value; + + if (includeFix) + { + start += prefix.Length; + if (start > end) break; + } + + sections.Add(str[start..end]); + i = end; + } + + return sections.ToArray(); + } + public static string[] GetSections(this string str, string prefix, string suffix, bool includeFix = true, int startIndex = 0, + int? endIndex = null) + { + endIndex ??= str.Length; + + List sections = new(); + for (int i = startIndex; i < endIndex && i < str.Length; ) + { + endIndex ??= str.Length; + + int start = str.IndexOf(prefix, i); + if (start == -1 || start > endIndex.Value) break; + + int end = str.IndexOf(suffix, start + prefix.Length); + if (end == -1) break; + else if (end > endIndex.Value) end = endIndex.Value; + + if (includeFix) start += prefix.Length; + else end += suffix.Length; + + if (start > end) break; + + i = end; + + sections.Add(str[start..end]); + } + + return sections.ToArray(); + } +}