Forum > Suggestions
CASE statement with strings.
Leledumbo:
Back in the day I proposed to implement this using prefix tree (or trie) instead (fastest, guaranteed O(n) where n is the query length), but it turns out generating code that calls AnsiCompareStr sequentially is much easier and the resulting code is smaller, too.
CCRDude:
--- Quote from: Leledumbo on April 28, 2015, 07:27:49 am ---
--- Quote from: Blaazen on April 28, 2015, 01:45:19 am ---Hello, I guess you use
--- Code: ---{$mode delphi}
--- End code ---
It works here with FPC 3.1.1 and
--- Code: ---{$mode objfpc}
--- End code ---
--- End quote ---
It works in FPC/ObjFPC mode only. Delphi, TP, MacPas and ISO mode doesn't support it for compatibility.
--- End quote ---
Bringing up an old thread here... would it be possible to enable this in other modes using a $modeswitch maybe?
tr_escape:
I know old topic.
Also maybe switch case able to use like as C#'s switch case like as:
https://ochzhen.com/blog/switch-case-when-in-csharp
and :
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/patterns3#relational-patterns
but ofcourse if stay in pascal rules :)
cdbc:
Hi
A long time ago, I needed to use strings in a case statement...
I came up with this and i've been using it a lot, since then ;)
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{ bcStrCase usage.... case bcStrCase(S,['+date','+hello','+data','+quit']) of 0..1: ShowMessage('Hello @ '+datetimetostr(now)); // '+date' -> '+hello' 2: SendDataSomewhere; //+data 3: Close; // +quit end; }{ bcStrCase is zero-based, returns -1 on not found }function bcStrCase(const S: string;Elements: array of string): ptrint;var Idx: ptrint;begin Result:= -1; { hence the result ~ ptrint } { perform a linear search } for Idx:= low(Elements) to high(Elements) do if S = Elements[Idx] then begin Result:= Idx; break; end;end; { bcStrCase } It deals with range too, see "0..1: showme...."
I dunno how fast it is, I just find it conveinient to use.
Regards Benny
PascalDragon:
--- Quote from: cdbc on October 17, 2022, 02:53:05 pm ---A long time ago, I needed to use strings in a case statement...
I came up with this and i've been using it a lot, since then ;)
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{ bcStrCase usage.... case bcStrCase(S,['+date','+hello','+data','+quit']) of 0..1: ShowMessage('Hello @ '+datetimetostr(now)); // '+date' -> '+hello' 2: SendDataSomewhere; //+data 3: Close; // +quit end; }{ bcStrCase is zero-based, returns -1 on not found }function bcStrCase(const S: string;Elements: array of string): ptrint;var Idx: ptrint;begin Result:= -1; { hence the result ~ ptrint } { perform a linear search } for Idx:= low(Elements) to high(Elements) do if S = Elements[Idx] then begin Result:= Idx; break; end;end; { bcStrCase } It deals with range too, see "0..1: showme...."
I dunno how fast it is, I just find it conveinient to use.
--- End quote ---
Are you aware that this already exists in the StrUtils unit in the form of IndexStr and IndexText?
Navigation
[0] Message Index
[#] Next page
[*] Previous page