CharInSet doesn't work with non English letter

2019-03-17 23:44发布

I have updated an application from Delphi 2007 to Delphi 2010, everything went fine, except one statement that compiled fine but not working which is:

If Edit1.Text[1] in ['S','س'] then 
  ShowMessage('Found')
else
  ShowMessage('Not Found')

However, I knew that in will not, so I changed to CharInSet

If CharinSet(Edit1.Text[1],['S','س']) then
  ShowMessage('Found')
else
  ShowMessage('Not Found')

but it never worked when the string is س, but always work with S, even I cast the edt1.Text1 with AnsiChar it always not work Arabic letters.

Am doing anything wrong, or it's not the way CharInSet works?, or that's a bug in CharinSet?

UPDATE:

My Great friend Issam Ali has suggested another solution which's worked fine as it :

  If CharinSet(AnsiString(edt1.Text)[1],['S','س']) then

5条回答
爷的心禁止访问
2楼-- · 2019-03-18 00:12

Use TCharHelper.IsInArray as follows:

if Edit1.Text[1].IsInArray(['S','س']) then 
  ShowMessage('Found')
else
  ShowMessage('Not Found');
查看更多
Deceive 欺骗
3楼-- · 2019-03-18 00:21

In addition.

sets are limited to ordinal values of 256 elements. So AnsiChar fits and (Unicode)Char does not fit. You can use CharInSet to port pre unicode versions of Delphi to the unicode versions. Because of the set limitation, sets are not extremely usefull anymore with Chars.

The reason behind this, is that sets are implemented as bitmasks. You are free to implement your own version of a set. For example:

type
  TSet<T> = class 
  public
    procedure Add(const AElem: T);
    function InSet(const AElem: T): Boolean;
  end;
查看更多
forever°为你锁心
4楼-- · 2019-03-18 00:22

This happens because set of char structured type (limited to 256 elements maximum) doesn't support Unicode at all. That is, any characters Ord(ch) > High(AnsiChar) being truncated in the set constructor and warning W1061 about narrowing WideChar to AnsiChar is being emitted. Look at the following testcase:

  { naturally, fails, emits CharInSet() suggestion }
  Result := 'س' in ['S','س'];

  { fails because second argument is set of AnsiChar }
  Result := CharInSet(
    'س',
    ['S','س']
  );

  { workaround for WideChar in AnsiCharSet, fails }
  Result := WideStrUtils.InOpSet(
    'س',
    ['S','س']
  );

  { a syntactical workaround, which finally works }
  Result := WideStrUtils.InOpArray(
    'س',
    ['S','س']
  );

  if Result then
    ShowMessage('PASS')
  else
    ShowMessage('FAIL');
查看更多
smile是对你的礼貌
5楼-- · 2019-03-18 00:23

Have you set the encoding of your source file to UTF-8 (right click to open the context menu)? (The default is ANSI iirc, which would not work.)

查看更多
放荡不羁爱自由
6楼-- · 2019-03-18 00:34

CharInSet is useless for the characters above 255. In your case you should use

  case C of
    'S','س' : ShowMessage('Found');
  end;
查看更多
登录 后发表回答