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
Mailing lists / fpc-pascal / 2015-12
Assigning string constant to a WideString and the Format function
13 messages · 6 people · –
- LLubos Pintes 5
- MMichael Van Canneyt 3
- MMark Morgan Lloyd 2
- Ssilvioprog 1
- TTomas Hajny 1
- UUnknown author 1
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Lubos Pintes wrote 9 lines
What happens if the format string is put into a temporary widestring?
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 5 lines
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
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
Lubos Pintes wroteI 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
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
The result isthe same, I tried it before I wrote my message. I even tried to temporarily…
Lubos Pintes wrote 12 lines
I suggest posting a *minimal* test program would be appropriate, and telling us exactly what OS etc. you've got.
I have a text in the WideString variable. I want to send this text to a SAPI5…
Lubos Pintes wroteHello, 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.
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 24 lines
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.
Here is a minimal program, it doesn't work as I expected either because there is a bug…
Lubos Pintes wrote 26 lines
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
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):
