Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Posts: 2146 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Mon Apr 18, 2022 10:55 am
Instead of using a regular expression (\d, \w) that adds 150 KB to the size of the executable, you can use compact code.
Code: Select all
EnableExplicit
Procedure IsLatin(*text)
Protected flag = #True, *c.Character = *text
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
While *c\c
If Not ((*c\c >= '0' And *c\c <= '9') Or (*c\c >= 'a' And *c\c <= 'z') Or (*c\c >= 'A' And *c\c <= 'Z') Or *c\c = '_')
flag = #False
Break
EndIf
*c + SizeOf(Character)
Wend
ProcedureReturn flag
EndProcedure
Debug IsLatin(@"123")
Debug IsLatin(@"12 3")
Debug IsLatin(@"qwerty")
Debug IsLatin(@"")
Debug IsLatin(@" ")
Debug "==="
Procedure IsDigital(*text)
Protected flag = #True, *c.Character = *text
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
While *c\c
If *c\c < '0' Or *c\c > '9'
flag = #False
Break
EndIf
*c + SizeOf(Character)
Wend
ProcedureReturn flag
EndProcedure
Debug IsDigital(@"123")
Debug IsDigital(@"12 3")
Debug IsDigital(@"")
Debug "==="
Procedure IsHex(*text)
Protected flag = #True, *c.Character = *text
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
While *c\c
If Not ((*c\c >= '0' And *c\c <= '9') Or (*c\c >= 'a' And *c\c <= 'f') Or (*c\c >= 'A' And *c\c <= 'F'))
flag = #False
Break
EndIf
*c + SizeOf(Character)
Wend
ProcedureReturn flag
EndProcedure
Debug IsHex(@"123")
Debug IsHex(@"FF34FF")
Debug IsHex(@"")
Debug IsHex(@" ")
Debug "==="
Procedure IsFloat(*text)
Protected flag = #True, *c.Character = *text, sep = 0, increment = 1
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
While *c\c
If *c\c >= '0' And *c\c <= '9'
; Debug "-> 0-9"
sep = increment
ElseIf sep And *c\c = '.'
; Debug "-> ."
If sep <= 0
; Debug "-> Out2"
flag = #False
Break
EndIf
sep = 0
increment = -1
Else
; Debug "-> Out"
flag = #False
Break
EndIf
*c + SizeOf(Character)
Wend
If sep <> -1
; Debug "-> Out3"
flag = #False
EndIf
ProcedureReturn flag
EndProcedure
Debug IsFloat(@"1.2")
Debug IsFloat(@"1..2")
Debug IsFloat(@"1.2.3")
Debug IsFloat(@"1")
Debug IsFloat(@"1.")
Debug IsFloat(@".1")
Debug IsFloat(@"qwerty")
Debug IsFloat(@".")
Debug IsFloat(@"")
Last edited by
AZJIO on Tue Apr 19, 2022 5:31 pm, edited 4 times in total.
idle
Always Here
Posts: 5850 Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand
Post
by idle » Tue Apr 19, 2022 1:45 am
It's better to pass by reference so you don't copy the string
Code: Select all
Procedure IsLatin(*text)
Protected flag = 1, *c.Character = *text
While *c\c
If Not ((*c\c >= '0' And *c\c <= '9') Or (*c\c >= 'a' And *c\c <= 'z') Or (*c\c >= 'A' And *c\c <= 'Z') Or *c\c = '_')
flag = 0
Break
EndIf
*c + SizeOf(Character)
Wend
ProcedureReturn flag
EndProcedure
Debug IsLatin(@"123")
Debug IsLatin(@"12 3")
Debug IsLatin(@"dfg")
Debug IsLatin(@" ")
Procedure IsDigital(*text)
Protected flag = 1, *c.Character = @text
While *c\c
If *c\c < '0' Or *c\c > '9'
flag = 0
Break
EndIf
*c + SizeOf(Character)
Wend
ProcedureReturn flag
EndProcedure
Debug IsDigital(@"123")
Debug IsDigital(@"12 3")
juergenkulow
Enthusiast
Posts: 581 Joined: Wed Sep 25, 2019 10:18 am
Post
by juergenkulow » Tue Apr 19, 2022 7:38 am
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
Rinzwind
Enthusiast
Posts: 679 Joined: Wed Mar 11, 2009 4:06 pm
Location: NL
Post
by Rinzwind » Tue Apr 19, 2022 9:13 am
Well, can't handle everything. But yeah, accents are also not handled: é ë ê
AZJIO
Addict
Posts: 2146 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Tue Apr 19, 2022 2:29 pm
Added to make empty string be 0
Code: Select all
If *c\c = 0
ProcedureReturn 0
EndIf
Repeat-Until
Code: Select all
EnableExplicit
Procedure IsLatin(*text)
Protected flag = #True, *c.Character = *text
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
Repeat
If Not ((*c\c >= '0' And *c\c <= '9') Or (*c\c >= 'a' And *c\c <= 'z') Or (*c\c >= 'A' And *c\c <= 'Z') Or *c\c = '_')
flag = #False
Break
EndIf
*c + SizeOf(Character)
Until Not *c\c
ProcedureReturn flag
EndProcedure
Debug IsLatin(@"123")
Debug IsLatin(@"12 3")
Debug IsLatin(@"qwerty")
Debug IsLatin(@"")
Debug IsLatin(@" ")
Debug "==="
Procedure IsDigital(*text)
Protected flag = #True, *c.Character = *text
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
Repeat
If *c\c < '0' Or *c\c > '9'
flag = #False
Break
EndIf
*c + SizeOf(Character)
Until Not *c\c
ProcedureReturn flag
EndProcedure
Debug IsDigital(@"123")
Debug IsDigital(@"12 3")
Debug IsDigital(@"")
Debug "==="
Procedure IsHex(*text)
Protected flag = #True, *c.Character = *text
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
Repeat
If Not ((*c\c >= '0' And *c\c <= '9') Or (*c\c >= 'a' And *c\c <= 'f') Or (*c\c >= 'A' And *c\c <= 'F'))
flag = #False
Break
EndIf
*c + SizeOf(Character)
Until Not *c\c
ProcedureReturn flag
EndProcedure
Debug IsHex(@"123")
Debug IsHex(@"FF34FF")
Debug IsHex(@"")
Debug IsHex(@" ")
Debug "==="
Procedure IsFloat(*text)
Protected flag = #True, *c.Character = *text, sep = 0, increment = 1
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
Repeat
If *c\c >= '0' And *c\c <= '9'
; Debug "-> 0-9"
sep = increment
ElseIf sep And *c\c = '.'
; Debug "-> ."
If sep <= 0
; Debug "-> Out2"
flag = #False
Break
EndIf
sep = 0
increment = -1
Else
; Debug "-> Out"
flag = #False
Break
EndIf
*c + SizeOf(Character)
Until Not *c\c
If sep <> -1
; Debug "-> Out3"
flag = #False
EndIf
ProcedureReturn flag
EndProcedure
Debug IsFloat(@"1.2")
Debug IsFloat(@"1..2")
Debug IsFloat(@"1.2.3")
Debug IsFloat(@"1")
Debug IsFloat(@"1.")
Debug IsFloat(@".1")
Debug IsFloat(@"qwerty")
Debug IsFloat(@".")
Debug IsFloat(@"")
Last edited by
AZJIO on Tue Apr 19, 2022 5:25 pm, edited 3 times in total.
mk-soft
Always Here
Posts: 6219 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » Tue Apr 19, 2022 3:22 pm
Its better
Code: Select all
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
because a string can be Nothing (NIL)
AZJIO
Addict
Posts: 2146 Joined: Sun May 14, 2017 1:48 am
Post
by AZJIO » Tue Apr 19, 2022 3:41 pm
Code: Select all
EnableExplicit
Procedure IsFloat(*text)
Protected flag = #True, *c.Character = *text, sep = 0, increment = 1
If *c = 0 Or *c\c = 0
ProcedureReturn 0
EndIf
Repeat
If *c\c >= '0' And *c\c <= '9'
; Debug "-> 0-9"
sep = increment
ElseIf sep And *c\c = '.'
; Debug "-> ."
If sep <= 0
; Debug "-> Out2"
flag = #False
Break
EndIf
sep = 0
increment = -1
Else
; Debug "-> Out"
flag = #False
Break
EndIf
*c + SizeOf(Character)
Until Not *c\c
If sep <> -1
; Debug "-> Out3"
flag = #False
EndIf
ProcedureReturn flag
EndProcedure
Debug IsFloat(@"1.2")
Debug IsFloat(@"1..2")
Debug IsFloat(@"1.2.3")
Debug IsFloat(@"1")
Debug IsFloat(@"1.")
Debug IsFloat(@".1")
Debug IsFloat(@"qwerty")
Debug IsFloat(@".")
Debug IsFloat(@"")