This is my first post in the english forum. I'm from the german forum.

In fact, I'm from Germany.

I suggest these procedures to be new commands in the next PureBasic version:
- StringField_InsertString() — Insert a field before an other field
- StringField_SwapFields() — Swap two fields
- StringField_RemoveField() — Remove the given field
- Space2() — Similar to Space(), but returns any letters using 3 different, optimized methods
- bytecalc() — Converts a quad number in bytes to the biggest possible device (KB, MB...)
I don't mind if these code will be implemented, but optimized by other people. NicTheQuick from the german forum tried to optimize my procedures by using a lot of *Pointer. He had no success, my code beat him (I won the "contest"

A great advantage of my code is, that it don't makes any use of *pointers and *buffers. If I need to cut a string, for example, I use Left(), Right(), RTrim() and LTrim().
It's also very improtant to know that Right() is faster than Left(). Similar to RTrim() being faster than LTrim() / Trim(). In my code I payed attention to these facts - that's why my code should be optimized.
Note: Space() is still faster than Space2(), but only if you want a string filled with space-letters. However, Space2() is the fastest way to fill a string with "PB" (for example). But only, if the modulo specified length of the string and the length of the given string is zero (see code to understand this strange sentence

Note: I was told that RemoveField was counting/working wrong, but I cannot detect any bugs. It works fine on my PC.
Note: My StringField-Funtions start couting at zero (0). The normal StringField() starts counting at one (1). Don't swap this!
Code: Select all
#Franz="Franz jagt im komplett verwahrlosten Taxi quer durch Bayern."
#Fuchs="The quick brown fox jumps over the lazy dog."
#Jack="Jackdaws loves my big sphinx of quarz."
; Define your own test-string:
#Teststring=#franz
Procedure.s StringField_InsertString(String$, InsertString$, Index=1, Separator$=" ") ; by AND51
Protected n.l, position.q
If Index > CountString(String$, Separator$) And Not Right(String$, Len(Separator$)) = Separator$
String$+Separator$
EndIf
If FindString(String$, Separator$, 1)
For n=1 To Index
position=FindString(String$, Separator$, position+1)
Next
ProcedureReturn Left(String$, position)+InsertString$+Separator$+Right(String$, IntQ(Len(String$))-position)
Else
ProcedureReturn String$
EndIf
EndProcedure
Debug StringField_InsertString(#Teststring, "PureBasic", 1, ".")
Procedure.s StringField_SwapFields(String$, FieldA, FieldB, Separator$=" ") ; by AND51
If FieldA <> FieldB
String$=Separator$+String$+Separator$
If FieldA > FieldB
Swap FieldA, FieldB
EndIf
Protected FieldA$=StringField(String$, FieldA+1, Separator$), FieldB$=StringField(String$, FieldB+1, Separator$)
FieldA=FindString(String$, FieldA$, 1)
FieldB=FindString(String$, FieldB$, 1)
ProcedureReturn Mid(Left(String$, FieldA-1)+FieldB$+Mid(String$, FieldA+Len(FieldA$), FieldB-FIeldA-Len(FieldA$))+FieldA$+Right(String$, Len(String$)-(FieldB+Len(FieldB$))+1), Len(Separator$)+1, Len(String$)-Len(Separator$)*2)
Else
ProcedureReturn String$
EndIf
EndProcedure
Debug StringField_SwapFields(#Teststring, 7, 1)
Procedure.s StringField_RemoveField(String$, Index, Separator$=" ") ; by AND51
Protected n.l=CountString(String$, Separator$), position.q
If Index > n
Index=n
EndIf
For n=1 To Index
position=FindString(String$, Separator$, position+1)
Next
ProcedureReturn Left(String$, position)+Right(String$, Len(String$)-(position+Len(StringField(String$, Index+1, Separator$)))-Len(Separator$))
EndProcedure
Debug StringField_RemoveField(#Teststring, 3)
Procedure.s Space2(Length, Fill$) ; by AND51
Protected space.s, n.l
If Not Length % Len(Fill$) And RemoveString(Fill$, " ")
space=Space(Length*Len(Fill$))
ReplaceString(space, Space(Len(Fill$)), Fill$, 2)
ElseIf RTrim(Fill$)
For n=1 To Length
space+Fill$
Next
Else
space=Space(Length*Len(Fill$))
EndIf
ProcedureReturn space
EndProcedure
Debug Space2(34, "AB") ; 'In Place'-replace, if Length % Len(Fill$) = 0 (see MODULO-Operator).
Debug space2(3, "PureBasic")
This is the most optimized byte-calc function which has ever been written

It turns a number into the biggest device. For emaple, it turns 4096 into "4 KB". Optional comma-decimals possible.
Note: If this code doesn't seem to work, remove the PeekQ() command from the second line.
Code: Select all
Procedure.s byterechner(byte.q, NbDecimals.b=0)
Protected bytes.d=PeekQ(@byte), unit.b=Round(Log(bytes)/Log(1024), 0)
ProcedureReturn StrD(Bytes/Pow(1024, unit) , NbDecimals*(unit And 1))+" "+StringField("Byte,KB,MB,GB,TB,PB,EB", unit+1, ",")
EndProcedure
- I hope, you like these codes.
@ PB-Team: What do you think? Have these codes a chance?
RemoveField corrected, thx!
SwapField corrected. thx to whilbert