IsLatin, IsDigital, IsHex, IsFloat
Posted: 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(@"")