How can I build WideString with diacritics in a non-Unicode Delphi version?

I am trying to build (test) WideStringof:

á ( U + 00E1 Small Latin letter A with a sharp )

but using its decomposed form:

LATIN SMALL LETTER A ( U + 0061 ) COMBINING SHARP ACCENT ( U + 0301 )

So, I have a piece of code:

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

Except that it does not work:

enter image description here

It may be a bug in MessageBox, but I'm going to go ahead and say that, most likely, a mistake in my code.

Some other options I've tried:

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 thought:
test := #$0301#$0061;

Bonus Chatter

+4
source share
3 answers

:

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;
    enter image description here
  • 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
    enter image description here

, , :

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

; , . , , .

+10

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;

:

  • Delphi 5 delphi 7 , WideChars WideString #$xxxx.
  • #, , , Unicode.

  • , :

    test := WideChar(a)+WideChar(b);  // won't compile in D5/D7.
    
+2

Have you tried # $ 0301 # $ 0061 (i.e. diacritical first)?

OK

So, # $ .... only processes 8-bit ASCII constants in this version.

You can use a workaround using the memory level:

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;

This will always work, since you are not playing with symbols / wide clocks etc.

And it will work just as expected in the Unicode Delphi version.

0
source

All Articles