Table of Contents

Class Ext

Namespace
SearchAThing.Ext
Assembly
netcore-ext.dll
public static class Ext
Inheritance
Ext
Inherited Members

Methods

Act<T>(T, Action<T>)

Allow to apply some action on the object inline returning the same object.

eg dxf.Entities.Add(new Line3D().DxfEntity.Act(ent => ent.Color = AciColor.Red))

public static T Act<T>(this T obj, Action<T> setter)

Parameters

obj T
setter Action<T>

Returns

T

Type Parameters

T

Align(string, int, ColumnAlignment)

align given string into given size with alignment specified. resulting string will fit into given size with spaces or truncated if not enough for given size vs str length

public static string Align(this string str, int size, ColumnAlignment align = ColumnAlignment.left)

Parameters

str string
size int
align ColumnAlignment

Returns

string

Clamp(double, double, double)

return clamped number between [min,max] interval

public static double Clamp(this double n, double min, double max)

Parameters

n double

number

min double

min value admissible

max double

max value admissible

Returns

double

n if between [min,max] otherwise min when n less than min or max when n great than max

Clamp(float, float, float)

return clamped number between [min,max] interval

public static float Clamp(this float n, float min, float max)

Parameters

n float

number

min float

min value admissible

max float

max value admissible

Returns

float

n if between [min,max] otherwise min when n less than min or max when n great than max

CompareTol(double, double, double)

public static int CompareTol(this double x, double tol, double y)

Parameters

x double
tol double
y double

Returns

int

ComputeMD5Sum(string)

compute MD5Sum of given input

public static string ComputeMD5Sum(string input)

Parameters

input string

Returns

string

ContainsIgnoreCase(string, string)

check if given string contains the part ( ignoring case )

public static bool ContainsIgnoreCase(this string str, string part)

Parameters

str string
part string

Returns

bool

CreateGetterSetter<T, V>(Expression<Func<T, V>>)

create getter (func) and setter (action) from given lambda expr

public static (Func<T, V> getter, Action<T, V> setter) CreateGetterSetter<T, V>(this Expression<Func<T, V>> expr)

Parameters

expr Expression<Func<T, V>>

Returns

(Func<T, V> getter, Action<T, V> setter)

Type Parameters

T
V

CreateGetterSetter<T, V>(Expression<Func<T, V>>, out MemberExpression)

create getter (func) and setter (action) from given lambda expr, and retrieve member expression

public static (Func<T, V> getter, Action<T, V> setter) CreateGetterSetter<T, V>(this Expression<Func<T, V>> expr, out MemberExpression mExpr)

Parameters

expr Expression<Func<T, V>>
mExpr MemberExpression

Returns

(Func<T, V> getter, Action<T, V> setter)

Type Parameters

T
V

Distinct<T, TKey>(IEnumerable<T>, Func<T, TKey>)

distinct with lambda

public static IEnumerable<T> Distinct<T, TKey>(this IEnumerable<T> lst, Func<T, TKey> keySelector)

Parameters

lst IEnumerable<T>
keySelector Func<T, TKey>

Returns

IEnumerable<T>

Type Parameters

T
TKey

EqualsAutoTol(double, double, double)

Returns true if two numbers are equals using a default tolerance of 1e-6 about the smaller one.

public static bool EqualsAutoTol(this double x, double y, double precision = 1E-06)

Parameters

x double
y double
precision double

Returns

bool

EqualsTol(double, double, double)

true if ( |x-y| LTE tol )

public static bool EqualsTol(this double x, double tol, double y)

Parameters

x double
tol double
y double

Returns

bool

Export(double[])

export to a string ( invariant ) comma separated

public static string Export(this double[] ary)

Parameters

ary double[]

Returns

string

Fn<T, U>(T, Func<T, U>)

Allow to tranform the object into other type.
eg. intvar.Fn((x) => (x == 0) ? "zero" : "non-zero")

public static U Fn<T, U>(this T obj, Func<T, U> fn)

Parameters

obj T
fn Func<T, U>

Returns

U

Type Parameters

T
U

ForEach<T>(IEnumerable<T>, Action<T>)

performs given action on enumerable items

public static void ForEach<T>(this IEnumerable<T> en, Action<T> act)

Parameters

en IEnumerable<T>
act Action<T>

Type Parameters

T

Foreach<T>(IEnumerable<T>, Action<T>)

Allow to apply an action foreach enum objects

public static IEnumerable<T> Foreach<T>(this IEnumerable<T> en, Action<T> action)

Parameters

en IEnumerable<T>
action Action<T>

Returns

IEnumerable<T>

Type Parameters

T

GreatThanOrEqualsTol(double, double, double)

true if (x GT y) AND ( |x-y| LTE tol )

public static bool GreatThanOrEqualsTol(this double x, double tol, double y)

Parameters

x double
tol double
y double

Returns

bool

GreatThanTol(double, double, double)

true if (x GT y) AND NOT ( |x-y| LTE tol )

public static bool GreatThanTol(this double x, double tol, double y)

Parameters

x double
tol double
y double

Returns

bool

HumanReadable(long, bool, long, int)

Returns a human readable bytes length. (eg. 1000, 1K, 1M, 1G, 1T) if onlyBytesUnit is set to false it will enable representation through K, M, G, T suffixes

public static string HumanReadable(this long bytes, bool onlyBytesUnit = true, long bytesMultiple = 1, int decimals = 1)

Parameters

bytes long
onlyBytesUnit bool
bytesMultiple long
decimals int

Returns

string

Import(string)

parse given array of doubles ( invariant ) comma separated

public static double[] Import(this string ary)

Parameters

ary string

Returns

double[]

InvDoubleParse(string)

Invariant culture double parse

public static double InvDoubleParse(this string str)

Parameters

str string

Returns

double

InvariantDate(DateTime, string)

return yyyy-MM-dd representation

public static string InvariantDate(this DateTime dt, string sep = "-")

Parameters

dt DateTime
sep string

Returns

string

InvariantTime(DateTime, string)

return HH:mm:ss representation

public static string InvariantTime(this DateTime dt, string sep = ":")

Parameters

dt DateTime
sep string

Returns

string

InvarianteDateTime(DateTime, string, string)

return yyyy-MM-dd HH:mm.ss representation

public static string InvarianteDateTime(this DateTime dt, string datesep = "-", string timesep = ":")

Parameters

dt DateTime
datesep string
timesep string

Returns

string

IsInRange(double, double, string)

eval if a number fits in given range eg.

  • "[0, 10)" are numbers from 0 (included) to 10 (excluded)
  • "[10, 20]" are numbers from 10 (included) to 20 (included)
  • "(30,)" are numbers from 30 (excluded) to +infinity
public static bool IsInRange(this double nr, double tol, string range)

Parameters

nr double
tol double
range string

Returns

bool

Latest(string, int, string)

convert a string that exceed N given characters length to {prefix}{latest N chars}

public static string Latest(this string str, int last_n_chars, string prefix_if_exceed = "...")

Parameters

str string
last_n_chars int
prefix_if_exceed string

Returns

string

LessThanOrEqualsTol(double, double, double)

true if (x LT y) AND ( |x-y| LTE tol )

public static bool LessThanOrEqualsTol(this double x, double tol, double y)

Parameters

x double
tol double
y double

Returns

bool

LessThanTol(double, double, double)

true if (x LT y) AND NOT ( |x-y| LTE tol )

public static bool LessThanTol(this double x, double tol, double y)

Parameters

x double
tol double
y double

Returns

bool

Lines(string, bool)

Smart line splitter that split a text into lines whatever unix or windows line ending style. By default its remove empty lines.

public static IEnumerable<string> Lines(this string txt, bool removeEmptyLines = true)

Parameters

txt string

string to split into lines

removeEmptyLines bool

If true remove empty lines.

Returns

IEnumerable<string>

MMToEMU(double)

10mm = 360000 EMU
mm = 36000 EMU

public static int MMToEMU(this double mm)

Parameters

mm double

Returns

int

MMToEMU(int)

10mm = 360000 EMU
mm = 36000 EMU

public static int MMToEMU(this int mm)

Parameters

mm int

Returns

int

MMToPt(double)

font size
half_point = hp = 1/144 inch = 25.4/144 mm
pt = hp/2
pt = 225.4/144mm

public static double MMToPt(this double mm)

Parameters

mm double

Returns

double

MMToTwip(double)

convert mm to twip
twip = 1/20 pp = 1/20 * 1/72 in = 1/1440 in = 1/1440 * 25.4 mm
mm = 1440/25.4 twip

public static int MMToTwip(this double mm)

Parameters

mm double

Returns

int

MMToTwip(int)

convert mm to twip
twip = 1/20 pp = 1/20 * 1/72 in = 1/1440 in = 1/1440 * 25.4 mm
mm = 1440/25.4 twip

public static int MMToTwip(this int mm)

Parameters

mm int

Returns

int

MRound(double, double)

Round the given value using the multiple basis

public static double MRound(this double value, double multiple)

Parameters

value double
multiple double

Returns

double

MRound(double, double?)

Round the given value using the multiple basis

public static double MRound(this double value, double? multiple)

Parameters

value double
multiple double?

Returns

double

MRound(double?, double)

Round the given value using the multiple basis if null return null

public static double? MRound(this double? value, double multiple)

Parameters

value double?
multiple double

Returns

double?

Magnitude(double)

Magnitude of given number. (eg. 190 -> 1.9e2 -> 2) (eg. 0.0034 -> 3.4e-3 -> -3)

public static int Magnitude(this double value)

Parameters

value double

Returns

int

MatchesFilter(IEnumerable<string>, string, bool)

Checks whatever fields matches given filter all words in any of inputs. ex. fields={ "abc", "de" } filter="a" results: true ex. fields={ "abc", "de" } filter="a d" results: true ex. fields={ "abc", "de" } filter="a f" results: false autoskips null fields check; returns true if filter empty

public static bool MatchesFilter(this IEnumerable<string> fields, string filter, bool ignoreCase = true)

Parameters

fields IEnumerable<string>
filter string
ignoreCase bool

Returns

bool

Mean(IEnumerable<double>)

Mean of given numbers

public static double Mean(this IEnumerable<double> set)

Parameters

set IEnumerable<double>

Returns

double

Mean(IEnumerable<Vector3>)

mean of given vectors

public static Vector3 Mean(this IEnumerable<Vector3> vectors)

Parameters

vectors IEnumerable<Vector3>

Returns

Vector3

MinMax(IEnumerable<double>)

Retrieve min, max at once.

public static (double min, double max)? MinMax(this IEnumerable<double> values)

Parameters

values IEnumerable<double>

Returns

(double min, double max)?

(min,max) of given set of values.

MinMax(IEnumerable<float>)

Retrieve min, max at once.

public static (float min, float max)? MinMax(this IEnumerable<float> values)

Parameters

values IEnumerable<float>

Returns

(float min, float max)?

(min,max) of given set of values.

NormalizeFilename(string, char)

public static string NormalizeFilename(this string filename, char subst = '_')

Parameters

filename string
subst char

Returns

string

NormalizeWorksheetName(string)

convert invalid worksheet characters :/?*[]' into underscore

public static string NormalizeWorksheetName(this string s)

Parameters

s string

Returns

string

ParseInt(string)

public static int ParseInt(this string s)

Parameters

s string

Returns

int

Pct(double)

convert given percent 0..100 to fiftieths of a Percent

public static int Pct(this double percent)

Parameters

percent double

Returns

int

PtToHalfPoint(double)

font size
hp = 2*pt

public static double PtToHalfPoint(this double pt)

Parameters

pt double

Returns

double

PtToMM(double)

font size
half_point = hp = 1/144 inch = 25.4/144 mm
pt = hp/2

mm = 144/(2*25.4) pt

public static double PtToMM(this double pt)

Parameters

pt double

Returns

double

RegexMatch(string, string, RegexOptions)

retrieve nr. of occurrence of given pattern through regex

public static int RegexMatch(this string s, string pattern, RegexOptions opt = RegexOptions.None)

Parameters

s string
pattern string
opt RegexOptions

Returns

int

Repeat(string, int)

Repeat given string for cnt by concatenate itself

public static string Repeat(this string s, int cnt)

Parameters

s string
cnt int

Returns

string

Round(Vector2, int)

round vector components to given digits

public static Vector2 Round(this Vector2 v, int digits)

Parameters

v Vector2
digits int

Returns

Vector2

Round(Vector3, int)

round vector components to given digits

public static Vector3 Round(this Vector3 v, int digits)

Parameters

v Vector3
digits int

Returns

Vector3

Round(Vector4, int)

round vector components to given digits

public static Vector4 Round(this Vector4 v, int digits)

Parameters

v Vector4
digits int

Returns

Vector4

RouteFirst<T>(IEnumerable<T>, T)

from given elements return the sequence starting from wantedFirstElement and continue until end then restart from begin until wantedFirstElement excluded pre: wantedFirstElement must in the list unit test

public static IEnumerable<T> RouteFirst<T>(this IEnumerable<T> lst, T wantedFirstElement)

Parameters

lst IEnumerable<T>
wantedFirstElement T

Returns

IEnumerable<T>

Type Parameters

T

Sign(double)

returns 1.0 if n>=0 -1 otherwise

public static double Sign(this double n)

Parameters

n double

Returns

double

Sign(double, double)

returns 0,+1,-1 depending on the sign. (0) : if given number EqualsTol(zeroTol, 0) (+1) : if given number positive; (-1) : if given number negative;

public static int Sign(this double n, double zeroTol)

Parameters

n double

number to test

zeroTol double

tolerance to consider it zero

Returns

int

0,+1,-1

Sign(int)

returns 1.0 if n>=0 -1 otherwise

public static double Sign(this int n)

Parameters

n int

Returns

double

SmartDoubleParse(string)

Parse string that represent number without knowing current culture so that it can parse "1.2" or "1,2" equivalent to 1.2 it will throw error more than one dot or comma found

public static double SmartDoubleParse(this string str)

Parameters

str string

Returns

double

Sort<TSource, TKey>(ObservableCollection<TSource>, Func<TSource, TKey>, bool)

sort obc

public static void Sort<TSource, TKey>(this ObservableCollection<TSource> obc, Func<TSource, TKey> keySelector, bool descending = false)

Parameters

obc ObservableCollection<TSource>

observable collection to sort

keySelector Func<TSource, TKey>

fn to select key

descending bool

if true then sort descending

Type Parameters

TSource
TKey

Split(string, string)

split string with given separator string

public static string[] Split(this string str, string sepStr)

Parameters

str string
sepStr string

Returns

string[]

SplitBy<T, V>(IEnumerable<T>, Func<T, V>)

Separate into a set of lists given items ensuring no same key object exists in the same list.

public static List<List<T>> SplitBy<T, V>(this IEnumerable<T> set, Func<T, V> splitBySelector)

Parameters

set IEnumerable<T>

Input element set

splitBySelector Func<T, V>

Function that retrieve key from item.

Returns

List<List<T>>

List of list of items where no dup key in the same list.

Type Parameters

T

Type of items to split.

V

Type of item key.

Stringify(double, int)

Return an invariant string representation rounded to given dec.

public static string Stringify(this double x, int dec)

Parameters

x double
dec int

Returns

string

StripBegin(string, char, bool)

Returns the given string stripped from the given part if exists at beginning.

public static string StripBegin(this string str, char c, bool ignoreCase = false)

Parameters

str string
c char
ignoreCase bool

Returns

string

StripBegin(string, string, bool)

Returns the given string stripped from the given part if exists at beginning.

public static string StripBegin(this string str, string partToStrip, bool ignoreCase = false)

Parameters

str string
partToStrip string
ignoreCase bool

Returns

string

StripEnd(string, char, bool)

Returns the given string stripped from the given part if exists at end.

public static string StripEnd(this string str, char c, bool ignoreCase = false)

Parameters

str string
c char
ignoreCase bool

Returns

string

StripEnd(string, string, bool)

Returns the given string stripped from the given part if exists at end.

public static string StripEnd(this string str, string partToStrip, bool ignoreCase = false)

Parameters

str string
partToStrip string
ignoreCase bool

Returns

string

TableFormat(IEnumerable<IEnumerable<string>>, IEnumerable<string>?, IEnumerable<ColumnAlignment>?, int)

formats given rows into a table aligning by columns. optional column spacing and alignment can be specified.

public static string TableFormat(this IEnumerable<IEnumerable<string>> src, IEnumerable<string>? headers = null, IEnumerable<ColumnAlignment>? aligns = null, int columnSpacing = 3)

Parameters

src IEnumerable<IEnumerable<string>>
headers IEnumerable<string>
aligns IEnumerable<ColumnAlignment>
columnSpacing int

Returns

string

ToBytes<T>(T)

retrieve a binary representation of given struct

public static byte[] ToBytes<T>(this T obj) where T : struct

Parameters

obj T

Returns

byte[]

Type Parameters

T

ToDeg(double)

convert given angle(rad) to deg

public static double ToDeg(this double angleRad)

Parameters

angleRad double

angle (rad)

Returns

double

angle (deg)

ToDeg(float)

convert given angle(rad) to deg

public static float ToDeg(this float angleRad)

Parameters

angleRad float

angle (rad)

Returns

float

angle (deg)

ToHashSetExt<T>(IEnumerable<T>)

Create an HashSet from given enumerable. (net standard 2.0 ext)

public static HashSet<T> ToHashSetExt<T>(this IEnumerable<T> set)

Parameters

set IEnumerable<T>

Returns

HashSet<T>

Type Parameters

T

ToObservableCollection<T>(IEnumerable<T>)

Convert given enumerable to observable collection

public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> en)

Parameters

en IEnumerable<T>

Returns

ObservableCollection<T>

Type Parameters

T

ToRad(double)

convert given angle(deg) to rad

public static double ToRad(this double angleGrad)

Parameters

angleGrad double

angle (deg)

Returns

double

angle (radians)

ToRad(float)

convert given angle(deg) to rad

public static float ToRad(this float angleDeg)

Parameters

angleDeg float

angle (deg)

Returns

float

angle (radians)

ToReadOnlyList<T>(IEnumerable<T>)

Convert given IEnumerable into IReadOnlyList with type convert if given argument was already a IReadOnlyList or creating a new object List and returning as IReadOnlyList

public static IReadOnlyList<T> ToReadOnlyList<T>(this IEnumerable<T> en)

Parameters

en IEnumerable<T>

enumerable to convert to IReadOnlyList

Returns

IReadOnlyList<T>

IReadOnlyList ( may the same object reference or a new depending if the argument was already a IReadOnlyList or not )

Type Parameters

T

typename

ToString(double, int)

format number so that show given significant digits. (eg. 2.03 with significantDigits=4 create "2.0300")

public static string ToString(this double d, int significantDigits)

Parameters

d double
significantDigits int

Returns

string

ToStringWrapper(StringBuilder)

public static StringWrapper ToStringWrapper(this StringBuilder sb)

Parameters

sb StringBuilder

Returns

StringWrapper

ToVector3(Vector2, float)

convert x,y to x,y,z

public static Vector3 ToVector3(this Vector2 v, float z = 0)

Parameters

v Vector2
z float

Returns

Vector3

TrimNonNumericCharacters(string)

removes all characters that aren't 0-9 dot or comma

public static string TrimNonNumericCharacters(this string s)

Parameters

s string

Returns

string

TwipToMM(int)

convert twip to mm
mm = 1440/25.4 twip
twip = 25.4/1440 mm

public static double TwipToMM(this int twip)

Parameters

twip int

Returns

double

TwipToMM(uint)

convert twip to mm
mm = 1440/25.4 twip
twip = 25.4/1440 mm

public static double TwipToMM(this uint twip)

Parameters

twip uint

Returns

double

UnspecifiedAsLocalDateTime(DateTime)

if given dt has unspecified kind rectified to Local without any conversion

public static DateTime UnspecifiedAsLocalDateTime(this DateTime dt)

Parameters

dt DateTime

Returns

DateTime

UnspecifiedAsUTCDateTime(DateTime)

if given dt has unspecified kind rectifies to UTC without any conversion

public static DateTime UnspecifiedAsUTCDateTime(this DateTime dt)

Parameters

dt DateTime

Returns

DateTime

WildcardMatch(string, string, bool)

return true if given string matches the given pattern the asterisk '*' character replace any group of chars the question '?' character replace any single character

public static bool WildcardMatch(this string str, string pattern, bool caseSentitive = true)

Parameters

str string
pattern string
caseSentitive bool

Returns

bool

WildcardToRegex(string)

convert wildcard pattern to regex the asterisk '*' character replace any group of chars the question '?' character replace any single character

public static string WildcardToRegex(this string pattern)

Parameters

pattern string

Returns

string

WithIndexIsLast<T>(IEnumerable<T>)

enumerable extension to enumerate itself into an (item, idx, isLast) set

public static IEnumerable<(T item, int idx, bool isLast)> WithIndexIsLast<T>(this IEnumerable<T> en)

Parameters

en IEnumerable<T>

Returns

IEnumerable<(T item, int idx, bool isLast)>

Type Parameters

T

Examples

\snippet with-index-is-last/Program.cs example

WithIndex<T>(IEnumerable<T>)

enumerable extension to enumerate itself into an (item, idx) set

public static IEnumerable<(T item, int idx)> WithIndex<T>(this IEnumerable<T> en)

Parameters

en IEnumerable<T>

Returns

IEnumerable<(T item, int idx)>

Type Parameters

T

WithNextPrimitive<T>(IEnumerable<T>, bool)

enumerate given items returning a tuple with null ( for last hit ) next element

public static IEnumerable<(T item, T? next, int itemIdx, bool isLast)> WithNextPrimitive<T>(this IEnumerable<T> en, bool repeatFirstAtEnd = false) where T : struct

Parameters

en IEnumerable<T>
repeatFirstAtEnd bool

Returns

IEnumerable<(T item, T? next, int itemIdx, bool isLast)>

Type Parameters

T

Remarks

WithNext<T>(IEnumerable<T>, bool)

enumerate given items returning a tuple (item,next,isLast) with next=null for last element or next=first if repeatFirstAtEnd=true on latest el

public static IEnumerable<(T item, T? next, int itemIdx, bool isLast)> WithNext<T>(this IEnumerable<T> en, bool repeatFirstAtEnd = false) where T : class

Parameters

en IEnumerable<T>

input set

repeatFirstAtEnd bool

last enumerated result will (last,first,true)

Returns

IEnumerable<(T item, T next, int itemIdx, bool isLast)>

Type Parameters

T

Remarks

WithPrevNextPrimitive<T>(IEnumerable<T>, bool)

enumerate given items returning a tuple (prev,item,next,isLast) with prev=null for first element with next=null for last element or next=first if repeatFirstAtEnd=true on latest el

public static IEnumerable<(T? prev, T item, T? next, int itemIdx, bool isLast)> WithPrevNextPrimitive<T>(this IEnumerable<T> en, bool repeatFirstAtEnd = false) where T : struct

Parameters

en IEnumerable<T>

input set

repeatFirstAtEnd bool

last enumerated result will (last,first,true)

Returns

IEnumerable<(T? prev, T item, T? next, int itemIdx, bool isLast)>

Type Parameters

T

Remarks

WithPrevNext<T>(IEnumerable<T>, bool)

enumerate given items returning a tuple (prev,item,next,isLast) with prev=null for first element with next=null for last element or next=first if repeatFirstAtEnd=true on latest el

public static IEnumerable<(T? prev, T item, T? next, int itemIdx, bool isLast)> WithPrevNext<T>(this IEnumerable<T> en, bool repeatFirstAtEnd = false) where T : class

Parameters

en IEnumerable<T>

input set

repeatFirstAtEnd bool

last enumerated result will (last,first,true)

Returns

IEnumerable<(T prev, T item, T next, int itemIdx, bool isLast)>

Type Parameters

T

Remarks

WithPrevPrimitive<T>(IEnumerable<T>)

enumerate given items returning a tuple with nullable ( for first hit ) prev element

public static IEnumerable<(T? prev, T item, int itemIdx)> WithPrevPrimitive<T>(this IEnumerable<T> en) where T : struct

Parameters

en IEnumerable<T>

Returns

IEnumerable<(T? prev, T item, int itemIdx)>

Type Parameters

T

WithPrev<T>(IEnumerable<T>)

enumerate given items returning a tuple with null ( for first hit ) prev element

public static IEnumerable<(T? prev, T item, int itemIdx)> WithPrev<T>(this IEnumerable<T> en) where T : class

Parameters

en IEnumerable<T>

Returns

IEnumerable<(T prev, T item, int itemIdx)>

Type Parameters

T

XY(Vector3)

swizzle vector2 xy from vector3

public static Vector2 XY(this Vector3 v)

Parameters

v Vector3

Returns

Vector2

XYZ(Vector4)

swizzle vector3 xyz from vector4

public static Vector3 XYZ(this Vector4 v)

Parameters

v Vector4

Returns

Vector3