From efe51f129f6f08f03ca9fcf718ac7ecd9292ee12 Mon Sep 17 00:00:00 2001 From: Sun Serega Date: Sun, 31 Mar 2024 14:30:21 +0200 Subject: [PATCH] cleanup TODO's --- Fixers.pas | 2 +- Patterns.pas | 3 -- Patterns/MergedStrings.pas | 5 ++- Patterns/test.pas | 4 +-- SubExecuters.pas | 2 -- TypeMagic.pas | 68 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 TypeMagic.pas diff --git a/Fixers.pas b/Fixers.pas index b96fae5d..4dfc97ea 100644 --- a/Fixers.pas +++ b/Fixers.pas @@ -53,7 +53,7 @@ interface var start := true; var bl_start := true; - //TODO #2683 + //TODO #2715 var make_res: function: (array of string,array of string) := ()-> begin var lc := res.Count; diff --git a/Patterns.pas b/Patterns.pas index f884fee3..7c9ce696 100644 --- a/Patterns.pas +++ b/Patterns.pas @@ -1,9 +1,6 @@ unit Patterns; {$zerobasedstrings} -//TODO #2739 -{$savepcu false} - interface uses System; diff --git a/Patterns/MergedStrings.pas b/Patterns/MergedStrings.pas index acab1d7c..afe13026 100644 --- a/Patterns/MergedStrings.pas +++ b/Patterns/MergedStrings.pas @@ -529,7 +529,7 @@ implementation private parts: array of MergedStringPart; private len_caps: array of MergedStringLength; - private constructor(parts: array of MergedStringPart); + private constructor(params parts: array of MergedStringPart); begin self.parts := parts; SetLength(len_caps, parts.Count); @@ -675,8 +675,7 @@ implementation function MergedStringPointer.IsOut := data.part_i = s.parts.Length; static function MergedString.Literal(s: string) := -//TODO #3064: adding params breaks case where array is passed to "new MergedString" -new MergedString(new MergedStringPart[](new MergedStringPartSolid(s))); +new MergedString(new MergedStringPartSolid(s)); static function MergedString.Parse(s: StringSection; escape_sym: char) := new MergedString(OptimizeParts(MakeParts(s, escape_sym)).ToArray); diff --git a/Patterns/test.pas b/Patterns/test.pas index 104e569a..a0aca4c9 100644 --- a/Patterns/test.pas +++ b/Patterns/test.pas @@ -37,9 +37,9 @@ var p: MergedString; begin - var ps := EnumerateFiles('G:/0Prog/AutoMaximize/Classes/1 What') + var ps := EnumerateFiles('G:\0Prog\AutoMaximize\Classes\1 What') .Select(System.IO.Path.GetFileName) - .Where(fname->fname.StartsWith('HwndWrapper')) +// .Where(fname->fname.StartsWith('HwndWrapper')) .Select(MergedString.Literal) .ToHashSet; while ps.Count.Println>1 do diff --git a/SubExecuters.pas b/SubExecuters.pas index 6dc573cf..4bb92537 100644 --- a/SubExecuters.pas +++ b/SubExecuters.pas @@ -7,8 +7,6 @@ uses PathUtils; uses Timers; -uses AQueue; //TODO #2543 - {$region Helpers} type diff --git a/TypeMagic.pas b/TypeMagic.pas new file mode 100644 index 00000000..fc72491a --- /dev/null +++ b/TypeMagic.pas @@ -0,0 +1,68 @@ +unit TypeMagic; + +interface + +type + + {$region Convert} + + ConvertTo = sealed class + + private constructor := raise new System.InvalidOperationException; + + public static function FromChecked(o: T2): T; + public static function FromUnchecked(o: T2): T; + public static function From(o: T2; checked: boolean := true) := + if checked then FromChecked(o) else FromUnchecked(o); + + end; + + {$endregion Convert} + +implementation + +uses System.Linq.Expressions; + +{$region Convert} + +type ConvertCache = sealed class + + private constructor := raise new System.InvalidOperationException; + + public static checked, unchecked: T2->T1; + private static function MakeFunc(conv: (Expression,System.Type)->Expression): T2->T1; + begin + try + var p := Expression.Parameter(typeof(T2)); + var c := conv(p, typeof(T1)); + var l := Expression.Lambda&>(c, p); + Result := l.Compile(); + except + on e: Exception do + begin + Result := o-> + begin + Result := default(T1); + raise new System.InvalidCastException($'Failed to make [{TypeToTypeName(typeof(T2))}]=>[{TypeToTypeName(typeof(T1))}] conversion', e); + end; + exit; + end; + end; + + end; + static constructor; + begin + checked := MakeFunc(Expression.ConvertChecked); + unchecked := MakeFunc(Expression.Convert); + end; + +end; + +static function ConvertTo.FromChecked(o: T2) := + ConvertCache&.checked(o); +static function ConvertTo.FromUnchecked(o: T2) := + ConvertCache&.unchecked(o); + +{$endregion Convert} + +end. \ No newline at end of file