Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As mentioned by Uwe Raabe, the problem with Unicode char is, they're pretty large. If Delphi allowed you to create an "set of Char" it would be 8 Kb in size! An "set of AnsiChar" is only 32 bytes in size, pretty manageable.</p> <p>I'd like to offer some alternatives. First is a sort of drop-in replacement for the CharInSet function, one that uses an array of CHAR to do the tests. It's only merit is that it can be called immediately from almost anywhere, but it's benefits stop there. I'd avoid this if I can:</p> <pre><code>function UnicodeCharInSet(UniChr:Char; CharArray:array of Char):Boolean; var i:Integer; begin for i:=0 to High(CharArray) do if CharArray[i] = UniChr then begin Result := True; Exit; end; Result := False; end; </code></pre> <p>The trouble with this function is that it doesn't handle the <code>x in ['a'..'z']</code> syntax and it's slow! The alternatives are faster, but aren't as close to a drop-in replacement as one might want. The first set of alternatives to be investigated are the <a href="http://msdn.microsoft.com/en-us/library/ff468910(v=VS.85).aspx" rel="nofollow noreferrer">string functions from Microsoft.</a> Amongst them there's IsCharAlpha and IsCharAlphanumeric, they might fix lots of issues. The problem with those, all "alpha" chars are the same: You might end up with valid Alpha chars in non-enlgish non-czech languages. Alternatively you can use the <a href="http://docwiki.embarcadero.com/VCL/en/Character.TCharacter" rel="nofollow noreferrer">TCharacter class from Embarcadero</a> - the implementation is all in the Character.pas unit, and it looks effective, I have no idea how effective Microsoft's implementation is.</p> <p>An other alternative is to write your own functions, using an "case" statement to get things to work. Here's an example:</p> <pre><code>function UnicodeCharIs(UniChr:Char):Boolean; var i:Integer; begin case UniChr of 'ă': Result := True; 'ş': Result := False; 'Ă': Result := True; 'Ş': Result := False; else Result := False; end; end; </code></pre> <p>I inspected the assembler generated for this function. While Delphi has to implement a series of "if" conditions for this, it does it very effectively, way better then implementing the series of IF statements from code. But it could use a lot of improvement.</p> <p>For tests that are used ALOT you might want to look for some bit-mask based implementation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload