如何在非unicode Delphi版本中构建具有diacratic的WideString?

Ian*_*oyd 4 delphi unicode delphi-5 unicode-string widestring

我正在尝试构建一个(测试)WideString:

á(U + 00E1小写字母拉丁文A,急性)

但使用它的分解形式:

拉丁文小写字母A(U + 0061)结合急性加速(U + 0301)

所以我有代码片段:

var
    test: WideString;
begin
   test := #$0061#$0301;
   MessageBoxW(0, PWideChar(test), 'Character with diacratic', MB_ICONINFORMATION or MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)

除了它似乎不起作用:

在此输入图像描述

可能是一个错误MessageBox,但我会继续说,它更可能是我的代码中的错误.

我尝试过的其他一些变化:

test := WideString(#$0061#$0301);


const
    SmallLetterLatinAWithAcuteDecomposed: WideString = #$0061#$0301;
test := SmallLetterLatinAWithAcuteDecomposed


test := #$0061+#$0301;  (Doesn't compile; incompatible types)


test := WideString(#$0061)+WideString(#$0301);  (Doesn't compile; crashes compiler)


test := 'a'+WideString(#$0301);  (Doesn't compile; crashes compiler)


//Arnauld's thought:
test := #$0301#$0061;
Run Code Online (Sandbox Code Playgroud)

奖金喋喋不休

Ian*_*oyd 10

最佳答案:

const
    n: WideString = '';  //n=Nothing

s := n+#$0061+#$0301;
Run Code Online (Sandbox Code Playgroud)

这修复了我在下面的所有情况,否则失败.


唯一有效的变体是将其声明为常量:

AccentAcute: WideString = #$0301;
AccentAcute: WideString = WideChar($0301);
AccentAcute: WideString = WideChar(#$0301);
AccentAcute: WideString = WideString(#$0301);
Run Code Online (Sandbox Code Playgroud)

样品用法:

s := 'Pasta'+AccentAcute;
Run Code Online (Sandbox Code Playgroud)

基于常量的语法不起作用

  • AccentAcute: WideString = $0301;
    不兼容的类型
  • AccentAcute: WideString = #0301;
    在此输入图像描述
  • AccentAcute: WideString = WideString($0301);
    无效的类型转换
  • AccentAcute: WideString = WideString(#$0301);
    无效的类型转换
  • AccentAcute: WideChar = WideChar(#0301); Pastai
  • AccentAcute: WideChar = WideChar($0301); Pasta´

其他失败的语法

  • 'Pasta'+WideChar($0301)
    Pasta´
  • 'Pasta'+#$0301
    Pasta´
  • WideString('Pasta')+#$0301
    在此输入图像描述

我发现想到的所有基于常量的语法的摘要:

AccentAcute: WideString =            #$0301;   //works
AccentAcute: WideString =   WideChar(#$0301);  //works
AccentAcute: WideString = WideString(#$0301);  //works
AccentAcute: WideString =             $0301;   //incompatble types
AccentAcute: WideString =    WideChar($0301);  //works
AccentAcute: WideString =  WideString($0301);  //invalid typecast

AccentAcute: WideChar =            #$0301;     //fails, gives Pasta´
AccentAcute: WideChar =   WideChar(#$0301);    //fails, gives Pasta´
AccentAcute: WideChar = WideString(#$0301);    //incompatible types
AccentAcute: WideChar =             $0301;     //incompatible types
AccentAcute: WideChar =    WideChar($0301);    //fails, gives Pasta´
AccentAcute: WideChar =  WideString($0301);    //invalid typecast
Run Code Online (Sandbox Code Playgroud)

重新排列WideChar可以工作,只要您只附加到变量

//Works
t := '0123401234012340123';
t := t+WideChar(#$D840);
t := t+WideChar(#$DC00);

//fails
t := '0123401234012340123'+WideChar(#$D840);
t := t+WideChar(#$DC00);

//fails
t := '0123401234012340123'+WideChar(#$D840)+WideChar(#$DC00);

//works
t := '0123401234012340123';
t := t+WideChar(#$D840)+WideChar(#$DC00);

//works
t := '';
t := t+WideChar(#$D840)+WideChar(#$DC00);

//fails; gives junk
t := ''+WideChar(#$D840)+WideChar(#$DC00);

//crashes compiler
t := WideString('')+WideChar(#$D840)+WideChar(#$DC00);

//doesn't compile
t := WideChar(#$D840)+WideChar(#$DC00);
Run Code Online (Sandbox Code Playgroud)

绝对打击编译器废话; 没有经过测试的病例完全测试.是的,我认识大卫,我们应该升级.