IsLatin, IsDigital, IsHex, IsFloat

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 2146
Joined: Sun May 14, 2017 1:48 am

IsLatin, IsDigital, IsHex, IsFloat

Post by AZJIO »

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.
User avatar
idle
Always Here
Always Here
Posts: 5850
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: IsLatin, IsDigital

Post by idle »

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
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: IsLatin, IsDigital

Post by juergenkulow »

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
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: IsLatin, IsDigital

Post by Rinzwind »

juergenkulow wrote: Tue Apr 19, 2022 7:38 am Sorry but there are 1475 Latin Charaters in Unicode. and
more 37 times 10 digits
Well, can't handle everything. But yeah, accents are also not handled: é ë ê
AZJIO
Addict
Addict
Posts: 2146
Joined: Sun May 14, 2017 1:48 am

Re: IsLatin, IsDigital

Post by AZJIO »

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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6219
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: IsLatin, IsDigital

Post by mk-soft »

Its better

Code: Select all

	If *c = 0 Or *c\c = 0
		ProcedureReturn 0
	EndIf
because a string can be Nothing (NIL)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
AZJIO
Addict
Addict
Posts: 2146
Joined: Sun May 14, 2017 1:48 am

Re: IsLatin, IsDigital

Post by AZJIO »

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(@"")
Post Reply