Hallo,
eine Erweiterung der C-Syntax des Format-Befehles wäre ja schön.
Da fehlen z.B. %c für Zeichen und %s für Strings.
Allerdings bin ich wohl schon zu lange nicht mehr dabei um das fehlerfrei hin zu bekommen.
Deshalb stelle ich das einfach mal ein. Vielleicht findet ihr ja den Fehler.
Das Programm ist noch gespickt mit Print-Anweisungen um dem Fehler auf die Spur zu kommen. Es wird einfach nicht der Ersatzwert an der gewünschten Stelle eingefügt. Eine vorherige Version mit Match$ hat auch Probleme gemacht.
Code
/*
Name: FormatEx$
Autor: Michael Wodrich
Version: XProfan X2 (neueste)
--- zur beliebig freien Verwendung ---
Fügt der C-Syntax noch die fehlenden Formen hinzu:
%c - ein Zeichen
%[-][min][.max]s - ein String
Außerdem werden zur Übergabe 2 dyn. Arrays benutzt.
Damit sind dann unendlich viele Parameter möglich.
Für jedes folgende Formatzeichen wird dann ein Eintrag im jeweiligen Array vorgenommen.
Ein Float-Array für Zahlen und ein String-Array für Zeichen und Strings (Basis 0).
*/
// %%|%[-]?[\d]*(\.{1}\d*)??[csduefgnmpx]
//
// Options: case insensitive; ^ and $ match at line breaks
//
// Match either the regular expression below (attempting the next alternative only if this one fails) «%%»
// Match the characters “%%” literally «%%»
// Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «%[-]?[\d]*
(\.{1}\d*)??[csduefgnmpx]»
// Match the character “%” literally «%»
// Match the character “-” «[-]?»
// Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
// Match a single digit 0..9 «[\d]*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Match the regular expression below and capture its match into backreference number 1 «(\.{1}\d*)??»
// Between zero and one times, as few times as possible, expanding as needed (lazy) «??»
// Match the character “.” literally «\.{1}»
// Exactly 1 times «{1}»
// Match a single digit 0..9 «\d*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Match a single character present in the list “csduefgnmpx” «[csduefgnmpx]»
// (ich arbeite gerne mit RegExBuddy -- Vollversion)
Proc FormatEx$
// erg$ = FormatEx$("%d%s",f[],s[])
// Der übergebene Formatstring wird manipuliert und als Ergebnis zurück gegeben.
Parameters String Fmt, Float aZahl[], String aString[]
Var int iZahl = 0 : Var int iString = 0 // Index für die dyn. Arrays
Declare int oldRegEx
Declare int p // Positionierhilfe
Declare int i // Min-Wert %5s
Declare int j // Max-Wert %.7s
Declare int li // linksbündig
Declare int ml // tmp
Declare string tmp // --
Declare string m // zum ermitteln von Min/Max
Declare string s // umgewandeltes Format
Case Len(Fmt) = 0 : Return "" // Schnellausstieg, wenn Fmt leer
oldRegEx = Set("RegEx",1)
Var Int Posi = 1
p = InStr("%%|%[~-]?[~d]*(~.{1}~d*)??[csduefgnmpx]", Fmt, Posi)
While p <> 0
' Fmt = Del$(Fmt,posi,len)
' Fmt = Ins$(Fmt,Einfügen,posi)
cls
print "len fmt =",Len(Fmt)
print "---"
print " ";fmt
print "---"
print space$(%matchpos);MkStr$("#",%MatchLen)
print "Posi ",Posi
print "p ",p
print "matchpos",%MatchPos
print "matchlen",%MatchLen
print "match: [";$match;"]"
// %% - Wenn das Prozentzeichen selbst eingefügt werden soll
Set("RegEx",0)
If (%MatchLen = 2) and ($Match = "%%") // %%
print "wert: %% [%]"
Fmt = Del$(Fmt,%MatchPos,1) // entferne eines der beiden Zeichen
Posi = %MatchPos + 1 // dahinter geht es dann weiter
// %c - Einfügen eines einzelnen Zeichens (ohne Längeninfos)
ElseIf InStr(Right$($Match,1),"cC") // %c
s = if(iString < SizeOf(aString[]), \
Left$(aString[iString],1), "") // ein String-Zeichen
Inc iString
print "wert: %c [";s;"]"
Fmt = Del$(Fmt,%MatchPos,%MatchLen) // Formatzeichen raus
If Len(s) > 0 // bei Leerstring keine Positionsänderung
' Fmt = Ins$(Fmt,Einfügen,posi)
Fmt = Ins$( Fmt, s, %MatchPos )
Posi = %MatchPos + Len(S)
EndIf
// %s - Einfügen eines Strings
ElseIf InStr(Right$($Match,1),"sS") // %[-][i][.j]s
s = if(iString < SizeOf(aString[]), \
aString[iString], "") // String
Inc iString
If (%MatchLen > 2) and (Len(s) > 0) // Formatierungen nötig?
m = Mid$( $Match, 2, %MatchLen - 2 )
mL = Len(m)
li = 0
If Left$($Match,1) = "-" // %[-]s linksbündig
m = Right$( m, mL - 1 )
Inc li
Dec mL
EndIf
If mL
If Len( m, "." ) > 1 // %[i][.j]s
i = Val(SubStr$( m, 1, "." ))
j = Val(SubStr$( m, 2, "." ))
If li
s = s + if( Len(s) < i, Space$(i - Len(s)), "" )
s = s + if( Len(s) < j, Space$(j - Len(s)), "" )
s = Left$(s,j)
Else
s = if( Len(s) < i, Space$(i - Len(s)), "" ) + s
s = if( Len(s) < j, Space$(j - Len(s)), "" ) + s
s = Right$(s,j)
EndIf
Else // %[i]s
i = Val(m)
If li
s = s + if( Len(s) < i, Space$(i - Len(s)), "" )
Else
s = if( Len(s) < i, Space$(i - Len(s)), "" ) + s
EndIf
EndIf
EndIf
EndIf
Fmt = Del$(Fmt,%MatchPos,%MatchLen) // Formatzeichen raus
If Len(s) > 0 // bei Leerstring keine Positionsänderung
' Fmt = Ins$(Fmt,Einfügen,posi)
Fmt = Ins$( Fmt, s, %MatchPos )
Posi = %MatchPos + Len(S)
EndIf
print "wert: %s [";translate$(s," ",".");"]"
Else // alle anderen Formatierungen
print "wert: %.. ["; if(iZahl < SizeOf(aZahl[]), aZahl[iZahl], 0) ;"]"
s = Format$( $Match, if(iZahl < SizeOf(aZahl[]), aZahl[iZahl], 0) )
Inc iZahl
Fmt = Del$(Fmt,%MatchPos,%MatchLen) // Formatzeichen raus
If Len(s) > 0 // bei Leerstring keine Positionsänderung
' Fmt = Ins$(Fmt,Einfügen,posi)
Fmt = Ins$( Fmt, s, %MatchPos )
Posi = %MatchPos + Len(S)
EndIf
print "Ergebnis: [";translate$(s," ","*");"]"
EndIf
waitkey
Set("RegEx",1)
p = InStr("%%|%[~-]?[~d]*(~.{1}~d*)??[csduefgnmpx]", Fmt, Posi)
EndWhile
Set("RegEx",oldRegEx)
Return Fmt
EndProc
// #############################################################
Proc Test
Declare float ff[], string ss[], yyy
var string fmt = "Wert%-7.3d %7.3DDollar %9u %9U%12.8e %12.8E %8.17f %8.17F %g %G%nNoten %N %m %M %p
%P %2x%2X %c-class %C %-12s:4 %12S"
ff[ 0] = 123.051
ff[ 1] = -2
ff[ 2] = 98765432
ff[ 3] = 4
ff[ 4] = 17.9876
ff[ 5] = 3.14159
ff[ 6] = 6.7
ff[ 7] = 99.01
ff[ 8] = 23.18
ff[ 9] = 1.1
ff[10] = 4
ff[11] = 5
ff[12] = 21.98
ff[13] = 4.99
ff[14] = $FF1234
ff[15] = $1234
ff[16] = $23
ff[17] = $74
ss[ 0] = "E"
ss[ 1] = "y"
ss[ 2] = "Profan"
ss[ 3] = "TEsT"
Print "aus:"
Print Fmt
yyy = FormatEx$(Fmt,ff[],ss[])
Print "wird:"
Print yyy
EndProc
// #############################################################
// Hauptprogramm
windowtitle "Test " + $ProfVer
cls
Test
print "---anzeigen---"
waitkey
End
// #############################################################
Alles anzeigen
Ach und.....
Hello again - wer mich noch kennt
(Kann man das evtl. auch als ZIP hochladen? Es wird nicht wie das Original angezeigt.)
Gruß
Michael Wodrich