Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 


Impressum SF_String.xba   Sprache: unbekannt

 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="SF_String" script:language="StarBasic" script:moduleType="normal">REM =======================================================================================================================
REM ===   The ScriptForge library and its associated libraries are part of the LibreOffice project.    ===
REM ===     Full documentation is available on https://help.libreoffice.org/        ===
REM =======================================================================================================================

Option Compatible
Option Explicit

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' SF_String
''' =========
'''  Singleton class implementing the "ScriptForge.String" service
'''  Implemented as a usual Basic module
'''  Focus on string manipulation, regular expressions, encodings and hashing algorithms
'''  The first argument of almost every method is the string to consider
'''   It is always passed by reference and left unchanged
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Definitions
'''  Line breaks: symbolic name(Ascii number)
'''   LF(10), VT(12), CR(13), LF+CR, File separator(28), Group separator(29), Record separator(30), 
'''   Next Line(133), Line separator(8232), Paragraph separator(8233)
'''  Whitespaces: symbolic name(Ascii number)
'''   Space(32), HT(9), LF(10), VT(11), FF(12), CR(13), Next Line(133), No-break space(160),
'''   Line separator(8232), Paragraph separator(8233)
'''  A quoted string:
'''   The quoting character must be the double quote (")
'''   To preserve a quoting character inside the quoted substring, use (\) or (") as escape character
'''     => [str\"i""ng]  means [str"i"ng]
'''  Escape sequences: symbolic name(Ascii number) = escape sequence
'''   Line feed(10) = "\n"
'''   Carriage return(13) = "\r"
'''   Horizontal tab(9) = "\t"
'''   Double the backslash to ignore the sequence, e.g. "\\n" means "\n" (not "\" & Chr(10)).
'''  Not printable characters:
'''   Defined in the Unicode character database as “Other” or “Separator”
'''   In particular, "control" characters (ascii code <= 0x1F) are not printable
'''
'''  Detailed user documentation:
'''   https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_string.html?DbPAR=BASIC
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Some references:
'''  https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1i18n_1_1KCharacterType.html
'''   com.sun.star.i18n.KCharacterType.###
'''  https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1i18n_1_1XCharacterClassification.html
'''   com.sun.star.i18n.XCharacterClassification

REM ============================================================ MODULE CONSTANTS

''' Most expressions below are derived from https://www.regular-expressions.info/

Const REGEXALPHA   = "^[A-Za-z]+$"   ' Not used
Const REGEXALPHANUM   = "^[\w]+$"
Const REGEXDATEDAY   = "(0[1-9]|[12][0-9]|3[01])"
Const REGEXDATEMONTH  = "(0[1-9]|1[012])"
Const REGEXDATEYEAR   = "(19|20)\d\d"
Const REGEXTIMEHOUR   = "(0[1-9]|1[0-9]|2[0123])"
Const REGEXTIMEMIN   = "([0-5][0-9])"
Const REGEXTIMESEC   = REGEXTIMEMIN
Const REGEXDIGITS   = "^[0-9]+$"
Const REGEXEMAIL   = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$"
Const REGEXFILELINUX  = "^[^<>:;,?""*|\\]+$"
Const REGEXFILEWIN   = "^([A-Z]|[a-z]:)?[^<>:;,?""*|]+$"
Const REGEXHEXA    = "^(0X|&H)?[0-9A-F]+$" ' Includes 0xFF and &HFF
Const REGEXIPV4    = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
Const REGEXNUMBER   = "^[-+]?(([0-9]+)?\.)?[0-9]+([eE][-+]?[0-9]+)?$"
Const REGEXURL    = "^(https?|ftp)://[^\s/$.?#].[^\s]*$"
Const REGEXWHITESPACES  = "^[\s]+$"
Const REGEXLTRIM   = "^[\s]+"
Const REGEXRTRIM   = "[\s]+$"
Const REGEXSPACES   = "[\s]+"

''' Accented characters substitution: https://docs.google.com/spreadsheets/d/1pJKSueZK8RkAcJFQIiKpYUamWSC1u1xVQchK7Z7BIwc/edit#gid=0
'''  (Many of them are in the list, but do not consider the list as closed vs. the Unicode database)

Const cstCHARSWITHACCENT = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿŠšŸŽž" _
        & "ĂăĐđĨĩŨũƠơƯưẠạẢảẤấẦầẨẩẪẫẬậẮắẰằẲẳẴẵẶặẸẹẺẻẼẽẾếỀềỂểỄễỆệỈỉỊịỌọỎỏỐốỒồỔổỖỗỘộỚớỜờỞởỠỡỢợỤụỦủỨứỪừỬửỮữỰựỲỳỴỵỶỷỸỹ₫"
Const cstCHARSWITHOUTACCENT = "AAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyySsYZz" _
        & "AaDdIiUuOoUuAaAaAaAaAaAaAaAaAaAaAaAaEeEeEeEeEeEeEeEeIiIiOoOoOoOoOoOoOoOoOoOoOoOoUuUuUuUuUuUuUuYyYyYyYyd"

REM ===================================================== CONSTRUCTOR/DESTRUCTOR

REM -----------------------------------------------------------------------------
Public Function Dispose() As Variant
 Set Dispose = Nothing
End Function ' ScriptForge.SF_String Explicit destructor

REM ================================================================== PROPERTIES

REM -----------------------------------------------------------------------------
Property Get CHARSWITHACCENT() As String
''' Latin accents
 CHARSWITHACCENT = cstCHARSWITHACCENT
End Property ' ScriptForge.SF_String.CHARSWITHACCENT

REM -----------------------------------------------------------------------------
Property Get CHARSWITHOUTACCENT() As String
''' Latin accents
 CHARSWITHOUTACCENT = cstCHARSWITHOUTACCENT
End Property ' ScriptForge.SF_String.CHARSWITHOUTACCENT

''' Symbolic constants for linebreaks
REM -----------------------------------------------------------------------------
Property Get sfCR() As Variant
''' Carriage return
 sfCR = Chr(13)
End Property ' ScriptForge.SF_String.sfCR

REM -----------------------------------------------------------------------------
Property Get sfCRLF() As Variant
''' Carriage return
 sfCRLF = Chr(13) & Chr(10)
End Property ' ScriptForge.SF_String.sfCRLF

REM -----------------------------------------------------------------------------
Property Get sfLF() As Variant
''' Linefeed
 sfLF = Chr(10)
End Property ' ScriptForge.SF_String.sfLF

REM -----------------------------------------------------------------------------
Property Get sfNEWLINE() As Variant
''' Linefeed or Carriage return + Linefeed
 sfNEWLINE = Iif(GetGuiType() = 1, Chr(13), "") & Chr(10)
End Property ' ScriptForge.SF_String.sfNEWLINE

REM -----------------------------------------------------------------------------
Property Get sfTAB() As Variant
''' Horizontal tabulation
 sfTAB = Chr(9)
End Property ' ScriptForge.SF_String.sfTAB

REM -----------------------------------------------------------------------------
Property Get ObjectType As String
''' Only to enable object representation
 ObjectType = "SF_String"
End Property ' ScriptForge.SF_String.ObjectType

REM -----------------------------------------------------------------------------
Property Get ServiceName As String
''' Internal use
 ServiceName = "ScriptForge.String"
End Property ' ScriptForge.SF_String.ServiceName

REM ============================================================== PUBLIC METHODS

REM -----------------------------------------------------------------------------
Public Function Capitalize(Optional ByRef InputStr As Variant) As String
''' Return the input string with the 1st character of each word in title case
''' Args:
'''  InputStr: the input string
''' Returns:
'''  The input string with the 1st character of each word in title case
''' Examples:
'''  SF_String.Capitalize("this is a title for jean-pierre") returns "This Is A Title For Jean-Pierre"

Dim sCapital As String    ' Return value
Dim lLength As Long     ' Length of input string
Dim oLocale As Object    ' com.sun.star.lang.Locale
Dim oChar As Object     ' com.sun.star.i18n.CharacterClassification
Const cstThisSub = "String.Capitalize"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sCapital = ""

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 lLength = Len(InputStr)
 If lLength > 0 Then
  Set oLocale = SF_Utils._GetUNOService("SystemLocale")
  Set oChar = SF_Utils._GetUNOService("CharacterClass")
  sCapital = oChar.toTitle(InputStr, 0, lLength * 4, oLocale) ' length * 4 because length is expressed in bytes
 End If

Finally:
 Capitalize = sCapital
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.Capitalize

REM -----------------------------------------------------------------------------
Public Function Count(Optional ByRef InputStr As Variant _
        , Optional ByVal Substring As Variant _
        , Optional ByRef IsRegex As Variant _
        , Optional ByVal CaseSensitive As Variant _
        ) As Long
''' Counts the number of occurrences of a substring or a regular expression within a string
''' Args:
'''  InputStr: the input stringto examine
'''  Substring: the substring to identify
'''  IsRegex: True if Substring is a regular expression (default = False)
'''  CaseSensitive: default = False
''' Returns:
'''  The number of occurrences as a Long
''' Examples:
'''  SF_String.Count("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "\b[a-z]+\b", IsRegex := True, CaseSensitive := True)
'''    returns 7 (the number of words in lower case)
'''  SF_String.Count("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "or", CaseSensitive := False)
'''    returns 2


Dim lOccurrences As Long    ' Return value
Dim lStart As Long      ' Start index of search
Dim sSubstring As String    ' Substring to replace
Dim iCaseSensitive As Integer   ' Integer alias for boolean CaseSensitive
Const cstThisSub = "String.Count"
Const cstSubArgs = "InputStr, Substring, [IsRegex=False], [CaseSensitive=False]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 lOccurrences = 0

Check:
 If IsMissing(IsRegex) Or IsEmpty(IsRegex) Then IsRegex = False
 If IsMissing(CaseSensitive) Or IsEmpty(CaseSensitive) Then CaseSensitive = False
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Substring, "Substring", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(IsRegex, "IsRegex", V_BOOLEAN) Then GoTo Finally
  If Not SF_Utils._Validate(CaseSensitive, "CaseSensitive", V_BOOLEAN) Then GoTo Finally
 End If

Try:
 iCaseSensitive = Iif(CaseSensitive, 0, 1)    ' 1 = False ;)
 lStart = 1

 Do While lStart >= 1 And lStart <= Len(InputStr)
  Select Case IsRegex
   Case False ' Use InStr
    lStart = InStr(lStart, InputStr, Substring, iCaseSensitive)
    If lStart = 0 Then Exit Do
    lStart = lStart + Len(Substring)
   Case True ' Use FindRegex
    sSubstring = SF_String.FindRegex(InputStr, Substring, lStart, CaseSensitive)
    If lStart = 0 Then Exit Do
    lStart = lStart + Len(sSubstring)
  End Select
  lOccurrences = lOccurrences + 1
 Loop

Finally:
 Count = lOccurrences
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.Count

REM -----------------------------------------------------------------------------
Public Function EndsWith(Optional ByRef InputStr As Variant _
       , Optional ByVal Substring As Variant _
       , Optional ByVal CaseSensitive As Variant _
       ) As Boolean
''' Returns True if the last characters of InputStr are identical to Substring
''' Args:
'''  InputStr: the input string
'''  Substring: the suffixing characters
'''  CaseSensitive: default = False
''' Returns:
'''  True if the comparison is satisfactory
'''  False if either InputStr or Substring have a length = 0
'''  False if Substr is longer than InputStr
''' Examples:
'''  SF_String.EndsWith("abcdefg", "EFG") returns True
'''  SF_String.EndsWith("abcdefg", "EFG", CaseSensitive := True) returns False

Dim bEndsWith As Boolean    ' Return value
Dim lSub As Long      ' Length of SUbstring
Const cstThisSub = "String.EndsWith"
Const cstSubArgs = "InputStr, Substring, [CaseSensitive=False]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bEndsWith = False

Check:
 If IsMissing(CaseSensitive) Or IsEmpty(CaseSensitive) Then CaseSensitive = False
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Substring, "Substring", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(CaseSensitive, "CaseSensitive", V_BOOLEAN) Then GoTo Finally
 End If

Try:
 lSub = Len(Substring)
 If Len(InputStr) > 0 And lSub > 0 And lSub <= Len(InputStr) Then
  bEndsWith = ( StrComp(Right(InputStr, lSub), Substring, Iif(CaseSensitive, 1, 0)) = 0 )
 End If

Finally:
 EndsWith = bEndsWith
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.EndsWith

REM -----------------------------------------------------------------------------
Public Function Escape(Optional ByRef InputStr As Variant) As String
''' Convert any hard line breaks or tabs by their escaped equivalent
''' Args:
'''  InputStr: the input string
''' Returns:
'''  The input string after replacement of "\", Chr(10), Chr(13), Chr(9)characters
''' Examples:
'''  SF_String.Escape("abc" & Chr(10) & Chr(9) & "def\n") returns "abc\n\tdef\\n"

Dim sEscape As String     ' Return value
Const cstThisSub = "String.Escape"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sEscape = ""

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 sEscape = SF_String.ReplaceStr( InputStr _       
       , Array("\", SF_String.sfLF, SF_String.sfCR, SF_String.sfTAB) _
       , Array("\\", "\n", "\r", "\t") _
       )

Finally:
 Escape = sEscape
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.Escape

REM -----------------------------------------------------------------------------
Public Function ExpandTabs(Optional ByRef InputStr As Variant _
        , Optional ByVal TabSize As Variant _
        ) As String
''' Return the input string with each TAB (Chr(9)) character replaced by the adequate number of spaces
''' Args:
'''  InputStr: the input string
'''  TabSize: defines the TAB positions at TabSize + 1, 2 * TabSize + 1 , ... N * TabSize + 1
'''   Default = 8
''' Returns:
'''  The input string with spaces replacing the TAB characters
'''  If the input string contains line breaks, the TAB positions are reset
''' Examples:
'''  SF_String.ExpandTabs("abc" & SF_String.sfTAB & SF_String.sfTAB & "def", 4) returns "abc     def"
'''  SF_String.ExpandTabs("abc" & SF_String.sfTAB & "def" & SF_String.sfLF & SF_String.sfTAB & "ghi")
'''    returns "abc     def" & SF_String.sfLF & "        ghi"

Dim sExpanded As String    ' Return value
Dim lCharPosition As Long   ' Position of current character in current line in expanded string
Dim lSpaces As Long     ' Spaces counter
Dim sChar As String     ' A single character
Dim i As Long
Const cstTabSize = 8
Const cstThisSub = "String.ExpandTabs"
Const cstSubArgs = "InputStr, [TabSize=8]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sExpanded = ""

Check:
 If IsMissing(TabSize) Or IsEmpty(TabSize) Then TabSize = cstTabSize
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(TabSize, "TabSize", V_NUMERIC) Then GoTo Finally
 End If
 If TabSize <= 0 Then TabSize = cstTabSize

Try:
 lCharPosition = 0
 If Len(InputStr) > 0 Then
  For i = 1 To Len(InputStr)
   sChar = Mid(InputStr, i, 1)
   Select Case sChar
    Case SF_String.sfLF, Chr(12), SF_String.sfCR, Chr(28), Chr(29), Chr(30), Chr(133), Chr(8232), Chr(8233)
     sExpanded = sExpanded & sChar
     lCharPosition = 0
    Case SF_String.sfTAB
     lSpaces = Int(lCharPosition / TabSize + 1) * TabSize - lCharPosition
     sExpanded = sExpanded & Space(lSpaces)
     lCharPosition = lCharPosition + lSpaces
    Case Else
     sExpanded = sExpanded & sChar
     lCharPosition = lCharPosition + 1
   End Select
  Next i
 End If

Finally:
 ExpandTabs = sExpanded
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.ExpandTabs

REM -----------------------------------------------------------------------------
Public Function FilterNotPrintable(Optional ByRef InputStr As Variant _
          , Optional ByVal ReplacedBy As Variant _
          ) As String
''' Return the input string in which all the not printable characters are replaced by ReplacedBy
''' Among others, control characters (Ascii <= 1F) are not printable
''' Args:
'''  InputStr: the input string
'''  ReplacedBy: zero, one or more characters replacing the found not printable characters
'''   Default = the zero-length string
''' Returns:
'''  The input string in which all the not printable characters are replaced by ReplacedBy
''' Examples:
'''  SF_String.FilterNotPrintable("àén ΣlPµ" & Chr(10) & " Русский", "\n") returns "àén ΣlPµ\n Русский"

Dim sPrintable As String   ' Return value
Dim bPrintable As Boolean   ' Is a single character printable ?
Dim lLength As Long     ' Length of InputStr
Dim lReplace As Long    ' Length of ReplacedBy
Dim oChar As Object     ' com.sun.star.i18n.CharacterClassification
Dim oLocale As Object    ' com.sun.star.lang.Locale
Dim lType As Long     ' com.sun.star.i18n.KCharacterType
Dim sChar As String     ' A single character
Dim lPRINTABLE As Long    : lPRINTABLE = com.sun.star.i18n.KCharacterType.PRINTABLE
Dim i As Long
Const cstThisSub = "String.FilterNotPrintable"
Const cstSubArgs = "InputStr, [ReplacedBy=""""]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sPrintable = ""

Check:
 If IsMissing(ReplacedBy) Or IsEmpty(ReplacedBy) Then ReplacedBy = ""
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(ReplacedBy, "ReplacedBy", V_STRING) Then GoTo Finally
 End If

Try:
 lLength = Len(InputStr)
 lReplace = Len(ReplacedBy)
 If lLength > 0 Then
  Set oLocale = SF_Utils._GetUNOService("SystemLocale")
  Set oChar = SF_Utils._GetUNOService("CharacterClass")
  For i = 0 To lLength - 1
   sChar = Mid(InputStr, i + 1, 1)
   lType = oChar.getCharacterType(sChar, 0, oLocale)
   ' Parenthses (), [], {} have a KCharacterType = 0
   bPrintable = ( (lType And lPRINTABLE) = lPRINTABLE Or (lType = 0 And Asc(sChar) <= 127) )
   If Not bPrintable Then
    If lReplace > 0 Then sPrintable = sPrintable & ReplacedBy
   Else
    sPrintable = sPrintable & sChar
   End If
  Next i
 End If

Finally:
 FilterNotPrintable = sPrintable
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.FilterNotPrintable

REM -----------------------------------------------------------------------------
Public Function FindRegex(Optional ByRef InputStr As Variant _
        , Optional ByVal Regex As Variant _
        , Optional ByRef Start As Variant _
        , Optional ByVal CaseSensitive As Variant _
        , Optional ByVal Forward As Variant _
        ) As String
''' Find in InputStr a substring matching a given regular expression
''' Args:
'''  InputStr: the input string to be searched for the expression
'''  Regex: the regular expression
'''  Start (passed by reference): where to start searching from
'''   Should be = 1 (Forward = True) or = Len(InputStr) (Forward = False) the 1st time
'''   After execution points to the first character of the found substring
'''  CaseSensitive: default = False
'''  Forward: True (default) or False (backward)
''' Returns:
'''  The found substring matching the regular expression
'''   A zero-length string if not found (Start is set to 0)
''' Examples:
'''  Dim lStart As Long : lStart = 1
'''  SF_String.FindRegex("abCcdefghHij", "C.*H", lStart, CaseSensitive := True) returns "CcdefghH"
'''   Above statement may be reexecuted for searching the same or another pattern
'''    by starting from lStart + Len(matching string)

Dim sOutput As String     ' Return value
Dim oTextSearch As Object    ' com.sun.star.util.TextSearch
Dim vOptions As Variant     ' com.sun.star.util.SearchOptions
Dim lEnd As Long      ' Upper limit of search area
Dim vResult As Object     ' com.sun.star.util.SearchResult
Const cstThisSub = "String.FindRegex"
Const cstSubArgs = "InputStr, Regex, [Start=1], [CaseSensitive=False], [Forward=True]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sOutput = ""

Check:
 If IsMissing(Start) Or IsEmpty(Start) Then Start = 1
 If IsMissing(CaseSensitive) Or IsEmpty(CaseSensitive) Then CaseSensitive = False
 If IsMissing(Forward) Or IsEmpty(Forward) Then Forward = True
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Regex, "Regex", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Start, "Start", V_NUMERIC) Then GoTo Finally
  If Not SF_Utils._Validate(CaseSensitive, "CaseSensitive", V_BOOLEAN) Then GoTo Finally
  If Not SF_Utils._Validate(Forward, "Forward", V_BOOLEAN) Then GoTo Finally
 End If
 If Start <= 0 Or Start > Len(InputStr) Then GoTo Finally

Try:
 sOutput = ""
 Set oTextSearch = SF_Utils._GetUNOService("TextSearch")
 ' Set pattern search options
 vOptions = SF_Utils._GetUNOService("SearchOptions")
 With vOptions
  .searchString = Regex
  If CaseSensitive Then .transliterateFlags = 0 Else .transliterateFlags = com.sun.star.i18n.TransliterationModules.IGNORE_CASE
 End With
 ' Run search
 With oTextSearch
  .setOptions(vOptions)
  If Forward Then
   lEnd = Len(InputStr)
   vResult = .searchForward(InputStr, Start - 1, lEnd)
  Else
   lEnd = 1
   vResult = .searchBackward(InputStr, Start, lEnd - 1)
  End If
 End With
 ' https://api.libreoffice.org/docs/idl/ref/structcom_1_1sun_1_1star_1_1util_1_1SearchResult.html
 With vResult
  If .subRegExpressions >= 1 Then
   If Forward Then
    Start = .startOffset(0) + 1
    lEnd = .endOffset(0) + 1
   Else
    Start = .endOffset(0) + 1
    lEnd = .startOffset(0) + 1
   End If
   sOutput = Mid(InputStr, Start, lEnd - Start)
  Else
   Start = 0
  End If
 End With

Finally:
 FindRegex = sOutput
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.FindRegex

REM -----------------------------------------------------------------------------
Public Function GetProperty(Optional ByVal PropertyName As Variant) As Variant
''' Return the actual value of the given property
''' Args:
'''  PropertyName: the name of the property as a string
''' Returns:
'''  The actual value of the property
''' Exceptions
'''  ARGUMENTERROR  The property does not exist

Const cstThisSub = "String.GetProperty"
Const cstSubArgs = "PropertyName"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 GetProperty = Null

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(PropertyName, "PropertyName", V_STRING, Properties()) Then GoTo Catch
 End If

Try:
 Select Case UCase(PropertyName)
  Case "SFCR"   : GetProperty = sfCR
  Case "SFCRLF"  : GetProperty = sfCRLF
  Case "SFLF"   : GetProperty = sfLF
  Case "SFNEWLINE" : GetProperty = sfNEWLINE
  Case "SFTAB"  : GetProperty = sfTAB
  Case Else
 End Select

Finally:
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.GetProperty

REM -----------------------------------------------------------------------------
Public Function HashStr(Optional ByVal InputStr As Variant _
       , Optional ByVal Algorithm As Variant _
       ) As String
''' Return an hexadecimal string representing a checksum of the given input string
''' Next algorithms are supported: MD5, SHA1, SHA224, SHA256, SHA384 and SHA512
''' Args:
'''  InputStr: the string to be hashed
'''  Algorithm: The hashing algorithm to use
''' Returns:
'''  The requested checksum as a string. Hexadecimal digits are lower-cased
'''  A zero-length string when an error occurred
''' Example:
'''  Print SF_String.HashStr("œ∑¡™£¢∞§¶•ªº–≠œ∑´®†¥¨ˆøπ“‘åß∂ƒ©˙∆˚¬", "MD5") ' 616eb9c513ad07cd02924b4d285b9987

Dim sHash As String    ' Return value
Const cstPyHelper = "$" & "_SF_String__HashStr"
Const cstThisSub = "String.HashStr"
Const cstSubArgs = "InputStr, Algorithm=""MD5""|""SHA1""|""SHA224""|""SHA256""|""SHA384""|""SHA512"""

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sHash = ""

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Algorithm, "Algorithm", V_STRING _
     , Array("MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512")) Then GoTo Finally
 End If

Try:
 With ScriptForge.SF_Session
  sHash = .ExecutePythonScript(.SCRIPTISSHARED, _SF_.PythonHelper & cstPyHelper _
      , InputStr, LCase(Algorithm))
 End With

Finally:
 HashStr = sHash
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function    '   ScriptForge.SF_String.HashStr

REM -----------------------------------------------------------------------------
Public Function HtmlEncode(Optional ByRef InputStr As Variant) As String
''' &-encoding of the input string (e.g. "é" becomes "&eacute;" or numeric equivalent
''' Args:
'''  InputStr: the input string
''' Returns:
'''  the encoded string
''' Examples:
'''  SF_String.HtmlEncode("<a href=""https://a.b.com"">From α to ω</a>")
'''   returns "&lt;a href=&quot;https://a.b.com&quot;&gt;From &#945; to &#969;&lt;/a&gt;"

Dim sEncode As String     ' Return value
Dim lPos As Long      ' Position in InputStr
Dim sChar As String      ' A single character extracted from InputStr
Dim i As Long
Const cstThisSub = "String.HtmlEncode"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 sEncode = ""

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then
  lPos = 1
  sEncode = InputStr
  Do While lPos <= Len(sEncode)
   sChar = Mid(sEncode, lPos, 1)
   ' Leave as is or encode every single char
   Select Case sChar
    Case """"   : sChar = "&quot;"
    Case "&"   : sChar = "&amp;"
    Case "<"   : sChar = "&lt;"
    Case ">"   : sChar = "&gt;"
    Case "'"   : sChar = "&apos;"
    Case ":", "/", "?", "#", "[", "]", "@"    ' Reserved characters
    Case SF_String.sfCR : sChar = ""   ' Carriage return
    Case SF_String.sfLF : sChar = "<br>"  ' Line Feed
    Case < Chr(126)
    Case "€"   : sChar = "&euro;"
    Case Else   : sChar = "&#" & Asc(sChar) & ";"
   End Select
   If Len(sChar) = 1 Then
    Mid(sEncode, lPos, 1) = sChar
   Else
    sEncode = Left(sEncode, lPos - 1) & sChar & Mid(sEncode, lPos + 1)
   End If
   lPos = lPos + Len(sChar)
  Loop
 End If

Finally:
 HtmlEncode = sEncode
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.HtmlEncode

REM -----------------------------------------------------------------------------
Public Function IsADate(Optional ByRef InputStr As Variant _
       , Optional ByVal DateFormat _
       ) As Boolean
''' Return True if the string is a valid date respecting the given format
''' Args:
'''  InputStr: the input string
'''  DateFormat: either YYYY-MM-DD (default), DD-MM-YYYY or MM-DD-YYYY
'''   The dash (-) may be replaced by a dot (.), a slash (/) or a space
''' Returns:
'''  True if the string contains a valid date and there is at least one character
'''  False otherwise or if the date format is invalid
''' Examples:
'''  SF_String.IsADate("2019-12-31", "YYYY-MM-DD") returns True

Dim bADate As Boolean    ' Return value
Dim sFormat As String    ' Alias for DateFormat
Dim iYear As Integer    ' Alias of year in input string
Dim iMonth As Integer    ' Alias of month in input string
Dim iDay As Integer     ' Alias of day in input string
Dim dDate As Date     ' Date value
Const cstFormat = "YYYY-MM-DD"  ' Default date format
Const cstFormatRegex = "(YYYY[- /.]MM[- /.]DD|MM[- /.]DD[- /.]YYYY|DD[- /.]MM[- /.]YYYY)"
         ' The regular expression the format must match
Const cstThisSub = "String.IsADate"
Const cstSubArgs = "InputStr, [DateFormat=""" & cstFormat & """]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bADate = False

Check:
 If IsMissing(DateFormat) Or IsEmpty(DateFormat) Then DateFormat = "YYYY-MM-DD"
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(DateFormat, "DateFormat", V_STRING) Then GoTo Finally
 End If
 sFormat = UCase(DateFormat)
 If Len(sFormat) <> Len(cstFormat)Then GoTo Finally
 If sFormat <> cstFormat Then ' Do not check if default format
  If Not SF_String.IsRegex(sFormat, cstFormatRegex) Then GoTo Finally
 End If

Try:
 If Len(InputStr) = Len(DateFormat) Then
  ' Extract the date components YYYY, MM, DD from the input string
  iYear = CInt(Mid(InputStr, InStr(sFormat, "YYYY"), 4))
  iMonth = CInt(Mid(InputStr, InStr(sFormat, "MM"), 2))
  iDay = CInt(Mid(InputStr, InStr(sFormat, "DD"), 2))
  ' Check the validity of the date
  On Local Error GoTo NotADate
  dDate = DateSerial(iYear, iMonth, iDay)
  bADate = True ' Statement reached only if no error
 End If

Finally:
 IsADate = bADate
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
NotADate:
 On Error GoTo 0  ' Reset the error object
 GoTo Finally
End Function ' ScriptForge.SF_String.IsADate

REM -----------------------------------------------------------------------------
Public Function IsAlpha(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are alphabetic
''' Alphabetic characters are those characters defined in the Unicode character database as “Letter”
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string is alphabetic and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsAlpha("àénΣlPµ") returns True
''' Note:
'''  Use SF_String.IsRegex("...", REGEXALPHA) to limit characters to latin alphabet

Dim bAlpha As Boolean    ' Return value
Dim lLength As Long     ' Length of InputStr
Dim oChar As Object     ' com.sun.star.i18n.CharacterClassification
Dim oLocale As Object    ' com.sun.star.lang.Locale
Dim lType As Long     ' com.sun.star.i18n.KCharacterType
Dim lLETTER As Long     : lLETTER = com.sun.star.i18n.KCharacterType.LETTER
Dim i As Long
Const cstThisSub = "String.IsAlpha"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bAlpha = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 lLength = Len(InputStr)
 If lLength > 0 Then
  Set oLocale = SF_Utils._GetUNOService("SystemLocale")
  Set oChar = SF_Utils._GetUNOService("CharacterClass")
  For i = 0 To lLength - 1
   lType = oChar.getCharacterType(InputStr, i, oLocale)
   bAlpha = ( (lType And lLETTER) = lLETTER )
   If Not bAlpha Then Exit For
  Next i
 End If

Finally:
 IsAlpha = bAlpha
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsAlpha

REM -----------------------------------------------------------------------------
Public Function IsAlphaNum(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are alphabetic, digits or "_" (underscore)
''' The first character must not be a digit
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string is alphanumeric and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsAlphaNum("_ABC_123456_abcàénΣlPµ") returns True

Dim bAlphaNum As Boolean   ' Return value
Dim sInputStr As String    ' Alias of InputStr without underscores
Dim sFirst As String    ' Leftmost character of InputStr
Dim lLength As Long     ' Length of InputStr
Dim oChar As Object     ' com.sun.star.i18n.CharacterClassification
Dim oLocale As Object    ' com.sun.star.lang.Locale
Dim lType As Long     ' com.sun.star.i18n.KCharacterType
Dim lLETTER As Long     : lLETTER = com.sun.star.i18n.KCharacterType.LETTER
Dim lDIGIT As Long     : lDIGIT = com.sun.star.i18n.KCharacterType.DIGIT
Dim i As Long
Const cstThisSub = "String.IsAlphaNum"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bAlphaNum = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 lLength = Len(InputStr)
 If lLength > 0 Then
  sFirst = Left(InputStr, 1)
  bAlphanum = ( sFirst < "0" Or sFirst > "9" )
  If bAlphaNum Then
   sInputStr = Replace(InputStr, "_", "A")  ' Replace by an arbitrary alphabetic character
   Set oLocale = SF_Utils._GetUNOService("SystemLocale")
   Set oChar = SF_Utils._GetUNOService("CharacterClass")
   For i = 0 To lLength - 1
    lType = oChar.getCharacterType(sInputStr, i, oLocale)
    bAlphaNum = ( (lType And lLETTER) = lLETTER  _
        Or (lType And lDIGIT) = lDIGIT )
    If Not bAlphaNum Then Exit For
   Next i
  End If
 End If

Finally:
 IsAlphaNum = bAlphaNum
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsAlphaNum

REM -----------------------------------------------------------------------------
Public Function IsAscii(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are Ascii characters
''' Ascii characters are those characters defined between &H00 and &H7F
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string is Ascii and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsAscii("a%?,25") returns True

Dim bAscii As Boolean    ' Return value
Dim lLength As Long     ' Length of InputStr
Dim sChar As String     ' Single character
Dim i As Long
Const cstThisSub = "String.IsAscii"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bAscii = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 lLength = Len(InputStr)
 If lLength > 0 Then
  For i = 1 To lLength
   sChar = Mid(InputStr, i, 1)
   bAscii = ( Asc(sChar) <= 127 )
   If Not bAscii Then Exit For
  Next i
 End If

Finally:
 IsAscii = bAscii
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsAscii

REM -----------------------------------------------------------------------------
Public Function IsDigit(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are digits
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains only digits and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsDigit("123456") returns True

Dim bDigit As Boolean    ' Return value
Const cstThisSub = "String.IsDigit"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bDigit = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bDigit = SF_String.IsRegex(InputStr, REGEXDIGITS, CaseSensitive := False)

Finally:
 IsDigit = bDigit
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsDigit

REM -----------------------------------------------------------------------------
Public Function IsEmail(Optional ByRef InputStr As Variant) As Boolean
''' Return True if the string is a valid email address
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains an email address and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsEmail("first.last@something.org") returns True

Dim bEmail As Boolean    ' Return value
Const cstThisSub = "String.IsEmail"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bEmail = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bEmail = SF_String.IsRegex(InputStr, REGEXEMAIL, CaseSensitive := False)

Finally:
 IsEmail = bEmail
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsEmail

REM -----------------------------------------------------------------------------
Public Function IsFileName(Optional ByRef InputStr As Variant _
        , Optional ByVal OSName As Variant _
        ) As Boolean
''' Return True if the string is a valid filename in a given operating system
''' Args:
'''  InputStr: the input string
'''  OSName: Windows, Linux, macOS or Solaris
'''   The default is the current operating system on which the script is run
''' Returns:
'''  True if the string contains a valid filename and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsFileName("/home/a file name.odt", "LINUX") returns True

Dim bFileName As Boolean    ' Return value
Dim sRegex As String     ' Regex to apply depending on OS
Const cstThisSub = "String.IsFileName"
Const cstSubArgs = "InputStr, [OSName=""Windows""|""Linux""|""macOS""|Solaris""]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bFileName = False

Check:
 If IsMissing(OSName) Or IsEmpty(OSName) Then
  If _SF_.OSname = "" Then _SF_.OSName = SF_Platform.OSName
  OSName = _SF_.OSName
 End If
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(OSName, "OSName", V_STRING, Array("Windows", "Linux", "macOS", "Solaris")) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then
  Select Case UCase(OSName)
   Case "LINUX", "MACOS", "SOLARIS" : sRegex = REGEXFILELINUX
   Case "WINDOWS"      : sRegex = REGEXFILEWIN
  End Select
  bFileName = SF_String.IsRegex(InputStr, sRegex, CaseSensitive := False)
 End If

Finally:
 IsFileName = bFileName
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsFileName

REM -----------------------------------------------------------------------------
Public Function IsHexDigit(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are hexadecimal digits
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains only hexadecimal igits and there is at least one character
'''   The prefixes "0x" and "&H" are admitted
'''  False otherwise
''' Examples:
'''  SF_String.IsHexDigit("&H00FF") returns True

Dim bHexDigit As Boolean    ' Return value
Const cstThisSub = "String.IsHexDigit"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bHexDigit = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bHexDigit = SF_String.IsRegex(InputStr, REGEXHEXA, CaseSensitive := False)

Finally:
 IsHexDigit = bHexDigit
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
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
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains a valid IPv4 address and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsIPv4("192.168.1.50") returns True

Dim bIPv4 As Boolean    ' Return value
Const cstThisSub = "String.IsIPv4"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bIPv4 = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bIPv4 = SF_String.IsRegex(InputStr, REGEXIPV4, CaseSensitive := False)

Finally:
 IsIPv4 = bIPv4
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsIPv4

REM -----------------------------------------------------------------------------
Public Function IsLike(Optional ByRef InputStr As Variant _
       , Optional ByVal Pattern As Variant _
       , Optional ByVal CaseSensitive As Variant _
       ) As Boolean
''' Returns True if the whole input string matches a given pattern containing wildcards
''' Args:
'''  InputStr: the input string
'''  Pattern: the pattern as a string
'''   Admitted wildcard are: the "?" represents any single character
'''         the "*" represents zero, one, or multiple characters
'''  CaseSensitive: default = False
''' Returns:
'''  True if a match is found
'''  Zero-length input or pattern strings always return False
''' Examples:
'''  SF_String.IsLike("aAbB", "?A*") returns True
'''  SF_String.IsLike("C:\a\b\c\f.odb", "?:*.*") returns True

Dim bLike As Boolean           ' Return value
' Build an equivalent regular expression by escaping the special characters present in Pattern
Dim sRegex As String           ' Equivalent regular expression
Const cstSpecialChars = "\,^,$,.,|,+,(,),[,{,?,*"    ' List of special chars in regular expressions
Const cstEscapedChars = "\\,\^,\$,\.,\|,\+,\(,\),\[,\{,.,.*"

Const cstThisSub = "String.IsLike"
Const cstSubArgs = "InputStr, Pattern, [CaseSensitive=False]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bLike = False

Check:
 If IsMissing(CaseSensitive) Or IsEmpty(CaseSensitive) Then CaseSensitive = False
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Pattern, "Pattern", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(CaseSensitive, "CaseSensitive", V_BOOLEAN) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 And Len(Pattern) > 0 Then
  ' Substitute special chars by escaped chars
  sRegex = SF_String.ReplaceStr(Pattern, Split(cstSPecialChars, ","), Split(cstEscapedChars, ","))
  bLike = SF_String.IsRegex(InputStr, sRegex, CaseSensitive)
 End If

Finally:
 IsLike = bLike
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsLike

REM -----------------------------------------------------------------------------
Public Function IsLower(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are in lower case
''' Non alphabetic characters are ignored
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains only lower case characters and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsLower("abc'(-xyz") returns True

Dim bLower As Boolean    ' Return value
Const cstThisSub = "String.IsLower"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bLower = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bLower = ( StrComp(InputStr, LCase(InputStr), 1) = 0 )

Finally:
 IsLower = bLower
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsLower

REM -----------------------------------------------------------------------------
Public Function IsPrintable(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are printable
''' In particular, control characters (Ascii <= 1F) are not printable
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string is printable and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsPrintable("àén ΣlPµ Русский") returns True

Dim bPrintable As Boolean   ' Return value
Dim lLength As Long     ' Length of InputStr
Dim oChar As Object     ' com.sun.star.i18n.CharacterClassification
Dim oLocale As Object    ' com.sun.star.lang.Locale
Dim lType As Long     ' com.sun.star.i18n.KCharacterType
Dim sChar As String     ' A single character
Dim lPRINTABLE As Long    : lPRINTABLE = com.sun.star.i18n.KCharacterType.PRINTABLE
Dim i As Long
Const cstThisSub = "String.IsPrintable"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bPrintable = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 lLength = Len(InputStr)
 If lLength > 0 Then
  Set oLocale = SF_Utils._GetUNOService("SystemLocale")
  Set oChar = SF_Utils._GetUNOService("CharacterClass")
  For i = 0 To lLength - 1
   sChar = Mid(InputStr, i + 1, 1)
   lType = oChar.getCharacterType(sChar, 0, oLocale)
   ' Parenthses (), [], {} have a KCharacterType = 0
   bPrintable = ( (lType And lPRINTABLE) = lPRINTABLE Or (lType = 0 And Asc(sChar) <= 127) )
   If Not bPrintable Then Exit For
  Next i
 End If

Finally:
 IsPrintable = bPrintable
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsPrintable

REM -----------------------------------------------------------------------------
Public Function IsRegex(Optional ByRef InputStr As Variant _
       , Optional ByVal Regex As Variant _
       , Optional ByVal CaseSensitive As Variant _
       ) As Boolean
''' Returns True if the whole input string matches a given regular expression
''' Args:
'''  InputStr: the input string
'''  Regex: the regular expression as a string
'''  CaseSensitive: default = False
''' Returns:
'''  True if a match is found
'''  Zero-length input or regex strings always return False
''' Examples:
'''  SF_String.IsRegex("aAbB", "[A-Za-z]+") returns True

Dim bRegex As Boolean   ' Return value
Dim lStart As Long    ' Must be 1
Dim sMatch As String   ' Matching string
Const cstBegin = "^"   ' Beginning of line symbol
Const cstEnd = "$"    ' End of line symbol
Const cstThisSub = "String.IsRegex"
Const cstSubArgs = "InputStr, Regex, [CaseSensitive=False]"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bRegex = False

Check:
 If IsMissing(CaseSensitive) Or IsEmpty(CaseSensitive) Then CaseSensitive = False
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(Regex, "Regex", V_STRING) Then GoTo Finally
  If Not SF_Utils._Validate(CaseSensitive, "CaseSensitive", V_BOOLEAN) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 And Len(Regex) > 0 Then
  ' Whole string must match Regex
  lStart = 1
  If Left(Regex, 1) <> cstBegin Then Regex = cstBegin & Regex
  If Right(Regex, 1) <> cstEnd Then Regex = Regex & cstEnd
  sMatch = SF_String.FindRegex(InputStr, Regex, lStart, CaseSensitive)
  ' Match ?
  bRegex = ( lStart = 1 And Len(sMatch) = Len(InputStr) )
 End If

Finally:
 IsRegex = bRegex
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsRegex

REM -----------------------------------------------------------------------------
Public Function IsSheetName(Optional ByRef InputStr As Variant) As Boolean
''' Return True if the input string can serve as a valid Calc sheet name
''' The sheet name must not contain the characters [ ] * ? : / \ 
''' or the character ' (apostrophe) as first or last character.

''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string is validated as a potential Calc sheet name, False otherwise
''' Examples:
'''  SF_String.IsSheetName("1àbc + ""def""") returns True

Dim bSheetName As Boolean    ' Return value
Const cstThisSub = "String.IsSheetName"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bSheetName = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then
  If Left(InputStr, 1) = "'" Or Right(InputStr, 1) = "'" Then
  ElseIf InStr(InputStr, "[") _
     + InStr(InputStr, "]") _
     + InStr(InputStr, "*") _
     + InStr(InputStr, "?") _
     + InStr(InputStr, ":") _
     + InStr(InputStr, "/") _
     + InStr(InputStr, "\") _
     = 0 Then
   bSheetName = True
  End If
 End If

Finally:
 IsSheetName = bSheetName
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsSheetName

REM -----------------------------------------------------------------------------
Public Function IsTitle(Optional ByRef InputStr As Variant) As Boolean
''' Return True if the 1st character of every word is in upper case and the other characters are in lower case
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string is capitalized and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsTitle("This Is A Title For Jean-Pierre") returns True

Dim bTitle As Boolean    ' Return value
Const cstThisSub = "String.IsTitle"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bTitle = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bTitle = ( StrComp(InputStr, SF_String.Capitalize(InputStr), 1) = 0 )

Finally:
 IsTitle = bTitle
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsTitle

REM -----------------------------------------------------------------------------
Public Function IsUpper(Optional ByRef InputStr As Variant) As Boolean
''' Return True if all characters in the string are in upper case
''' Non alphabetic characters are ignored
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains only upper case characters and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsUpper("ABC'(-XYZ") returns True

Dim bUpper As Boolean    ' Return value
Const cstThisSub = "String.IsUpper"
Const cstSubArgs = "InputStr"

 If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
 bUpper = False

Check:
 If SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
  If Not SF_Utils._Validate(InputStr, "InputStr", V_STRING) Then GoTo Finally
 End If

Try:
 If Len(InputStr) > 0 Then bUpper = ( StrComp(InputStr, UCase(InputStr), 1) = 0 )

Finally:
 IsUpper = bUpper
 SF_Utils._ExitFunction(cstThisSub)
 Exit Function
Catch:
 GoTo Finally
End Function ' ScriptForge.SF_String.IsUpper

REM -----------------------------------------------------------------------------
Public Function IsUrl(Optional ByRef InputStr As Variant) As Boolean
''' Return True if the string is a valid absolute URL (Uniform Resource Locator)
''' The parsing is done by the ParseStrict method of the URLTransformer UNO service
'''  https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XURLTransformer.html
''' Args:
'''  InputStr: the input string
''' Returns:
'''  True if the string contains a URL and there is at least one character, False otherwise
''' Examples:
'''  SF_String.IsUrl("http://foo.bar/?q=Test%20URL-encoded%20stuff") returns True

Dim bUrl As Boolean    ' Return value
--> --------------------

--> maximum size reached

--> --------------------

[ Seitenstruktur0.51Drucken  etwas mehr zur Ethik  ]

                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Produkte
     Quellcodebibliothek

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....

Besucherstatistik

Besucherstatistik

Monitoring

Montastic status badge