字符被 GNAT 18.1 视为字符串

Character is considered by GNAT 18.1 as a string

我将我的项目从 GNAT 7.3.1 传递到 GNAT 18.1,但在 char 影响上出现编译错误。

我想影响字符串末尾度数 '°' 的特殊字符,但是编译器 returns 我遇到了这个错误:

R : String := "-NNN.N°";
begin
   ...
   R(R'Last) := '°';
   return R;
end

我得到了这个:

strings are delimited by double quote character
expected type "Standard.Character"
found a string type

如果我用字母或数字替换符号 °,就可以了。 如果我尝试转换为 R(R'Last) := Standard.Character('°');,编译器会说同样的话。

有人遇到过同样的问题吗?

您的代码(从此处复制并粘贴到我的编辑器中)是 UTF-8 编码的,因此您的学位符号是使用两个字节十六进制编码的 C2B0

GNAT 默认使用 Latin-1,so you have to tell it 使用 -gnatW8.

在 characters/strings/text IO 中使用 UTF-8

lnlb.adb为例,

with Ada.Text_IO;
procedure Lnlb is
   R : String := "-NNN.NX";
begin
   R(R'Last) := '°';
   Ada.Text_IO.Put_Line (R);
end Lnlb;

编译(在 macOS 上)

$ gnatmake lnlb.adb -gnatW8
gcc -c -gnatW8 lnlb.adb
gnatbind -x lnlb.ali
gnatlink lnlb.ali

和运行

$ ./lnlb
-NNN.N°

字符被定义为 Latin-1,因此当使用标准键盘上不直接可用的字符时,最好以字符文字以外的其他方式引用它们:

R (R'Last) := Ada.Characters.Latin_1.Degree_Sign;

R (R'Last) := Character'Val(176); -- Degree symbol