Delphi XE4中的CharInSet编译器警告
发布时间:2020-12-15 04:17:30 所属栏目:大数据 来源:网络整理
导读:我在Delphi 7代码中有以下声明. TMyCharSet = set of char; 当我将该代码迁移到Delphi XE4时,我在上面的行中收到了编译器警告. W1050 WideChar reduced to byte char in set expressions. Consider using 'CharInSet' function in 'SysUtils' unit. 我该如何
我在Delphi 7代码中有以下声明.
TMyCharSet = set of char; 当我将该代码迁移到Delphi XE4时,我在上面的行中收到了编译器警告. W1050 WideChar reduced to byte char in set expressions. Consider using 'CharInSet' function in 'SysUtils' unit. 我该如何重新声明TMyCharSet? 解决方法
您收到警告,因为XE4使用WideChar作为Char类型的变量(和WideString for String),因此Char现在占用2个字节而不是1个字节.现在可以在String / Char中保留unicode字符,但出于同样的原因,不可能再使用char集(在Delphi中它是固定大小,32字节位映射,因此最多可以保留256个项).
如果你只使用#0 ..#127范围内的字符(只有拉丁/常规符号),那么你可以只替换Char – > AnsiChar(但是当您从Char中分配它时,您将看到另一个警告,您将不得不使用显式类型转换来抑制它). 如果你需要国家/ unicode符号,那么Delphi中没有“随时可用”的结构,但你可以使用Tdictionary来达到这个目的: 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; 当然,您将像任何其他常规课程一样进行初始化. 或者你可以为unicode字符创建你自己的set类作为位映射,它需要8kb的内存来保存所有位,并且几乎与“set of”一样快. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |