View Single Post
Old 7th June 2017, 19:47   #1  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
StrFmt v1.00 - 14 Jan 2019

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.

Code:
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
Code:
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
Code:
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/
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 17th July 2022 at 15:56. Reason: Update
StainlessS is offline   Reply With Quote