I need to convert a string to a set, but in that string I want to be able to specify sub ranges.
eg.
program Testit;
type
TMyEnum = (a,b,c,d,e,f,g,h,i,j);
TMySet = set of TMyEnum;
function StringToMySet(const value : string) : TMySet;
begin
//what to do here
end;
begin
var myset := StringToMySet('a-d,f, g-i');
end.
It’s the conversion of the subranges which had me stumped for a bit - I have something working but it seems a bit ugly. Time to write some unit tests to break it!
function StringToCompilerVersions(const value : string) : TCompilerVersions;
var
entries : TArray<string>;
compilerVersion : TCompilerVersion;
range : TArray<string>;
lowVersion, highVersion : TCompilerVersion;
begin
result := [];
entries := SplitString(value, ',');
for var entry in entries do
begin
//check for range
if entry.Contains('-') then
begin
range := SplitString(entry,'-');
Assert(length(range) = 2);
lowVersion := StringToCompilerVersion(Trim(range[0]));
if lowVersion = TCompilerVersion.UnknownVersion then
raise EArgumentOutOfRangeException.Create('Invalid Compiler version : ' + range[0]);
highVersion := StringToCompilerVersion(Trim(range[1]));
if highVersion = TCompilerVersion.UnknownVersion then
raise EArgumentOutOfRangeException.Create('Invalid Compiler version : ' + range[1]);
for var version : TCompilerVersion := lowVersion to highVersion do
Include(result, version);
end
else
begin
compilerVersion := StringToCompilerVersion(Trim(entry));
Include(result, compilerVersion);
end;
end;
end;
Yeah good catch, I guess I need to check whether the low > high values in the range and swap them if so.
If the range includes values already in the string it’s no big deal as calling Include will do nothing in that case.
This is for a quick n dirty command line utility (generate projects for multiple compiler versions from a template project) so it doesn’t need to be bullet proof, just good enough
I had a go to try to take a functional approach … along the lines of applying functions into data structures.
My idea was : Given string → array of strings → tuples with ordered entries → predicate on inclusion of enums.
I wouldn’t say it’s great, but maybe it’s not completely awful.
I changed array to list, and leaked it, just to make it look and add nicer.
Just trying to convey an idea before my brain zonked out for the night.
IMO, the lhs is something we should be able to do … and the rhs should be in a library or the language.