[Libreoffice-commits] core.git: wizards/source
Jean-Pierre Ledure (via logerrit)
logerrit at kemper.freedesktop.org
Sat Mar 20 17:02:47 UTC 2021
wizards/source/scriptforge/SF_String.xba | 76 +++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)
New commits:
commit d6de19c3854c86d0514f70e8e679d1b5dc3c1775
Author: Jean-Pierre Ledure <jp at ledure.be>
AuthorDate: Sat Mar 20 16:41:34 2021 +0100
Commit: Jean-Pierre Ledure <jp at ledure.be>
CommitDate: Sat Mar 20 18:02:02 2021 +0100
ScriptForge - (SF_String) Add new IsIBAN() method
SF_String.IsIBAN() returns True if the given string is a valid
International Bank Account Number
https: //en.wikipedia.org/wiki/International_Bank_Account_Number
Change-Id: I7d1257c6a8f8728c523e578e9150fbffa424e5e5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112815
Tested-by: Jean-Pierre Ledure <jp at ledure.be>
Tested-by: Jenkins
Reviewed-by: Jean-Pierre Ledure <jp at ledure.be>
diff --git a/wizards/source/scriptforge/SF_String.xba b/wizards/source/scriptforge/SF_String.xba
index 23010e88c750..6519ac2383d3 100644
--- a/wizards/source/scriptforge/SF_String.xba
+++ b/wizards/source/scriptforge/SF_String.xba
@@ -1070,6 +1070,82 @@ Catch:
GoTo Finally
End Function ' ScriptForge.SF_String.IsHexDigit
+REM -----------------------------------------------------------------------------
+Public Function IsIBAN(Optional ByVal InputStr As Variant) As Boolean
+''' Returns True if the input string is a valid International Bank Account Number
+''' Read https://en.wikipedia.org/wiki/International_Bank_Account_Number
+''' Args:
+''' InputStr: the input string
+''' Returns:
+''' True if the string contains a valid IBAN number. The comparison is not case-sensitive
+''' Examples:
+''' SF_String.IsIBAN("BR15 0000 0000 0000 1093 2840 814 P2") returns True
+
+Dim bIBAN As Boolean ' Return value
+Dim sIBAN As String ' Transformed input string
+Dim sChar As String ' A single character
+Dim sLetter As String ' Integer representation of letters
+Dim iIndex As Integer ' Index in IBAN string
+Dim sLong As String ' String representation of a Long
+Dim iModulo97 As Integer ' Remainder of division by 97
+Dim i As Integer
+Const cstThisSub = "String.IsIBAN"
+Const cstSubArgs = "InputStr"
+
+ If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+ bIBAN = False
+
+Check:
+ If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+ If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
+ End If
+
+Try:
+ sIBAN = ""
+ ' 1. Remove spaces. Check that the total IBAN length is correct as per the country. If not, the IBAN is invalid
+ ' NOT DONE: Country specific
+ sIBAN = Replace(InputStr, " ", "")
+ If Len(sIBAN) < 5 Or Len(sIBAN) > 34 Then GoTo Finally
+
+ ' 2. Move the four initial characters to the end of the string. String is case-insensitive
+ sIBAN = UCase(Mid(sIBAN, 5) & Left(sIBAN, 4))
+
+ ' 3. Replace each letter in the string with two digits, thereby expanding the string, where A = 10, B = 11, ..., Z = 35
+ iIndex = 1
+ Do While iIndex < Len(sIBAN)
+ sChar = Mid(sIBAN, iIndex, 1)
+ If sChar >= "A" And sChar <= "Z" Then
+ sLetter = CStr(Asc(sChar) - Asc("A") + 10)
+ sIBAN = Left(sIBAN, iIndex - 1) & sLetter & Mid(sIBAN, iIndex + 1)
+ iIndex = iIndex + 2
+ ElseIf sChar < "0" Or sChar > "9" Then ' Remove any non-alphanumeric character
+ GoTo Finally
+ Else
+ iIndex = iIndex + 1
+ End If
+ Loop
+
+ ' 4. Interpret the string as a decimal integer and compute the remainder of that number on division by 97
+ ' Computation is done in chunks of 9 digits
+ iIndex = 3
+ sLong = Left(sIBAN, 2)
+ Do While iIndex <= Len(sIBAN)
+ sLong = sLong & Mid(sIBAN, iIndex, 7)
+ iModulo97 = CLng(sLong) Mod 97
+ iIndex = iIndex + Len(sLong) - 2
+ sLong = Right("0" & CStr(iModulo97), 2) ' Force leading zero
+ Loop
+
+ bIBAN = ( iModulo97 = 1 )
+
+Finally:
+ IsIBAN = bIBAN
+ SF_Utils._ExitFunction(cstThisSub)
+ Exit Function
+Catch:
+ GoTo Finally
+End Function ' ScriptForge.SF_String.IsIBAN
+
REM -----------------------------------------------------------------------------
Public Function IsIPv4(Optional ByRef InputStr As Variant) As Boolean
''' Return True if the string is a valid IPv4 address
More information about the Libreoffice-commits
mailing list