Share your advanced PureBasic knowledge/code with the community.
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Wed Sep 23, 2009 11:57 am
@Wolf
producing uncleanable garbage is no alternative...
oh... and have a nice day.
Hroudtwolf
Addict
Posts: 803 Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:
Post
by Hroudtwolf » Wed Sep 23, 2009 12:18 pm
Maybe you're right. I don't know if the stringmanager notes the memory-lenght of a string or it checks the null termination.
Therefore it could be unsafe.
Regards
Wolf
zikitrake
Addict
Posts: 875 Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain
Post
by zikitrake » Wed Sep 23, 2009 12:36 pm
My code:
Code: Select all
Procedure.s noext(tmp$)
pos.l = FindString(tmp$, ".", 1)
r$ = tmp$
Repeat
posseg = pos
If pos : r$ = Left(tmp$, pos-1) : EndIf
pos.l = FindString(tmp$, ".", pos + 1)
If pos< = posseg : Break : EndIf
ForEver
ProcedureReturn r$
EndProcedure
Debug noext("I.have.too.many.points.txt")
Debug noext("I have only one point.txt")
Debug noext("I haven't points")
helpy
Enthusiast
Posts: 552 Joined: Sat Jun 28, 2003 12:01 am
Post
by helpy » Wed Sep 23, 2009 1:53 pm
and how will your procedures handle ".htaccess"
?
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
zikitrake
Addict
Posts: 875 Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain
Post
by zikitrake » Wed Sep 23, 2009 1:57 pm
helpy wrote: and how will your procedures handle ".htaccess"
?
Code: Select all
Procedure.s noext(tmp$)
pos.l = FindString(tmp$, ".", 1)
r$ = tmp$
Repeat
posseg = pos
If pos > 1 : r$ = Left(tmp$, pos-1) : EndIf
pos.l = FindString(tmp$, ".", pos + 1)
If pos< = posseg : Break : EndIf
ForEver
ProcedureReturn r$
EndProcedure
Debug noext(".I.have.too.many.points.txt")
Debug noext("I have only one point.txt")
Debug noext(".htaccess")
Hroudtwolf
Addict
Posts: 803 Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:
Post
by Hroudtwolf » Wed Sep 23, 2009 2:03 pm
ROFL.
Jetzt hast du uns bei den Eiern. ^^ (german metaphor. I don't know what it is in english.)
Very good objection!
Joakim Christiansen
Addict
Posts: 2452 Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:
Post
by Joakim Christiansen » Wed Sep 23, 2009 3:42 pm
Kaeru Gaman wrote:
Code: Select all
filename$ = "Review 2009.09.23.doc"
Debug StringField(filename$,1,".")
pwned
Joakim Christiansen wrote: Can I get a cookie now?
NOPE!
This is a fun thread
I like logic, hence I dislike humans but love computers.
utopiomania
Addict
Posts: 1655 Joined: Tue May 10, 2005 10:00 pm
Location: Norway
Post
by utopiomania » Wed Sep 23, 2009 7:13 pm
@breez4me, does this leave the file on disc without the extension part?
If so, thanks for the tip
Kale
PureBasic Expert
Posts: 3000 Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:
Post
by Kale » Wed Sep 23, 2009 11:22 pm
All these procedures are too big. Text manipulation such as this is what regular expressions where invented for.
This is cross-platform:
Code: Select all
Procedure.s RemoveExtension(Filename.s)
CreateRegularExpression(0, "(?<!\A)\.[\w]+\Z")
ProcedureReturn ReplaceRegularExpression(0, Filename, "")
EndProcedure
Debug RemoveExtension(".htaccess")
Debug RemoveExtension("Filename.exe")
Debug RemoveExtension("Filename.html")
Debug RemoveExtension("Filename.config")
Debug RemoveExtension("File.Name.jpeg")
Debug RemoveExtension("I have only one point.txt")
Debug RemoveExtension(".I.have.too.many.points.txt")
--Kale
Hroudtwolf
Addict
Posts: 803 Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:
Post
by Hroudtwolf » Thu Sep 24, 2009 5:01 am
It is just a short memory leak @Kale ^^
My recommendation.
Code: Select all
Procedure.s RemoveExtension(sFilename.s)
Static idRegEx.i
; Object recycling.
If Not (idRegEx)
idRegEx = CreateRegularExpression(#PB_Any , "(?<!\A)\.[\w]+\Z")
If Not (idRegEx)
ProcedureReturn sFilename
EndIf
EndIf
ProcedureReturn ReplaceRegularExpression(idRegEx, sFilename, "")
EndProcedure
Debug RemoveExtension(".htaccess")
Debug RemoveExtension("Filename.exe")
Debug RemoveExtension("Filename.html")
Debug RemoveExtension("Filename.config")
Debug RemoveExtension("File.Name.jpeg")
Debug RemoveExtension("I have only one point.txt")
Debug RemoveExtension(".I.have.too.many.points.txt")
Regards
Wolf
SFSxOI
Addict
Posts: 2970 Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....
Post
by SFSxOI » Thu Sep 24, 2009 8:42 am
OK, I got a stupid question. Why can't you just rename the file to remove the extension?
Code: Select all
RenameFile("C:\test.txt", "C:\test")
works fine
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Kaeru Gaman
Addict
Posts: 4826 Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany
Post
by Kaeru Gaman » Thu Sep 24, 2009 10:32 am
Kale wrote: All these procedures are too big. Text manipulation such as this is what regular expressions where invented for.
oh... and have a nice day.
Kurzer
Enthusiast
Posts: 676 Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg
Post
by Kurzer » Thu Sep 24, 2009 11:01 am
Kale wrote: All these procedures are too big. Text manipulation such as this is what regular expressions where invented for.
Apropos "too big": How big will be your resulting Exefile if you use reg. expressions instead of a small procedure which do the same job?
PB 6.12 x64, OS: Win 11 24H2 x64, Desktopscaling: 150%, CPU: I7 12700 H, RAM: 32 GB, GPU: Intel(R) Iris(R) Xe Graphics | NVIDIA GeForce RTX 3070, User age in 2025: 57y
"Happiness is a pet." | "Never run a changing system!"
Demivec
Addict
Posts: 4270 Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA
Post
by Demivec » Thu Sep 24, 2009 2:36 pm
SFSxOI wrote: OK, I got a stupid question. Why can't you just rename the file to remove the extension?
Code: Select all
RenameFile("C:\test.txt", "C:\test")
works fine
@SFSxO1: You wouldn't know what to name it to until you had removed the extension.
I believe the thread title was meant to read "Remove the extension part of a
filename. "
Kale
PureBasic Expert
Posts: 3000 Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:
Post
by Kale » Thu Sep 24, 2009 5:48 pm
Kaeru Gaman wrote: Kale wrote: All these procedures are too big. Text manipulation such as this is what regular expressions where invented for.
xkcd.com image
Jealous?
Hroudtwolf wrote: It is just a short memory leak @Kale ^^
Explain. I don't understand why you've coded your procedure like that.
--Kale