Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nittka
Copy link
Contributor

@nittka nittka commented Dec 22, 2024

closes #1270

Local token:SToken = params.GetToken(1)
Local GUID:String = token.value
Local ID:Long = token.valueLong
Local ID:Long = token.GetValueLong()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bitte beachte, dass "Casts" von "string zu long" komische Ergebnisse liefern koennten!

Print Long("12-1234")
'-> 12

Print Long("12a-a1234")
'-> 12

Das heisst, eine nicht gefundene GUID, die aber zufaellig mit einer Zahl beginnt und als Long somit eine Zahl<>0 wird... koennte (zufaellig) eine valide ID werden.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mit neueren Blitzmaxversionen (bzw blitz.mod) bekommen wir "meinstring.ToDoubleEx()/ToLongEx()..." und bekommen dort die Dinge schneller, fehlerfreier und konfigurierbar.
Allerdings erzeugt es ebenfalls "12" (nur dann mit der Info, dass ab Char-Index 2 "non-long characters" gefunden wurden).

SuperStrict
Framework Brl.StandardIO

Print Long("12a-a1234")

Local l:Long
Print "result: " + "12a-a1234".ToLongEx(l)
Print "l  " + l

Ausgabe:

12
result: 2
l  12

doc:

// extracts a Long from a string, from the range startPos to endPos
// endPos of -1 means the end of the string
// returns 0 if the string is not a valid Long, or the position of the first character after the Long otherwise

@GWRon
Copy link
Member

GWRon commented Dec 22, 2024

Willst du noch GetValueInt hinzufuegen? Dann kannst du die int(getvalueLong()) casts sparen (naja sparen... sie finden dann im Getter statt :D).

Was zu klaeren waere, ist der Umgang mit "Long"-casts ... derzeit muesste man immer strings "trimmen" (whitespace erlauben) und dann schauen ob string(long(stringvalue)) = stringvalue ist ... ein "teures" Unterfangen.

@nittka
Copy link
Contributor Author

nittka commented Dec 22, 2024

Ich würde das alles nicht mehr vor dem Release machen wollen. Und man muss auch überlegen, ob wirklich alle valueLong auf getValueLong() umgestellt werden sollten; aber in Ruhe.

@GWRon
Copy link
Member

GWRon commented Dec 22, 2024

Kannst Du "bequem" testen, ob es GUIDs gibt, die mit Zahlen anfangen? Die waeren ja die Problemfaelle.

Was auch als kleine Hilfe gehen koennte ... String-Laenge checken.
9223372036854775807 ist das Limit eines Long, das sind 19 Stellen (so hoch sollte keine ID sein ...).
Theoretisch koennten wir also alles mit "s.length > 12" (oder so) getrost als "ist kein Long/Int" abtun. Die meisten GUIDs sind laengere Strings. Und die kuerzeren GUIDs sind von uns "base-12345" und fangen nicht numerisch an.

@GWRon
Copy link
Member

GWRon commented Dec 22, 2024

Ich habe mal was einfaches zusammengeschraubt ... mit Log10 bekommen wir die Menge an Zahlen heraus, die bei der Konvertierung rauskommen muessen.
Erspart uns die String-Konvertierung (string(long...))

SuperStrict
Framework Brl.StandardIO
Import Brl.Retro


Local s:String = "12a-a1234"
Local l:Long = Long(s)
Local digits:Int
If l > 0
	digits = Int(Log10(l)) + 1
ElseIf l = 0
	digits = 1
Else
	digits = Int(Log10(-l)) + 2
EndIf


If digits <> s.Length
	Print "invalid long: " + s + " -> " + l
Else
	Print "valid long :" + s + " -> " + l
EndIf

Sprich wir casten den Text-Wert dort, ermitteln die digits und vergleichen.

Wenn wir Whitespace erlauben ... dann koennten wir "trimmen" (sollte nur einen neuen String anlegen, wenn Trim notwendig war).

@GWRon
Copy link
Member

GWRon commented Dec 22, 2024

Wenn du das Trim vermeiden willst - hier mal die gekuerzte Fassung von "ExtractNumber" die wir in der ScriptExpressionklasse nutzen:

SuperStrict
Framework Brl.StandardIO
Import Brl.Retro


Local s:String = "12a-a1234"
Local l:Long = Long(s)
Local digits:Int
If l > 0
	digits = Int(Log10(l)) + 1
ElseIf l = 0
	digits = 1
Else
	digits = Int(Log10(-l)) + 2
EndIf


If digits <> s.Length
	Print "invalid long: " + s + " -> " + l
Else
	Print "valid long :" + s + " -> " + l
EndIf


Local l2:Long
If ExtractNumber(s, l2)
	Print "valid long : " + l2
Else
	Print "invalid long : " + s
EndIf

Function ExtractNumber:Int(s:String, longValue:Long Var)
	If s.Length = 0 Then Return 0 'no extraction happened
	
	longValue = 0
	
	Local negative:Int = 0
	Local decimalDivider:Long=10
	Local hasSpaceAfter:Int = 0
	Local hasDigits:Int = 0
	Local hasMinus:Int = 0
	Local index:Int = 0

	While index < s.Length
		Local charCode:Int = s[index]
		'only allow spaces once a space after a numeric value happened
		If hasSpaceAfter And charCode <> Asc(" ")
			Return 0 'invalid
		EndIf
		
		'extract number / decimals
		If (charCode >= 48 And charCode <= 57) '48 = "0", 57 = "9"
			longValue = longValue * 10 + (charCode-48)
			hasDigits = True
		' allow minus at begin
		ElseIf charCode = Asc("-") And Not hasDigits
			hasMinus = True
		' allow space at begin and end
		ElseIf charCode = Asc(" ")
			' if space at end - mark it
			If hasDigits
				hasSpaceAfter = True
			EndIf
		' invalid char found
		Else
			Return 0 'invalid
		EndIf
		index :+ 1 'processed 
	Wend

	If hasMinus
		longValue = -1 * longValue
	EndIf
	Return 1
End Function

und hier die lange Fassung, die auch Doubles extrahiert (hier derzeit aber ja nicht noetig):

Function ExtractNumber:Int(s:String, longValue:Long Var, doubleValue:Double Var)
	If s.length = 0 Then Return 0 'no extraction happened
	
	longValue = 0
	doubleValue = 0
	
	Local negative:Int = 0
	Local decimalDivider:Long=10
	Local hasDot:Int = 0
	Local hasSpaceAfter:Int = 0
	Local hasDigits:Int = 0
	Local hasMinus:Int = 0
	Local numberType:Int = 0
	Local index:int = 0

	While index < s.Length
		Local charCode:Int = s[index]
		'only allow spaces once a space after a numeric value happened
		If hasSpaceAfter and charCode <> Asc(" ")
			Return 0 'invalid
		EndIf
		
		'extract number / decimals
		If (charCode >= 48 And charCode <= 57) '48 = "0", 57 = "9"
			If not hasDot 'number
				longValue = longValue * 10 + (charCode-48)
				numberType = 1
			Else 'decimals
				if numberType = 1 Then doubleValue = longValue
				doubleValue :+ Double(charCode-48) / decimalDivider
				decimalDivider :* 10
				numberType = 2
			EndIf
			hasDigits = True
		ElseIf charCode = Asc(".")
			'there can only be one dot
			If hasDot Then Return 0 'invalid
			hasDot = True
		' allow minus at begin
		ElseIf charCode = Asc("-") And not HasDot And Not hasDigits
			hasMinus = True
		' allow space at begin and end
		ElseIf charCode = Asc(" ")
			' if space at end - mark it
			If hasDigits or hasDot
				hasSpaceAfter = True
			EndIf
		' invalid char found
		Else
			Return 0 'invalid
		EndIf
		index :+ 1 'processed 
	Wend

	If hasMinus
		If numberType = 1 	
			longValue = -1 * longValue
		ElseIf numberType = 2
			doubleValue = -1 * doubleValue
		EndIf
	EndIf
	Return numberType
End Function

Ich denke wir sollten entweder den Logarithmus nutzen, oder die ExtractNumber()-Funktion.

@nittka
Copy link
Contributor Author

nittka commented Dec 22, 2024

Eine Prüfung der GUIDs, die in der Datenbank verwendet werden, ist kein Problem. Aber da gibt es definitiv viele, die mit einer Ziffer anfangen. Aber diese Erweiterung ist wirklich nicht dringend. scr0llbaer compilert ja ohnehin selbst, kann also Datenbankerweiterungen, die Codeänderungen benötigen testen.

Wie ist Dein Zeitplan für den Releasebau (prepareRelease.sh nicht vergessen)?

@GWRon
Copy link
Member

GWRon commented Dec 22, 2024

Du kannst denke ich bequem noch einige der Funktionen oben einbauen. Das Release wuerde ich denke morgen vormittag zusammenschrauben.

Releasebau:
Windows-Buildenvironment ist auf dem aktuellen Stand, die .exe lief mehrfach einige AI-Spieltage ohne segfaults durch.
Mac-Environment aktualisiere ich gleich ... aber das kann ich eh nicht so richtig testen (M1-M4 chips nicht hier vorhanden).
Linux ist auf dem aktuellen Stand und bereit zum kompilieren.

mein createTVTowerRelease.sh enthaelt schon


cp Current/config/DEV.xml Current/config/DEV.xml.bak
cd Current
sh prepareRelease.sh
cd ..

...

cp Current/config/DEV.xml.bak Current/config/DEV.xml
rm Current/config/DEV.xml.bak

@nittka
Copy link
Contributor Author

nittka commented Dec 22, 2024

Vor dem Release passiert bei mir nichts mehr.

@GWRon
Copy link
Member

GWRon commented Sep 1, 2025

Wie schaut es hier aus?

@nittka
Copy link
Contributor Author

nittka commented Sep 2, 2025

Kein neuer Stand. Bin an dem Thema gerade nicht dran, würde den Branch aber noch leben lassen. Wenn Du in der Richtung was machst, habe ich nichts dagegen, wenn nicht auch kein Problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SToken sollte ein GetValueInt bekommen

3 participants