Page 1 of 1

Native FormatString (sprintf)

Posted: Sun Jul 10, 2016 12:28 pm
by mk-soft
Times had written this code a long time ago.
Add For better performans with dynamic number of parameters maybe "c sprintf" as native command.

Link: http://www.purebasic.fr/english/viewtop ... 12&t=32026

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 10:59 am
by mk-soft
Pushup :wink:

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 2:17 pm
by User_Russian

Code: Select all

s.s{1024}
OpenLibrary(0, "MSVCRT.dll")
CallCFunction(0, "swprintf", @s, @"Param %i", 10)
Debug s

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 3:20 pm
by skywalk
What's wrong with floats? :?:

Code: Select all

ImportC "MSVCRT.LIB" 
  swPrintf.l(out$, fmt$, Xd.d) As "swprintf"
EndImport
Define.s r$ = StrD(#PI,4) + "ZZZ"
x.d = #PI
Debug r$
Debug x
ShowMemoryViewer(@r$, 32)
Debug swPrintf(r$, "%e", x)
ShowMemoryViewer(@r$, 32)
Debug r$

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 3:35 pm
by IdeasVacuum
sprintf() would be an excellent addition to PB.

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 3:40 pm
by Little John
User_Russian wrote:

Code: Select all

s.s{1024}
OpenLibrary(0, "MSVCRT.dll")
CallCFunction(0, "swprintf", @s, @"Param %i", 10)
Debug s
Works on Windows, but not on Linux (and probably not on Mac, too).
I would appreciate to have a native cross-platform sprintf() function built-in into PureBasic.

For me, this is especially useful when I write programs that support multiple languages.
I then put all strings for one language in a file, and the strings contain placeholders such as %s etc., which will be replaced by their respective current values during runtime of the program. This way, all language dependent strings are easy to handle, and are completely separated from the rest of the program.

So +1 from me for this request.

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 4:29 pm
by fryquez
Linux has the glibc.

Code: Select all

ImportC ""
  snprintf(a, b, c.p-utf8 ,d)
EndImport

iBufferSize = 256
sOut.s = Space(iBufferSize)
snprintf(@sOut, iBufferSize, "Param %i", 10)
sOut = PeekS(@sOut, -1, #PB_UTF8)
Debug sOut
But would about Import with variable parameter?

Re: Native FormatString (sprintf)

Posted: Sat Jul 22, 2017 5:31 pm
by skywalk
As I mentioned, the sXXprintf functions are halfway useless without float support.

Code: Select all

ImportC "MSVCRT.lib"
  snprintf(a, b, c.p-utf8, d.f) As "_snprintf"
EndImport
iBufferSize = 256
sOut.s = Space(iBufferSize)
snprintf(@sOut, iBufferSize, "Param %f", 10.2)  ;<-- "Param 0.000000"
sOut = PeekS(@sOut, -1, #PB_UTF8)
Debug sOut

Re: Native FormatString (sprintf)

Posted: Sun Jul 23, 2017 11:04 am
by mk-soft
The import of sprintf don´t work with dynamics's parameters and different datatypes (String, float, double, ...)

Re: Native FormatString (sprintf)

Posted: Sun Jul 23, 2017 11:44 am
by wilbert
mk-soft wrote:The import of sprintf don´t work with dynamics's parameters and different datatypes (String, float, double, ...)
You could try vsprintf instead but of course a native solution is better.

Re: Native FormatString (sprintf)

Posted: Sun Jul 23, 2017 1:51 pm
by mk-soft
Yes, but how to create va_list with purebasic :?:

Re: Native FormatString (sprintf)

Posted: Sun Jul 23, 2017 2:32 pm
by skywalk
ImportC sprintf() used to work in earlier PB versions.
But, I lost track after I wrote my own Engineering/Scientific formatter.
Maybe Fred could compile MSVCRT.lib to work with floats instead of going to the trouble of vsprintf()?

Re: Native FormatString (sprintf)

Posted: Sun Jul 23, 2017 3:18 pm
by wilbert
mk-soft wrote:Yes, but how to create va_list with purebasic :?:
I looked into it but didn't realize that would be so complicated :(