Native FormatString (sprintf)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Native FormatString (sprintf)

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Native FormatString (sprintf)

Post by mk-soft »

Pushup :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Native FormatString (sprintf)

Post by User_Russian »

Code: Select all

s.s{1024}
OpenLibrary(0, "MSVCRT.dll")
CallCFunction(0, "swprintf", @s, @"Param %i", 10)
Debug s
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Native FormatString (sprintf)

Post 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$
Last edited by skywalk on Sat Jul 22, 2017 3:50 pm, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Native FormatString (sprintf)

Post by IdeasVacuum »

sprintf() would be an excellent addition to PB.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Native FormatString (sprintf)

Post 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.
fryquez
Enthusiast
Enthusiast
Posts: 369
Joined: Mon Dec 21, 2015 8:12 pm

Re: Native FormatString (sprintf)

Post 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?
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Native FormatString (sprintf)

Post 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
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Native FormatString (sprintf)

Post by mk-soft »

The import of sprintf don´t work with dynamics's parameters and different datatypes (String, float, double, ...)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Native FormatString (sprintf)

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5406
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Native FormatString (sprintf)

Post by mk-soft »

Yes, but how to create va_list with purebasic :?:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 3999
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Native FormatString (sprintf)

Post 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()?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Native FormatString (sprintf)

Post 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 :(
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply