“类型不匹配";“字符集"错误在Inno Setup Unicode版本的Pascal脚本中 [英] "Type mismatch" error on "set of char" in Pascal Script of the Inno Setup Unicode version

查看:87
本文介绍了“类型不匹配";“字符集"错误在Inno Setup Unicode版本的Pascal脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Inno Setup使用了有效的Pascal脚本,但是最近我们遇到了Unicode字符的问题.在将Inno Setup更改为Unicode版本后,我们在现有脚本中遇到了错误.而且此错误过于笼统-类型不匹配" 对于某些行.由于我们的主脚本由包含的许多其他脚本组成,所以我不确定该行是否正确,它指向下面的此函数,并与 Char 一致.查看Inno Setup文档后,我发现Unicode版本在某些方面更加严格,并且在ANSI字符串处理方面也有一些更改(可能还有 Char ).

I have an working Pascal scripts for Inno Setup, but recently we ran into an issue with Unicode characters. And after changing the Inno Setup to Unicode version we got errors in existing scripts. And this error is too generic - "Type mismatch" for certain line. Since our main script is composed of lots of other scripts that are included I'm not sure if that line is the correct, and it points to this function below, and line with Set of Char. After looking the Inno Setup documentation I found that Unicode version is bit more strict about some things, and also there was some changes around ANSI string handling (and probably Char as well).

function IsValidName(Name: String):String;
var
  Valid_Ch: Set of Char;
  Location: Integer;
begin
    Valid_Ch := ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_', '-'];
    Result := '';
    for Location := 1 to Length(Name) do
    begin
        if Name[Location] in Valid_Ch then
        begin
            Result := Result + Name[Location];
        end
        else
        begin
            Result := Result + '_';
        end;
    end;    
end;

这一切对我来说看起来不错,但这是我与Pascal的第一次接触.如果有经验的人可以帮助我,我将不胜感激.谢谢!

This all looks fine to me, but this is my first touch with Pascal. I would be thankful if someone more experienced could help me about this. Thanks!

推荐答案

在Pascal(脚本)中, set 只能包含256(2 ^ 8)个值.在Unicode Inno Setup中, Char 为2字节,因此它可以包含超过256个不同值的方式.因此,您不能再将其与 set 一起使用.

In Pascal (Script) the set can contain 256 (2^8) values only. In Unicode Inno Setup the Char is 2-byte, hence it can contain a way more than 256 different values. So you cannot use it with set anymore.

您有以下选择:

  • 在您的情况下,由于该集合实际上是常量,因此可以在 if 表达式中使用常量集合.有趣的是,即使在Unicode版本中,它也可以工作(可能内置了一些允许该异常的hack).

  • In your case, as the set is in fact constant, you can use a constant set in the if expression. Interestingly, that works even in Unicode version (possibly there's some hack built in that allows this exception).

function IsValidName(Name: string): string;
var
  Location: Integer;
begin
  Result := Name;
  for Location := 1 to Length(Name) do
  begin
    if not (Name[Location] in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_', '-']) then
    begin
      Result[Location] := '_';
    end;
  end;    
end;

您是否需要将集合设为可变的(不是您的情况):

Had you needed the set to be variable (not your case):

  • 使用 Byte 并在 Char Byte 之间进行强制转换,并用 Ord()>显式替换所有字符.255
  • 或使用 string 而不是 set .有关示例,请参见下文.

  • Use Byte and cast between Char and Byte and explicitly replace all characters with Ord() > 255
  • Or use a string instead of the set. For an example, see below.

function IsValidName(Name: string): string;
var
  Valid_Ch: string;
  Location: Integer;
begin
  Valid_Ch := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-';
  Result := Name;
  for Location := 1 to Length(Result) do
  begin
    if Pos(Result[Location], Valid_Ch) = 0 then
    begin
      Result[Location] := '_';
    end;
  end;    
end;

还请注意,将字符串逐个字符追加可能效率很低.替换为原位字符(如以上两个示例所示).

Also note that appending the string char-by-char can be pretty inefficient. Replace the characters in-place instead (as shown in both examples above).

这篇关于“类型不匹配";“字符集"错误在Inno Setup Unicode版本的Pascal脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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