Log in

View Full Version : StrFmt v1.00 - 14 Jan 2019


StainlessS
7th June 2017, 19:47
StrFmt() + StrRep().
Requires VS2008 CPP Runtimes.

dll's for avs v2.58 & avs/+ v2.60 x86 & x64.

Simple plugin to produce formatted string, and additional function to string replace with another string.


StrFmt(String format, dat1,...,datn)

dll's for avs v2.58 & v2.60 x86 & x64.

Returns a formatted string. The unnamed 'format' string and optional unnamed 'dat' args are used to construct the text string that is
returned, uses C/CPP printf() style formatting.
Format: compulsory string controlling format and describing the datn type args that are expected.
datn: Variable number of data args of any type (excluding clip).

printf Format spec here:- http://msdn.microsoft.com/en-us/library/56e442dc%28v=vs.71%29.aspx # EDIT: M$ link now Bad, see end of this post
NOTE, the only support for printing Bool variables is %s as string, ie prints "True" or "False".

Formatting supported, %[flags] [width] [.precision] type

flags, one of "-,+,0, ,#"
width, integer, "*" supported (width supplied via dat arg).
Precision, integer, "*" supported (precision supplied via dat arg).
type,
"c,C,d,i,o,u,x,X", Integer type, c,C=character, d,i=signed, o,u,x,X=unsigned (o=octal, x=Hex).
"e,E,f,g,G", Floating point type
"s,S", String type (also Bool).

Formatting Insertion point is marked with '%' character in the format string (as in Avisynth String function), if you wish to use
a percent character within the returned string, it should be inserted twice in the format string ie '%%' will produce a single '%'.
The data datn arg strings do not require a double '%' character.

A Backslash character '\' introduces escape sequences, to insert a backslash character itself, you must supply a double
backslash sequence ie '\\'.
Converts embedded escape character sequences (Case Significant):-
'\\' Converted to '\' Single Backslash
'\n' Converted to Chr(10) NewLine
'\r' Converted to Chr(13) Carriage Return
'\t' Converted to Chr(9) Horizontal TAB
'\v' Converted to Chr(11) Vertical TAB
'\f' Converted to Chr(12) FormFeed
'\b' Converted to Chr(8) BackSpace
'\a' Converted to Chr(7) Bell
'\x', where x is ANY OTHER CHARACTER not included above, will be copied verbatim, ie '\x'.

eg
StrFmt("Hello there %s and %s.\nGoodbye %d.","Fred","Ted",2019)
would return same as:- "Hello there Fred and Ted." + Chr(10) + "Goodbye 2019."

*****
*****
*****

StrRep(string source,string find,string replace,bool "sig"=True) # Based on Algorithm by Vampiredom, Gavino & IanB.

String args 'source', 'find' and 'replace' unnamed and compulsory.
Takes a source string, searches for all occurences of find string and replaces the found strings with the replace string.
Can use "" in replace string (only in replace) to delete the found substrings from the source string.
Newlines [Chr(10)] are treated no differently to other characters, and could be replaced/deleted.
'sig' arg,default true is Case Significant. Set false for case insignificant find string.




Test1

colorbars.killaudio
S = StrFmt("'%0*.*f'",10,3,9999.99)
Subtitle(S) # print '009999.990', ie total length of digits and '.' is 10, with 3 significant fractional digits.

# EDIT: the '0' after '%' uses '0' characters for leading zero padding instead of SPACE's
# EDIT: And the two '*' in format string, are where the 10 and 3 data args are inserted for Width and Precision specifiers. [ie script programmable Width & Precision rather than fixed format]



Test2

S="The Cat sat on the mat"

CASESIG=False

S2=StrRep(S,"cat","bat",CASESIG)

ColorBars.killAudio
Subtitle(S2) # print "The bat sat on the mat"


Equivalent functions in RT_Stats, RT_String and RT_StrReplace.

zip (~72KB) incl 3 dll's + source + VS2008 full project files for easy rebuild.

See MediaFire in sig below this post, or SendSpace.


EDIT: The given MicroSoft C Printf spec page is broken, so here some others.

https://docs.microsoft.com/en-us/cpp...?view=msvc-170
https://www.freecodecamp.org/news/fo...ecifiers-in-c/
https://cplusplus.com/reference/cstdio/printf/

amayra
9th June 2017, 20:00
this looks very interesting thanks

StainlessS
9th June 2017, 22:47
Glad you like it :)

StrFmt() provides a very powerful string generator, much better than the basic Avisynth String() function, (which is a little bit
clumsy to use when more than a very simple string with numeric).

And here the thread that seems to introduce the StrReplace() function, way back when:- https://forum.doom9.org/showthread.php?p=1540487&highlight=StrReplace#post1540487
The dll version will save on memory usage as it creates no temporary intermediary strings (only pertinent where lots and lots of strings replaced).

EDIT: Note also that the width and precision specifiers also work with strings, eg
StrFmt("%5s","Fred") would print " Fred", and [string width=5 example ie '%5s']
StrFmt("%.1s",True) would print "T". [string precision=1 example, ie '%.1s']
Width is the minimum length of generated string [EDIT: for strings includes SPACE padding], precision is the maximum length of generated string [EDIT: for strings, non padded length] .
And can left justify, eg StrFmt("%-5s","Fred") would print "Fred ".
And can also use Width specifier programmatically, eg,

n=5
StrFmt("%-*s",n,"Fred") # would produce "Fred ". ["Fred" + 1 SPACE], = length of 5 characters, left justified.


or


n=6
B = True
StrFmt("%-*s",n,B) # would produce "True " [2 SPACES] when B=True, and "False " [1 SPACE] when B=False.

StainlessS
29th July 2017, 22:24
Bug fix update, v0.01.
Mods in BLUE (was printing character from buffer after buffer released).

int tmpc=*r;
delete [] pbuf;
env->ThrowError("%sType='%c', Expecting Int data (%d)",myName,tmpc,ix+1); }


EDIT: Turns out that above bug was not a bug at all, first time in my life that I've fixed a non existing bug (No idea what I was thinking) :(

StainlessS
15th January 2019, 00:52
StrFmt() v1.00, update, see 1st post.

Moved to VS2008, Added Version Resource, added avs/+ v2.60 x86 & x64 dll's. (as well as 2.58 dll)