-
Notifications
You must be signed in to change notification settings - Fork 35
GetValueLong for SToken #1271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
GetValueLong for SToken #1271
Conversation
| Local token:SToken = params.GetToken(1) | ||
| Local GUID:String = token.value | ||
| Local ID:Long = token.valueLong | ||
| Local ID:Long = token.GetValueLong() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
Willst du noch GetValueInt hinzufuegen? Dann kannst du die Was zu klaeren waere, ist der Umgang mit "Long"-casts ... derzeit muesste man immer strings "trimmen" (whitespace erlauben) und dann schauen ob |
|
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. |
|
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. |
|
Ich habe mal was einfaches zusammengeschraubt ... mit Log10 bekommen wir die Menge an Zahlen heraus, die bei der Konvertierung rauskommen muessen. 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). |
|
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 Functionund 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 FunctionIch denke wir sollten entweder den Logarithmus nutzen, oder die ExtractNumber()-Funktion. |
|
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)? |
|
Du kannst denke ich bequem noch einige der Funktionen oben einbauen. Das Release wuerde ich denke morgen vormittag zusammenschrauben. Releasebau: mein createTVTowerRelease.sh enthaelt schon |
|
Vor dem Release passiert bei mir nichts mehr. |
|
Wie schaut es hier aus? |
|
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. |
closes #1270