Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-12

Assigning string constant to a WideString and the Format function

13 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Lubos Pintes

Hello,
I have a text in the WideString variable. I want to send this text to a SAPI5 synthesizer. I am doing this as follows:
WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]); Pitch is integer, Text is WideString.
The result is incorrect, only a half of the Text contents is copied to the result.
Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] for i386 Thanks

Reply by email

Mark Morgan Lloyd
to Lubos Pintes
Lubos Pintes wrote 9 lines
Hello, I have a text in the WideString variable. I want to send this text to a SAPI5 synthesizer. I am doing this as follows: WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]); Pitch is integer, Text is WideString. The result is incorrect, only a half of the Text contents is copied to the result. Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] for i386 Thanks

What happens if the format string is put into a temporary widestring?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Lubos Pintes
to Mark Morgan Lloyd

Hello Mark,
The result isthe same, I tried it before I wrote my message. I even tried to temporarily write ordinal codes of characters, it seems that a string is not automatically converted to a WideString when assigned, there wereno zeros, like H#0e#0l#0, etc.

Dňa 23. 12. 2015 o 11:07 Mark Morgan Lloyd napísal(a):

Show quoted text 13 lines
Lubos Pintes wrote: > Hello, > I have a text in the WideString variable. I want to send this text to > a SAPI5 synthesizer. I am doing this as follows: > WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]); > Pitch is integer, Text is WideString. > The result is incorrect, only a half of the Text contents is copied to > the result. > Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] for i386 > Thanks What happens if the format string is put into a temporary widestring?

Reply by email

Michael Van Canneyt
to Lubos Pintes
Lubos Pintes wrote 5 lines
Hello Mark, The result isthe same, I tried it before I wrote my message. I even tried to temporarily write ordinal codes of characters, it seems that a string is not automatically converted to a WideString when assigned, there wereno zeros, like H#0e#0l#0, etc.

Did you try
WideStr := Format(WideString('<pitch absmiddle="%d">%s</pitch>'), [Pitch, Text]); to make sure the correct function is used ?

Michael.

Show quoted text 20 lines
Dňa 23. 12. 2015 o 11:07 Mark Morgan Lloyd napísal(a): > Lubos Pintes wrote: >> Hello, >> I have a text in the WideString variable. I want to send this text to >> a SAPI5 synthesizer. I am doing this as follows: >> WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]); >> Pitch is integer, Text is WideString. >> The result is incorrect, only a half of the Text contents is copied to >> the result. >> Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] for i386 >> Thanks > > What happens if the format string is put into a temporary widestring? > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Lubos Pintes
to Michael Van Canneyt

I tried it now, and it doesn't work.
I also tried all string variables changed to WideString. Dňa 23. 12. 2015 o 11:59 Michael Van Canneyt napísal(a):

Show quoted text 43 lines
On Wed, 23 Dec 2015, Lubos Pintes wrote: > Hello Mark, > The result isthe same, I tried it before I wrote my message. > I even tried to temporarily write ordinal codes of characters, it > seems that a string is not automatically converted to a WideString > when assigned, there wereno zeros, like H#0e#0l#0, etc. Did you try WideStr := Format(WideString('<pitch absmiddle="%d">%s</pitch>'), [Pitch, Text]); to make sure the correct function is used ? Michael. > > Dňa 23. 12. 2015 o 11:07 Mark Morgan Lloyd napísal(a): >> Lubos Pintes wrote: >>> Hello, >>> I have a text in the WideString variable. I want to send this text to >>> a SAPI5 synthesizer. I am doing this as follows: >>> WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]); >>> Pitch is integer, Text is WideString. >>> The result is incorrect, only a half of the Text contents is copied to >>> the result. >>> Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] for i386 >>> Thanks >> >> What happens if the format string is put into a temporary widestring? >> > > > _______________________________________________ > fpc-pascal maillist - > > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Lubos Pintes
Lubos Pintes wrote
I tried it now, and it doesn't work. I also tried all string variables changed to WideString.

In that case, please file a bugreport with an example console program for FPC.

Michael.
y

Reply by email

Tomas Hajny
to Lubos Pintes: The result isthe same, I tried it before I wrote my message. I even tried to temporarily…

On Wed, December 23, 2015 11:41, Lubos Pintes wrote:

Hello Lubos,

The result isthe same, I tried it before I wrote my message. I even tried to temporarily write ordinal codes of characters, it seems that a string is not automatically converted to a WideString when assigned, there wereno zeros, like H#0e#0l#0, etc.

If you look at ordinal codes of characters in a WideString using Ord(WideStringVar[x]), you would never see any zeros, because the ordinal value represents the character (actually codepoint), not individual bytes.

Tomas

Reply by email

Mark Morgan Lloyd
to Lubos Pintes: The result isthe same, I tried it before I wrote my message. I even tried to temporarily…
Lubos Pintes wrote 12 lines
Hello Mark,The result isthe same, I tried it before I wrote my message.I even tried to temporarily write ordinal codes of characters, it seems that a string is not automatically converted to a WideString when assigned, there wereno zeros, like H#0e#0l#0, etc. Dňa 23. 12. 2015 o 11:07 Mark Morgan Lloyd napísal(a):> Lubos Pintes wrote:>> Hello,>> I have a text in the WideString variable. I want to send this text to>> a SAPI5 synthesizer. I am doing this as follows:>> WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]);>> Pitch is integer, Text is WideString.>> The result is incorrect, only a half of the Text contents is copied to>> the result.>> Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] for i386>> Thanks>> What happens if the format string is put into a temporary widestring?>

I suggest posting a *minimal* test program would be appropriate, and telling us exactly what OS etc. you've got.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

silvioprog
to Lubos Pintes: I have a text in the WideString variable. I want to send this text to a SAPI5…
Lubos Pintes wrote
Hello, I have a text in the WideString variable. I want to send this text to a SAPI5 synthesizer. I am doing this as follows: WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]);

[...]

Assuming that you tested it on Windows, you can't copy a AnsiString (FPC Format() is ansi on Windows) directly by pointing it to a WideString (your wide variable), and probably the compiler warns it with an 'Implicit string type conversion from "AnsiString" to "WideString"'. So, try it with a string type cast, something like this:

WideStr := WideString(Format('<pitch absmiddle="%d">%s</pitch>', [Pitch,
Text]));

Or use the SysUtils.WideFormat() function, that is wide:

WideStr := WideFormat('<pitch absmiddle="%d">%s</pitch>', [Pitch, Text]);

Just for testing, I did it here and it worked fine (FPC 3.0):

uses
  SysUtils;
var
  WideStr: WideString;
begin
  WideStr := WideString(Format('<pitch absmiddle="%d">%s</pitch>', [123,
'abc']));
  WriteLn('String: ', WideStr);
  WriteLn('Length: ', Length(BytesOf(WideStr)));
  WideStr := '';
  WideStr := WideFormat('<pitch absmiddle="%d">%s</pitch>', [123, 'abc']);
  WriteLn('String: ', WideStr);
  WriteLn('Length: ', Length(BytesOf(WideStr)));
end.

Result:

String: <pitch absmiddle="123">abc</pitch>
Length: 34
String: <pitch absmiddle="123">abc</pitch>
Length: 34

However it is interesting if you send a minimal test program showing the entire problem and informing your SO, as the guys said for you.

Silvio Clécio

Reply by email

Lubos Pintes
to Mark Morgan Lloyd: I suggest posting a *minimal* test program would be appropriate, and telling us exactly…

Hello,
Here is a minimal program, it doesn't work as I expected either because there is a bug somewhere in the RTL, or, more probably, I misunderstood something.
Basically, I am reading an UTF-8 encoded text from stdin, and collecting it in a WideChar array. I am assigning UTF-8 string to a WideString, and then collecting chars from it into the mentioned array. Then I am using a SetString procedure to recreate the WideString from collected chars.
Here is a simple program which demonstrates this:

program a;
{$mode delphi}
uses SysUtils;
var
   U: UTF8String;
   S, W: WideString;
begin
   U := 'Hello';
   W := U;
   SetString(S, @W[1], Length(W));
   W := Format(WideString('%s'), [S]);
   Writeln(W);
end.

Dňa 23. 12. 2015 o 17:29 Mark Morgan Lloyd napísal(a):

Show quoted text 18 lines
Lubos Pintes wrote: > Hello Mark,The result isthe same, I tried it before I wrote my > message.I even tried to temporarily write ordinal codes of characters, > it seems that a string is not automatically converted to a WideString > when assigned, there wereno zeros, like H#0e#0l#0, etc. > Dňa 23. 12. 2015 o 11:07 Mark Morgan Lloyd napísal(a):> Lubos Pintes > wrote:>> Hello,>> I have a text in the WideString variable. I want to > send this text to>> a SAPI5 synthesizer. I am doing this as follows:>> > WideStr := Format('<pitch absmiddle="%d">%s</pitch>', [Pitch, > Text]);>> Pitch is integer, Text is WideString.>> The result is > incorrect, only a half of the Text contents is copied to>> the > result.>> Tested with Free Pascal Compiler version 3.1.1 [2015/11/14] > for i386>> Thanks>> What happens if the format string is put into a > temporary widestring?> I suggest posting a *minimal* test program would be appropriate, and telling us exactly what OS etc. you've got.

Reply by email

Michael Van Canneyt
to Lubos Pintes
Lubos Pintes wrote 24 lines
Hello, Here is a minimal program, it doesn't work as I expected either because there is a bug somewhere in the RTL, or, more probably, I misunderstood something. Basically, I am reading an UTF-8 encoded text from stdin, and collecting it in a WideChar array. I am assigning UTF-8 string to a WideString, and then collecting chars from it into the mentioned array. Then I am using a SetString procedure to recreate the WideString from collected chars. Here is a simple program which demonstrates this: program a; {$mode delphi} uses SysUtils; var U: UTF8String; S, W: WideString; begin U := 'Hello'; W := U; SetString(S, @W[1], Length(W));

This is the problem, not the call to Format.

If you do a writeln('S : ',S); you will see that S is already wrong. Change it to

    SetString(S, @W[1], Length(W)*SizeOf(WideChar));

and all should go well.

Michael.

Reply by email

Unknown author
to Lubos Pintes: Here is a minimal program, it doesn't work as I expected either because there is a bug…
Lubos Pintes wrote 26 lines
Hello, Here is a minimal program, it doesn't work as I expected either because there is a bug somewhere in the RTL, or, more probably, I misunderstood something. Basically, I am reading an UTF-8 encoded text from stdin, and collecting it in a WideChar array. I am assigning UTF-8 string to a WideString, and then collecting chars from it into the mentioned array. Then I am using a SetString procedure to recreate the WideString from collected chars. Here is a simple program which demonstrates this: program a; {$mode delphi} uses SysUtils; var U: UTF8String; S, W: WideString; begin U := 'Hello'; W := U; SetString(S, @W[1], Length(W)); W := Format(WideString('%s'), [S]); Writeln(W); end.

Free Pascal Compiler version 3.1.1 [2015/06/13] for x86_64 Copyright (c) 1993-2015 by Florian Klaempfl and others (1002) Target OS: Linux for x86-64
(3104) Compiling /home/myuser/tmp/project1.lpr
/home/myuser/tmp/project1.lpr(12,8) Warning: (4104) Implicit string type conversion from "UTF8String" to "WideString"
/home/myuser/tmp/project1.lpr(14,8) Warning: (4104) Implicit string type conversion from "AnsiString" to "WideString"

~/tmp/project1
Hel

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list* unless private contact is specifically requested and granted.

Reply by email

Lubos Pintes
to Michael Van Canneyt: This is the problem, not the call to Format. If you do a writeln('S : ',S); you will see…

Interesting. I thought that SetString is properly overloaded. Maybe it is, maybe not. But anyway I fixed the program by using WideStr := PWideChar(@WideCharArray[Index]);
Thus ignoring badly-working SetString.
Anyway thank for all responses!

Dňa 24. 12. 2015 o 14:23 Michael Van Canneyt napísal(a):

Show quoted text 42 lines
On Thu, 24 Dec 2015, Lubos Pintes wrote: > Hello, > Here is a minimal program, it doesn't work as I expected either > because there is a bug somewhere in the RTL, or, more probably, I > misunderstood something. > Basically, I am reading an UTF-8 encoded text from stdin, and > collecting it in a WideChar array. I am assigning UTF-8 string to a > WideString, and then collecting chars from it into the mentioned array. > Then I am using a SetString procedure to recreate the WideString from > collected chars. > Here is a simple program which demonstrates this: > > program a; > {$mode delphi} > > uses SysUtils; > > var > U: UTF8String; > S, W: WideString; > > begin > U := 'Hello'; > W := U; > SetString(S, @W[1], Length(W)); This is the problem, not the call to Format. If you do a writeln('S : ',S); you will see that S is already wrong. Change it to SetString(S, @W[1], Length(W)*SizeOf(WideChar)); and all should go well. Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email