Delphi XE4中的CharInSet编译器警告 [英] CharInSet Compiler Warning in Delphi XE4

查看:177
本文介绍了Delphi XE4中的CharInSet编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi 7代码中有以下语句。

  TMyCharSet =字符集; 

当我将该代码迁移到Delphi XE4时,我在上一行得到了编译器警告。

  W1050在集合表达式中,WideChar简化为字节char。考虑在 SysUtils单元中使用 CharInSet功能。 

我应该如何重新声明TMyCharSet?

解决方案

您会收到警告,因为XE4将WideChar用作Char类型的变量(对于字符串类型则使用WideString,对于String),因此Char现在占用2个字节而不是1个字节。现在可以将Unicode字符保留在String / Char中,但是出于同样的原因,也无法再使用char集(在Delphi中,它是固定大小的32位字节映射,最多可以保留256个项目)。 / p>

如果仅使用#0 ..#127范围内的字符(仅使用拉丁/常规符号),则只需替换Char-> AnsiChar(但是当您分配时它来自Char,您将看到另一个警告,您将必须使用显式类型转换来禁止显示它。)



如果您需要国家/统一代码符号,则不会出现可以在Delphi中使用准备使用结构,但您可以为此使用Tdictionary:

  type 
TEmptyRecord =记录结束;

TSet< T> = class(TDictionary< T,TEmptyRecord>)
public
过程Add(Value:T);重新介绍排队;
过程AddOrSetValue(Value:T);重新介绍排队;
函数Contains(Value:T):Boolean;重新介绍排队;
结尾;

过程TSet< T> .Add(值:T);
var Dummy:TEmptyRecord;
开始
继承了AddOrSetValue(Value,Dummy);
结尾;

过程TSet< T> .AddOrSetValue(Value:T);
var Dummy:TEmptyRecord;
开始
继承了AddOrSetValue(Value,Dummy);
结尾;

函数TSet< T> ;.包含(值:T):布尔值;
开始
结果:=继承ContainsKey(Value);
结尾;

当然,您将像其他任何普通课程一样初始化。
但是它仍然会非常高效(当然,不如 set of那么快,只是因为 set始终受最大大小限制为256个项目,但高度优化)。



或者,您也可以为Unicode字符创建自己的集合类(作为位图),保留所有位将需要8kb的内存,并且几乎与 set of一样快。


I have following statement in my Delphi 7 code.

TMyCharSet = set of char;

When I migrated that code to Delphi XE4, I am getting following compiler warning at above line.

W1050 WideChar reduced to byte char in set expressions.  Consider using 'CharInSet' function in 'SysUtils' unit.

How should I redeclare TMyCharSet?

解决方案

You get the warning because XE4 uses WideChar for variable of Char type (and WideString for String), so Char takes 2 bytes instead of 1 byte now. Now it is possible to keep unicode characters in String/Char, but for same reason it is impossible to use set of char anymore (in Delphi it is fixed size, 32-bytes bits map and can keep up to 256 items so).

If you use only chars from range #0..#127 (only latin/regular symbols), then you can just replace Char -> AnsiChar (but when you will assign it from Char you will see another warning, you will have to use explicit type conversion to suppress it).

If you need national/unicode symbols, then there is no "ready to use" structure in Delphi, but you can use Tdictionary for this purpose:

type
  TEmptyRecord = record end;

  TSet<T> = class(TDictionary<T,TEmptyRecord>)
  public
    procedure Add(Value: T); reintroduce; inline;
    procedure AddOrSetValue(Value: T); reintroduce; inline;
    function Contains(Value: T):Boolean; reintroduce; inline;
  end;

procedure TSet<T>.Add(Value: T);
var Dummy: TEmptyRecord;
begin
  inherited AddOrSetValue(Value, Dummy);
end;

procedure TSet<T>.AddOrSetValue(Value: T);
var Dummy: TEmptyRecord;
begin
  inherited AddOrSetValue(Value, Dummy);
end;

function TSet<T>.Contains(Value: T): Boolean;
begin
  result := inherited ContainsKey(Value);
end;

Of course you will have initialize at as any other regular class. But it will be still quite efficient (not so fast as "set of" of course, just because "set" is always limited by 256 items max size but highly optimized).

Alternatively you can create your own set class for unicode chars as map of bits, it will take 8kb of memory to keep all the bits and will be almost as fast as "set of".

这篇关于Delphi XE4中的CharInSet编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆