4

我正在尝试构建一个(测试)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;

除了它似乎不起作用:

在此处输入图像描述

可能是 中的错误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;

奖金喋喋不休

4

3 回答 3

11

最佳答案:

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

s := n+#$0061+#$0301;

这解决了我下面的所有情况,否则会失败。


唯一可行的变体是将其声明为常量:

AccentAcute: WideString = #$0301;
AccentAcute: WideString = WideChar($0301);
AccentAcute: WideString = WideChar(#$0301);
AccentAcute: WideString = WideString(#$0301);

样品用法:

s := 'Pasta'+AccentAcute;

不起作用的基于常量的语法

  • 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

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);

绝对打击编译器的废话;未经过全面测试的案例。是的,我认识大卫,我们应该升级。

于 2011-08-11T19:50:34.957 回答
2

这适用于 Delphi 5/7:

var
  test: WideString;
begin

   test := WideChar($0061);
   test := test + WideChar($0301);

   MessageBoxW(0, PWideChar(test), 'Character with diacratic', MB_ICONINFORMATION or MB_OK);
end;

简而言之:

  • #$xxxx在 delphi 5 和 delphi 7 中,使用表单文字将 WideChars 连接到 WideString 似乎不起作用。
  • #似乎不像您对 unicode 文字所期望的那样工作。

  • 您不能只在一个表达式中添加两个或更多宽字符,如下所示:

    test := WideChar(a)+WideChar(b);  // won't compile in D5/D7.
    
于 2011-08-11T18:35:31.163 回答
0

您是否尝试过#$0301#$0061(即变音符号优先)?

好的。

所以 #$.... 在这个版本中只处理 ASCII 8 位常量。

您可以使用内存级别的解决方法:

type
    TWordArray  = array[1..MaxInt div SizeOf(word)-2] of word;
    // start at [1], just as WideStrings
    // or: TWordArray  = array[0..MaxInt div SizeOf(word)-1] of word;
    PWordArray = ^TWordArray;

var
  test: WideString;
begin
  test := '12'; // or SetLength(test,2);
  PWordArray(test)[1] := $61; 
  PWordArray(test)[2] := $301;
  MessageBoxW(0, pointer(test), 'Character with diacratic', MB_ICONINFORMATION or MB_OK);
end;

这将始终有效,因为您不使用 chars/widechars 等。

它也可以与 Unicode 版本的 Delphi 一起正常工作。

于 2011-08-11T17:33:11.710 回答